mirror of
https://github.com/grafana/grafana.git
synced 2024-12-27 09:21:35 -06:00
Plugins: Add username to datasource plugin logging (#59893)
Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com> Signed-off-by: bergquist <carl.bergquist@gmail.com>
This commit is contained in:
parent
1b676d0d49
commit
a5ace56be8
28
pkg/infra/log/requestTiming.go
Normal file
28
pkg/infra/log/requestTiming.go
Normal file
@ -0,0 +1,28 @@
|
||||
package log
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
type requestStartTimeContextKey struct{}
|
||||
|
||||
var requestStartTime = requestStartTimeContextKey{}
|
||||
|
||||
// InitCounter creates a pointer on the context that can be incremented later
|
||||
func InitstartTime(ctx context.Context, now time.Time) context.Context {
|
||||
return context.WithValue(ctx, requestStartTime, now)
|
||||
}
|
||||
|
||||
// TimeSinceStart returns time spend since the request started in grafana
|
||||
func TimeSinceStart(ctx context.Context, now time.Time) time.Duration {
|
||||
val := ctx.Value(requestStartTime)
|
||||
if val != nil {
|
||||
startTime, ok := val.(time.Time)
|
||||
if ok {
|
||||
return now.Sub(startTime)
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
@ -35,6 +35,8 @@ func Logger(cfg *setting.Cfg) web.Middleware {
|
||||
|
||||
// we have to init the context with the counter here to update the request
|
||||
r = r.WithContext(log.InitCounter(r.Context()))
|
||||
// put the start time on context so we can measure it later.
|
||||
r = r.WithContext(log.InitstartTime(r.Context(), time.Now()))
|
||||
|
||||
rw := web.Rw(w, r)
|
||||
next.ServeHTTP(rw, r)
|
||||
|
@ -51,6 +51,13 @@ func instrumentPluginRequest(ctx context.Context, cfg *config.Cfg, pluginCtx *ba
|
||||
"duration", elapsed,
|
||||
"pluginId", pluginCtx.PluginID,
|
||||
"endpoint", endpoint,
|
||||
"eventName", "grafana-data-egress",
|
||||
"insight_logs", true,
|
||||
"since_grafana_request_started", log.TimeSinceStart(ctx, time.Now()),
|
||||
}
|
||||
|
||||
if pluginCtx.User != nil {
|
||||
logParams = append(logParams, "uname", pluginCtx.User.Login)
|
||||
}
|
||||
|
||||
traceID := tracing.TraceIDFromContext(ctx, false)
|
||||
|
Loading…
Reference in New Issue
Block a user