grafana/pkg/middleware/request_tracing.go

37 lines
925 B
Go
Raw Normal View History

2017-09-10 07:13:57 -05:00
package middleware
import (
"fmt"
2017-09-10 07:13:57 -05:00
"net/http"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
2017-09-10 07:13:57 -05:00
"gopkg.in/macaron.v1"
)
2017-09-14 08:05:49 -05:00
func RequestTracing(handler string) macaron.Handler {
2017-09-10 07:13:57 -05:00
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
rw := res.(macaron.ResponseWriter)
tracer := opentracing.GlobalTracer()
wireContext, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
2017-09-14 12:01:22 -05:00
span := tracer.StartSpan(fmt.Sprintf("HTTP %s", handler), ext.RPCServerOption(wireContext))
2017-09-10 07:13:57 -05:00
defer span.Finish()
ctx := opentracing.ContextWithSpan(req.Context(), span)
c.Req.Request = req.WithContext(ctx)
2017-09-10 07:13:57 -05:00
c.Next()
status := rw.Status()
ext.HTTPStatusCode.Set(span, uint16(status))
ext.HTTPUrl.Set(span, req.RequestURI)
ext.HTTPMethod.Set(span, req.Method)
2017-09-18 02:10:15 -05:00
if status >= 400 {
ext.Error.Set(span, true)
}
2017-09-10 07:13:57 -05:00
}
}