mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Tracing: log traceID in request logger (#28952)
Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
parent
afb06ec21a
commit
db637a3630
@ -16,12 +16,15 @@
|
|||||||
package middleware
|
package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/grafana/grafana/pkg/models"
|
"github.com/grafana/grafana/pkg/models"
|
||||||
"github.com/grafana/grafana/pkg/setting"
|
"github.com/grafana/grafana/pkg/setting"
|
||||||
|
opentracing "github.com/opentracing/opentracing-go"
|
||||||
"github.com/prometheus/client_golang/prometheus"
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/uber/jaeger-client-go"
|
||||||
"gopkg.in/macaron.v1"
|
"gopkg.in/macaron.v1"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -49,13 +52,40 @@ func Logger() macaron.Handler {
|
|||||||
|
|
||||||
if ctx, ok := c.Data["ctx"]; ok {
|
if ctx, ok := c.Data["ctx"]; ok {
|
||||||
ctxTyped := ctx.(*models.ReqContext)
|
ctxTyped := ctx.(*models.ReqContext)
|
||||||
if status == 500 {
|
|
||||||
ctxTyped.Logger.Error("Request Completed", "method", req.Method, "path", req.URL.Path, "status", status,
|
logParams := []interface{}{
|
||||||
"remote_addr", c.RemoteAddr(), "time_ms", int64(timeTaken), "size", rw.Size(), "referer", req.Referer())
|
"method", req.Method,
|
||||||
|
"path", req.URL.Path,
|
||||||
|
"status", status,
|
||||||
|
"remote_addr", c.RemoteAddr(),
|
||||||
|
"time_ms", int64(timeTaken),
|
||||||
|
"size", rw.Size(),
|
||||||
|
"referer", req.Referer(),
|
||||||
|
}
|
||||||
|
|
||||||
|
traceID, exist := extractTraceID(ctxTyped.Req.Request.Context())
|
||||||
|
if exist {
|
||||||
|
logParams = append(logParams, "traceID", traceID)
|
||||||
|
}
|
||||||
|
|
||||||
|
if status >= 500 {
|
||||||
|
ctxTyped.Logger.Error("Request Completed", logParams...)
|
||||||
} else {
|
} else {
|
||||||
ctxTyped.Logger.Info("Request Completed", "method", req.Method, "path", req.URL.Path, "status", status,
|
ctxTyped.Logger.Info("Request Completed", logParams...)
|
||||||
"remote_addr", c.RemoteAddr(), "time_ms", int64(timeTaken), "size", rw.Size(), "referer", req.Referer())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func extractTraceID(ctx context.Context) (string, bool) {
|
||||||
|
sp := opentracing.SpanFromContext(ctx)
|
||||||
|
if sp == nil {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
sctx, ok := sp.Context().(jaeger.SpanContext)
|
||||||
|
if !ok {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
return sctx.TraceID().String(), true
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user