New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Logging: Add WithContextualAttributes
to pass log params based on the given context
#74428
Conversation
RegisterContextualLogProvider
RegisterContextualLogProvider
to pass log params to data source loggers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry I probably answered a bit confusing earlier. I'll respond in the existing review thread
@@ -146,6 +149,14 @@ func NewClientDecorator( | |||
c := client.ProvideService(pluginRegistry, pCfg) | |||
middlewares := CreateMiddlewares(cfg, oAuthTokenService, tracer, cachingService, features) | |||
|
|||
log.RegisterContextualLogProvider(func(ctx context.Context) ([]any, bool) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since WithContextualAttributes
is in the log package I would recommend putting this in there as well/together with it in the same package and by so you don't need to expose log.LogParamsContextKey
.
@@ -123,4 +130,9 @@ func reverseMiddlewares(middlewares []plugins.ClientMiddleware) []plugins.Client | |||
return reversed | |||
} | |||
|
|||
func instrumentContext(ctx context.Context, endpoint string, pCtx backend.PluginContext) context.Context { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the approach you did choose I think you can keep this in instrumentation.go for now as you had initially.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With that I needed to change the function signature in e.g. InstrumentCallResourceRequest
to also contain the context:https://github.com/grafana/grafana/pull/74428/files#diff-f68ce913764de9b2ec9c118f29476e2aeec3981d7561124e48659e5a8c2c6f78L111
Let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with some nits
RegisterContextualLogProvider
to pass log params to data source loggersWithContextualAttributes
to pass log params based on the given context
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay for this! Great job @svennergr for tackling this at backend plugin level rather than us having to pass these to every log individually. 🚀
…he given context (grafana#74428) * suggestion to use `RegisterContextualLogProvider` * add `pluginId` * change to `WithContextualAttributes` * move approach to instrumentation.go * improve `WithContextualAttributes` * unexport consts * typo * remove comment * add `nil` check
…he given context (#74428) * suggestion to use `RegisterContextualLogProvider` * add `pluginId` * change to `WithContextualAttributes` * move approach to instrumentation.go * improve `WithContextualAttributes` * unexport consts * typo * remove comment * add `nil` check
What is this feature?
This PR adds
WithContextualAttributes
method, that will just register some labels/log-parameters on the given context and return a new context with those values. If a new logger is created withlogger.FromContext
, those parameters will automatically be prepended.This is especially useful for datasource loggers to also include the
endpoint
,dsName
,dsUID
andpluginId
.