mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Logging: Introduce API for contextual logging (#55198)
Introduces a FromContext method on the log.Logger interface that allows contextual key/value pairs to be attached, e.g. per request, so that any logger using this API will automatically get the per request context attached. The proposal makes the traceID available for contextual logger , if available, and would allow logs originating from a certain HTTP request to be correlated with traceID. In addition, when tracing not enabled, skip adding traceID=00000000000000000000000000000000 to logs.
This commit is contained in:
committed by
GitHub
parent
c6ed7d6741
commit
862a6a2fa6
@@ -30,16 +30,18 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route
|
||||
SecureJsonData: ds.DecryptedSecureJSONData,
|
||||
}
|
||||
|
||||
ctxLogger := logger.FromContext(ctx)
|
||||
|
||||
if len(route.URL) > 0 {
|
||||
interpolatedURL, err := interpolateString(route.URL, data)
|
||||
if err != nil {
|
||||
logger.Error("Error interpolating proxy url", "error", err)
|
||||
ctxLogger.Error("Error interpolating proxy url", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
routeURL, err := url.Parse(interpolatedURL)
|
||||
if err != nil {
|
||||
logger.Error("Error parsing plugin route url", "error", err)
|
||||
ctxLogger.Error("Error parsing plugin route url", "error", err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -50,29 +52,29 @@ func ApplyRoute(ctx context.Context, req *http.Request, proxyPath string, route
|
||||
}
|
||||
|
||||
if err := addQueryString(req, route, data); err != nil {
|
||||
logger.Error("Failed to render plugin URL query string", "error", err)
|
||||
ctxLogger.Error("Failed to render plugin URL query string", "error", err)
|
||||
}
|
||||
|
||||
if err := addHeaders(&req.Header, route, data); err != nil {
|
||||
logger.Error("Failed to render plugin headers", "error", err)
|
||||
ctxLogger.Error("Failed to render plugin headers", "error", err)
|
||||
}
|
||||
|
||||
if err := setBodyContent(req, route, data); err != nil {
|
||||
logger.Error("Failed to set plugin route body content", "error", err)
|
||||
ctxLogger.Error("Failed to set plugin route body content", "error", err)
|
||||
}
|
||||
|
||||
if tokenProvider, err := getTokenProvider(ctx, cfg, ds, route, data); err != nil {
|
||||
logger.Error("Failed to resolve auth token provider", "error", err)
|
||||
ctxLogger.Error("Failed to resolve auth token provider", "error", err)
|
||||
} else if tokenProvider != nil {
|
||||
if token, err := tokenProvider.GetAccessToken(); err != nil {
|
||||
logger.Error("Failed to get access token", "error", err)
|
||||
ctxLogger.Error("Failed to get access token", "error", err)
|
||||
} else {
|
||||
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
|
||||
}
|
||||
}
|
||||
|
||||
if cfg.DataProxyLogging {
|
||||
logger.Debug("Requesting", "url", req.URL.String())
|
||||
ctxLogger.Debug("Requesting", "url", req.URL.String())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user