mirror of
https://github.com/grafana/grafana.git
synced 2024-11-27 11:20:27 -06:00
36 lines
921 B
Go
36 lines
921 B
Go
package middleware
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
"github.com/opentracing/opentracing-go/ext"
|
|
|
|
"gopkg.in/macaron.v1"
|
|
)
|
|
|
|
func RequestTracing(handler string) macaron.Handler {
|
|
return func(res http.ResponseWriter, req *http.Request, c *macaron.Context) {
|
|
rw := res.(macaron.ResponseWriter)
|
|
|
|
var span opentracing.Span
|
|
tracer := opentracing.GlobalTracer()
|
|
wireContext, _ := tracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header))
|
|
spanName := fmt.Sprintf("HTTP %s", handler)
|
|
span = tracer.StartSpan(spanName, ext.RPCServerOption(wireContext))
|
|
defer span.Finish()
|
|
|
|
ctx := opentracing.ContextWithSpan(req.Context(), span)
|
|
c.Req.Request = req.WithContext(ctx)
|
|
|
|
c.Next()
|
|
|
|
status := rw.Status()
|
|
|
|
ext.HTTPStatusCode.Set(span, uint16(status))
|
|
ext.HTTPUrl.Set(span, req.RequestURI)
|
|
ext.HTTPMethod.Set(span, req.Method)
|
|
}
|
|
}
|