Plugins: Add statusSource to partial data response error log (#78057)

* Plugins: Add statusSource to partial data response error log

* Introduce DefaultStatusSource

* Add StatusSourceFromPluginErrorSource

* Moved StatusSourceFromPluginErrorSource

* Update pkg/services/pluginsintegration/clientmiddleware/logger_middleware.go
This commit is contained in:
Giuseppe Guerra 2023-11-14 15:27:48 +01:00 committed by GitHub
parent 53758ad764
commit ab4fc07cc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package pluginrequestmeta
import ( import (
"context" "context"
"errors" "errors"
"github.com/grafana/grafana-plugin-sdk-go/backend"
) )
// StatusSource is an enum-like string value representing the source of a // StatusSource is an enum-like string value representing the source of a
@ -14,16 +16,19 @@ const (
StatusSourceDownstream StatusSource = "downstream" StatusSourceDownstream StatusSource = "downstream"
) )
// DefaultStatusSource is the default StatusSource that should be used when it is not explicitly set by the plugin.
const DefaultStatusSource StatusSource = StatusSourcePlugin
type statusSourceCtxKey struct{} type statusSourceCtxKey struct{}
// StatusSourceFromContext returns the plugin request status source stored in the context. // StatusSourceFromContext returns the plugin request status source stored in the context.
// If no plugin request status source is stored in the context, [StatusSourcePlugin] is returned. // If no plugin request status source is stored in the context, [DefaultStatusSource] is returned.
func StatusSourceFromContext(ctx context.Context) StatusSource { func StatusSourceFromContext(ctx context.Context) StatusSource {
value, ok := ctx.Value(statusSourceCtxKey{}).(*StatusSource) value, ok := ctx.Value(statusSourceCtxKey{}).(*StatusSource)
if ok { if ok {
return *value return *value
} }
return StatusSourcePlugin return DefaultStatusSource
} }
// WithStatusSource sets the plugin request status source for the context. // WithStatusSource sets the plugin request status source for the context.
@ -42,3 +47,13 @@ func WithDownstreamStatusSource(ctx context.Context) error {
*v = StatusSourceDownstream *v = StatusSourceDownstream
return nil return nil
} }
// StatusSourceFromPluginErrorSource takes an error source returned by a plugin and returns the corresponding
// StatusSource. If the provided value is a zero-value (i.e.: the plugin did not set it), the function returns
// DefaultStatusSource.
func StatusSourceFromPluginErrorSource(pluginErrorSource backend.ErrorSource) StatusSource {
if pluginErrorSource == "" {
return DefaultStatusSource
}
return StatusSource(pluginErrorSource)
}

View File

@ -51,7 +51,7 @@ func (m *LoggerMiddleware) logRequest(ctx context.Context, fn func(ctx context.C
logParams = append(logParams, "error", err) logParams = append(logParams, "error", err)
} }
if m.features.IsEnabled(featuremgmt.FlagPluginsInstrumentationStatusSource) { if m.features.IsEnabled(featuremgmt.FlagPluginsInstrumentationStatusSource) {
logParams = append(logParams, "status_source", pluginrequestmeta.StatusSourceFromContext(ctx)) logParams = append(logParams, "statusSource", pluginrequestmeta.StatusSourceFromContext(ctx))
} }
ctxLogger := m.logger.FromContext(ctx) ctxLogger := m.logger.FromContext(ctx)
@ -81,7 +81,11 @@ func (m *LoggerMiddleware) QueryData(ctx context.Context, req *backend.QueryData
ctxLogger := m.logger.FromContext(ctx) ctxLogger := m.logger.FromContext(ctx)
for refID, dr := range resp.Responses { for refID, dr := range resp.Responses {
if dr.Error != nil { if dr.Error != nil {
ctxLogger.Error("Partial data response error", "refID", refID, "error", dr.Error) logParams := []any{"refID", refID, "status", int(dr.Status), "error", dr.Error}
if m.features.IsEnabled(featuremgmt.FlagPluginsInstrumentationStatusSource) {
logParams = append(logParams, "statusSource", pluginrequestmeta.StatusSourceFromPluginErrorSource(dr.ErrorSource))
}
ctxLogger.Error("Partial data response error", logParams...)
} }
} }

View File

@ -16,7 +16,7 @@ func NewPluginRequestMetaMiddleware() plugins.ClientMiddleware {
return plugins.ClientMiddlewareFunc(func(next plugins.Client) plugins.Client { return plugins.ClientMiddlewareFunc(func(next plugins.Client) plugins.Client {
return &PluginRequestMetaMiddleware{ return &PluginRequestMetaMiddleware{
next: next, next: next,
defaultStatusSource: pluginrequestmeta.StatusSourcePlugin, defaultStatusSource: pluginrequestmeta.DefaultStatusSource,
} }
}) })
} }