-
Notifications
You must be signed in to change notification settings - Fork 9
Plugins: Chore: Renamed instrumentation middleware to metrics middleware #3
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,40 @@ | ||
| package log | ||
|
|
||
| import "context" | ||
|
|
||
| // Logger is the default logger | ||
| type Logger interface { | ||
| // New returns a new contextual Logger that has this logger's context plus the given context. | ||
| New(ctx ...interface{}) Logger | ||
| New(ctx ...any) Logger | ||
|
|
||
| // Debug logs a message with debug level and key/value pairs, if any. | ||
| Debug(msg string, ctx ...interface{}) | ||
| Debug(msg string, ctx ...any) | ||
|
|
||
| // Info logs a message with info level and key/value pairs, if any. | ||
| Info(msg string, ctx ...interface{}) | ||
| Info(msg string, ctx ...any) | ||
|
|
||
| // Warn logs a message with warning level and key/value pairs, if any. | ||
| Warn(msg string, ctx ...interface{}) | ||
| Warn(msg string, ctx ...any) | ||
|
|
||
| // Error logs a message with error level and key/value pairs, if any. | ||
| Error(msg string, ctx ...interface{}) | ||
| Error(msg string, ctx ...any) | ||
|
|
||
| // FromContext returns a new contextual Logger that has this logger's context plus the given context. | ||
| FromContext(ctx context.Context) Logger | ||
| } | ||
|
|
||
| // PrettyLogger is used primarily to facilitate logging/user feedback for both | ||
| // the grafana-cli and the grafana backend when managing plugin installs | ||
| type PrettyLogger interface { | ||
| Successf(format string, args ...interface{}) | ||
| Failuref(format string, args ...interface{}) | ||
|
|
||
| Info(args ...interface{}) | ||
| Infof(format string, args ...interface{}) | ||
| Debug(args ...interface{}) | ||
| Debugf(format string, args ...interface{}) | ||
| Warn(args ...interface{}) | ||
| Warnf(format string, args ...interface{}) | ||
| Error(args ...interface{}) | ||
| Errorf(format string, args ...interface{}) | ||
| Successf(format string, args ...any) | ||
| Failuref(format string, args ...any) | ||
|
|
||
| Info(args ...any) | ||
| Infof(format string, args ...any) | ||
| Debug(args ...any) | ||
| Debugf(format string, args ...any) | ||
| Warn(args ...any) | ||
| Warnf(format string, args ...any) | ||
| Error(args ...any) | ||
| Errorf(format string, args ...any) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| package clientmiddleware | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/grafana/grafana-plugin-sdk-go/backend" | ||
|
|
||
| "github.com/grafana/grafana/pkg/infra/log" | ||
| "github.com/grafana/grafana/pkg/plugins" | ||
| ) | ||
|
|
||
| // NewContextualLoggerMiddleware creates a new plugins.ClientMiddleware that adds | ||
| // a contextual logger to the request context. | ||
| func NewContextualLoggerMiddleware() plugins.ClientMiddleware { | ||
| return plugins.ClientMiddlewareFunc(func(next plugins.Client) plugins.Client { | ||
| return &ContextualLoggerMiddleware{ | ||
| next: next, | ||
| } | ||
| }) | ||
| } | ||
|
|
||
| type ContextualLoggerMiddleware struct { | ||
| next plugins.Client | ||
| } | ||
|
|
||
| // instrumentContext adds a contextual logger with plugin and request details to the given context. | ||
| func instrumentContext(ctx context.Context, endpoint string, pCtx backend.PluginContext) context.Context { | ||
| p := []any{"endpoint", endpoint, "pluginId", pCtx.PluginID} | ||
| if pCtx.DataSourceInstanceSettings != nil { | ||
| p = append(p, "dsName", pCtx.DataSourceInstanceSettings.Name) | ||
| p = append(p, "dsUID", pCtx.DataSourceInstanceSettings.UID) | ||
| } | ||
| if pCtx.User != nil { | ||
| p = append(p, "uname", pCtx.User.Login) | ||
| } | ||
| return log.WithContextualAttributes(ctx, p) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { | ||
| ctx = instrumentContext(ctx, endpointQueryData, req.PluginContext) | ||
| return m.next.QueryData(ctx, req) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error { | ||
| ctx = instrumentContext(ctx, endpointCallResource, req.PluginContext) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: endpointCallResource is not defined in this file or imported. This will cause a compilation error. |
||
| return m.next.CallResource(ctx, req, sender) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { | ||
| ctx = instrumentContext(ctx, endpointCheckHealth, req.PluginContext) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: endpointCheckHealth is not defined in this file or imported. This will cause a compilation error. |
||
| return m.next.CheckHealth(ctx, req) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) { | ||
| ctx = instrumentContext(ctx, endpointCollectMetrics, req.PluginContext) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: endpointCollectMetrics is not defined in this file or imported. This will cause a compilation error. |
||
| return m.next.CollectMetrics(ctx, req) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) { | ||
| return m.next.SubscribeStream(ctx, req) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) { | ||
| return m.next.PublishStream(ctx, req) | ||
| } | ||
|
|
||
| func (m *ContextualLoggerMiddleware) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error { | ||
| return m.next.RunStream(ctx, req, sender) | ||
| } | ||
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.
logic: endpointQueryData is not defined in this file or imported. This will cause a compilation error.