Skip to content

Commit 00393c2

Browse files
author
bobharper208
committed
Plugins: Chore: Renamed instrumentation middleware to metrics middleware (#76186)
* Plugins: Chore: Renamed instrumentation middleware to metrics middleware * Removed repeated logger attributes in middleware and contextual logger * renamed loggerParams to logParams * PR review suggestion * Add contextual logger middleware * Removed unused params from logRequest * Removed unwanted changes * Safer FromContext method * Removed traceID from logParams
1 parent abd2c9c commit 00393c2

File tree

8 files changed

+137
-78
lines changed

8 files changed

+137
-78
lines changed

pkg/plugins/log/fake.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package log
22

3+
import "context"
4+
35
var _ Logger = (*TestLogger)(nil)
46

57
type TestLogger struct {
@@ -41,6 +43,10 @@ func (f *TestLogger) Error(msg string, ctx ...any) {
4143
f.ErrorLogs.Ctx = ctx
4244
}
4345

46+
func (f *TestLogger) FromContext(_ context.Context) Logger {
47+
return NewTestLogger()
48+
}
49+
4450
type Logs struct {
4551
Calls int
4652
Message string

pkg/plugins/log/ifaces.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,40 @@
11
package log
22

3+
import "context"
4+
35
// Logger is the default logger
46
type Logger interface {
57
// New returns a new contextual Logger that has this logger's context plus the given context.
6-
New(ctx ...interface{}) Logger
8+
New(ctx ...any) Logger
79

810
// Debug logs a message with debug level and key/value pairs, if any.
9-
Debug(msg string, ctx ...interface{})
11+
Debug(msg string, ctx ...any)
1012

1113
// Info logs a message with info level and key/value pairs, if any.
12-
Info(msg string, ctx ...interface{})
14+
Info(msg string, ctx ...any)
1315

1416
// Warn logs a message with warning level and key/value pairs, if any.
15-
Warn(msg string, ctx ...interface{})
17+
Warn(msg string, ctx ...any)
1618

1719
// Error logs a message with error level and key/value pairs, if any.
18-
Error(msg string, ctx ...interface{})
20+
Error(msg string, ctx ...any)
21+
22+
// FromContext returns a new contextual Logger that has this logger's context plus the given context.
23+
FromContext(ctx context.Context) Logger
1924
}
2025

2126
// PrettyLogger is used primarily to facilitate logging/user feedback for both
2227
// the grafana-cli and the grafana backend when managing plugin installs
2328
type PrettyLogger interface {
24-
Successf(format string, args ...interface{})
25-
Failuref(format string, args ...interface{})
26-
27-
Info(args ...interface{})
28-
Infof(format string, args ...interface{})
29-
Debug(args ...interface{})
30-
Debugf(format string, args ...interface{})
31-
Warn(args ...interface{})
32-
Warnf(format string, args ...interface{})
33-
Error(args ...interface{})
34-
Errorf(format string, args ...interface{})
29+
Successf(format string, args ...any)
30+
Failuref(format string, args ...any)
31+
32+
Info(args ...any)
33+
Infof(format string, args ...any)
34+
Debug(args ...any)
35+
Debugf(format string, args ...any)
36+
Warn(args ...any)
37+
Warnf(format string, args ...any)
38+
Error(args ...any)
39+
Errorf(format string, args ...any)
3540
}

pkg/plugins/log/logger.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package log
22

33
import (
4+
"context"
5+
46
"github.com/grafana/grafana/pkg/infra/log"
57
)
68

@@ -42,3 +44,13 @@ func (d *grafanaInfraLogWrapper) Warn(msg string, ctx ...any) {
4244
func (d *grafanaInfraLogWrapper) Error(msg string, ctx ...any) {
4345
d.l.Error(msg, ctx...)
4446
}
47+
48+
func (d *grafanaInfraLogWrapper) FromContext(ctx context.Context) Logger {
49+
concreteInfraLogger, ok := d.l.FromContext(ctx).(*log.ConcreteLogger)
50+
if !ok {
51+
return d.New()
52+
}
53+
return &grafanaInfraLogWrapper{
54+
l: concreteInfraLogger,
55+
}
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package clientmiddleware
2+
3+
import (
4+
"context"
5+
6+
"github.com/grafana/grafana-plugin-sdk-go/backend"
7+
8+
"github.com/grafana/grafana/pkg/infra/log"
9+
"github.com/grafana/grafana/pkg/plugins"
10+
)
11+
12+
// NewContextualLoggerMiddleware creates a new plugins.ClientMiddleware that adds
13+
// a contextual logger to the request context.
14+
func NewContextualLoggerMiddleware() plugins.ClientMiddleware {
15+
return plugins.ClientMiddlewareFunc(func(next plugins.Client) plugins.Client {
16+
return &ContextualLoggerMiddleware{
17+
next: next,
18+
}
19+
})
20+
}
21+
22+
type ContextualLoggerMiddleware struct {
23+
next plugins.Client
24+
}
25+
26+
// instrumentContext adds a contextual logger with plugin and request details to the given context.
27+
func instrumentContext(ctx context.Context, endpoint string, pCtx backend.PluginContext) context.Context {
28+
p := []any{"endpoint", endpoint, "pluginId", pCtx.PluginID}
29+
if pCtx.DataSourceInstanceSettings != nil {
30+
p = append(p, "dsName", pCtx.DataSourceInstanceSettings.Name)
31+
p = append(p, "dsUID", pCtx.DataSourceInstanceSettings.UID)
32+
}
33+
if pCtx.User != nil {
34+
p = append(p, "uname", pCtx.User.Login)
35+
}
36+
return log.WithContextualAttributes(ctx, p)
37+
}
38+
39+
func (m *ContextualLoggerMiddleware) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
40+
ctx = instrumentContext(ctx, endpointQueryData, req.PluginContext)
41+
return m.next.QueryData(ctx, req)
42+
}
43+
44+
func (m *ContextualLoggerMiddleware) CallResource(ctx context.Context, req *backend.CallResourceRequest, sender backend.CallResourceResponseSender) error {
45+
ctx = instrumentContext(ctx, endpointCallResource, req.PluginContext)
46+
return m.next.CallResource(ctx, req, sender)
47+
}
48+
49+
func (m *ContextualLoggerMiddleware) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
50+
ctx = instrumentContext(ctx, endpointCheckHealth, req.PluginContext)
51+
return m.next.CheckHealth(ctx, req)
52+
}
53+
54+
func (m *ContextualLoggerMiddleware) CollectMetrics(ctx context.Context, req *backend.CollectMetricsRequest) (*backend.CollectMetricsResult, error) {
55+
ctx = instrumentContext(ctx, endpointCollectMetrics, req.PluginContext)
56+
return m.next.CollectMetrics(ctx, req)
57+
}
58+
59+
func (m *ContextualLoggerMiddleware) SubscribeStream(ctx context.Context, req *backend.SubscribeStreamRequest) (*backend.SubscribeStreamResponse, error) {
60+
return m.next.SubscribeStream(ctx, req)
61+
}
62+
63+
func (m *ContextualLoggerMiddleware) PublishStream(ctx context.Context, req *backend.PublishStreamRequest) (*backend.PublishStreamResponse, error) {
64+
return m.next.PublishStream(ctx, req)
65+
}
66+
67+
func (m *ContextualLoggerMiddleware) RunStream(ctx context.Context, req *backend.RunStreamRequest, sender *backend.StreamSender) error {
68+
return m.next.RunStream(ctx, req, sender)
69+
}

pkg/services/pluginsintegration/clientmiddleware/logger_middleware.go

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import (
66
"time"
77

88
"github.com/grafana/grafana-plugin-sdk-go/backend"
9+
910
"github.com/grafana/grafana/pkg/infra/log"
10-
"github.com/grafana/grafana/pkg/infra/tracing"
1111
"github.com/grafana/grafana/pkg/plugins"
1212
plog "github.com/grafana/grafana/pkg/plugins/log"
1313
"github.com/grafana/grafana/pkg/setting"
@@ -33,10 +33,11 @@ type LoggerMiddleware struct {
3333
logger plog.Logger
3434
}
3535

36-
func (m *LoggerMiddleware) logRequest(ctx context.Context, pluginCtx backend.PluginContext, endpoint string, fn func(ctx context.Context) error) error {
36+
func (m *LoggerMiddleware) logRequest(ctx context.Context, fn func(ctx context.Context) error) error {
3737
status := statusOK
3838
start := time.Now()
3939
timeBeforePluginRequest := log.TimeSinceStart(ctx, start)
40+
4041
err := fn(ctx)
4142
if err != nil {
4243
status = statusError
@@ -48,31 +49,13 @@ func (m *LoggerMiddleware) logRequest(ctx context.Context, pluginCtx backend.Plu
4849
logParams := []any{
4950
"status", status,
5051
"duration", time.Since(start),
51-
"pluginId", pluginCtx.PluginID,
52-
"endpoint", endpoint,
5352
"eventName", "grafana-data-egress",
5453
"time_before_plugin_request", timeBeforePluginRequest,
5554
}
56-
57-
if pluginCtx.User != nil {
58-
logParams = append(logParams, "uname", pluginCtx.User.Login)
59-
}
60-
61-
traceID := tracing.TraceIDFromContext(ctx, false)
62-
if traceID != "" {
63-
logParams = append(logParams, "traceID", traceID)
64-
}
65-
66-
if pluginCtx.DataSourceInstanceSettings != nil {
67-
logParams = append(logParams, "dsName", pluginCtx.DataSourceInstanceSettings.Name)
68-
logParams = append(logParams, "dsUID", pluginCtx.DataSourceInstanceSettings.UID)
69-
}
70-
7155
if status == statusError {
7256
logParams = append(logParams, "error", err)
7357
}
74-
75-
m.logger.Info("Plugin Request Completed", logParams...)
58+
m.logger.FromContext(ctx).Info("Plugin Request Completed", logParams...)
7659
return err
7760
}
7861

@@ -82,7 +65,7 @@ func (m *LoggerMiddleware) QueryData(ctx context.Context, req *backend.QueryData
8265
}
8366

8467
var resp *backend.QueryDataResponse
85-
err := m.logRequest(ctx, req.PluginContext, endpointQueryData, func(ctx context.Context) (innerErr error) {
68+
err := m.logRequest(ctx, func(ctx context.Context) (innerErr error) {
8669
resp, innerErr = m.next.QueryData(ctx, req)
8770
return innerErr
8871
})
@@ -95,7 +78,7 @@ func (m *LoggerMiddleware) CallResource(ctx context.Context, req *backend.CallRe
9578
return m.next.CallResource(ctx, req, sender)
9679
}
9780

98-
err := m.logRequest(ctx, req.PluginContext, endpointCallResource, func(ctx context.Context) (innerErr error) {
81+
err := m.logRequest(ctx, func(ctx context.Context) (innerErr error) {
9982
innerErr = m.next.CallResource(ctx, req, sender)
10083
return innerErr
10184
})
@@ -109,7 +92,7 @@ func (m *LoggerMiddleware) CheckHealth(ctx context.Context, req *backend.CheckHe
10992
}
11093

11194
var resp *backend.CheckHealthResult
112-
err := m.logRequest(ctx, req.PluginContext, endpointCheckHealth, func(ctx context.Context) (innerErr error) {
95+
err := m.logRequest(ctx, func(ctx context.Context) (innerErr error) {
11396
resp, innerErr = m.next.CheckHealth(ctx, req)
11497
return innerErr
11598
})
@@ -123,7 +106,7 @@ func (m *LoggerMiddleware) CollectMetrics(ctx context.Context, req *backend.Coll
123106
}
124107

125108
var resp *backend.CollectMetricsResult
126-
err := m.logRequest(ctx, req.PluginContext, endpointCollectMetrics, func(ctx context.Context) (innerErr error) {
109+
err := m.logRequest(ctx, func(ctx context.Context) (innerErr error) {
127110
resp, innerErr = m.next.CollectMetrics(ctx, req)
128111
return innerErr
129112
})

0 commit comments

Comments
 (0)