func (c *T) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	if opentracing.IsGlobalTracerRegistered() {
		span := opentracing.SpanFromContext(req.Context())
		span.Finish()
	}
	start := c.clock.UtcNow()
	pw := utils.NewProxyWriter(w)
	c.handler.ServeHTTP(pw, req)
	diff := c.clock.UtcNow().Sub(start)
	c.mu.Lock()
	defer c.mu.Unlock()
	c.rtm.Record(pw.StatusCode(), diff)
	if beSrvEnt, ok := c.beSrvRTMs[backend.NewSrvURLKey(req.URL)]; ok {
		beSrvEnt.rtm.Record(pw.StatusCode(), diff)
	}
}
ServeHTTP implements http.Handler.
package rtmcollect
import (
	"github.com/opentracing/opentracing-go"
	"net/http"
	"sync"
	"github.com/mailgun/timetools"
	"github.com/pkg/errors"
	"github.com/vulcand/oxy/memmetrics"
	"github.com/vulcand/oxy/utils"
	"github.com/vulcand/vulcand/engine"
	"github.com/vulcand/vulcand/proxy/backend"
)
func NewRTMetrics() *memmetrics.RTMetrics {
	rtm, err := memmetrics.NewRTMetrics()
	if err != nil {
		panic(errors.Wrap(err, "must never fail"))
	}
	return rtm
}
type T struct {
	mu        sync.Mutex
	rtm       *memmetrics.RTMetrics
	beSrvRTMs map[backend.SrvURLKey]BeSrvEntry
	clock     timetools.TimeProvider
	handler   http.Handler
}
type BeSrvEntry struct {
	beSrvCfg engine.Server
	rtm      *memmetrics.RTMetrics
}
func (e *BeSrvEntry) CfgWithStats() engine.Server {
	cfg := e.beSrvCfg
	var err error
	cfg.Stats, err = engine.NewRoundTripStats(e.rtm)
	if err != nil {
		panic(errors.Wrap(err, "must never fail"))
	}
	return cfg
}
func New(handler http.Handler) (*T, error) {
	feRTM, err := memmetrics.NewRTMetrics()
	if err != nil {
		return nil, err
	}
	return &T{
		rtm:       feRTM,
		beSrvRTMs: make(map[backend.SrvURLKey]BeSrvEntry),
		clock:     &timetools.RealTime{},
		handler:   handler,
	}, nil
}