mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
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:
parent
53758ad764
commit
ab4fc07cc7
@ -3,6 +3,8 @@ package pluginrequestmeta
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
)
|
||||
|
||||
// StatusSource is an enum-like string value representing the source of a
|
||||
@ -14,16 +16,19 @@ const (
|
||||
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{}
|
||||
|
||||
// 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 {
|
||||
value, ok := ctx.Value(statusSourceCtxKey{}).(*StatusSource)
|
||||
if ok {
|
||||
return *value
|
||||
}
|
||||
return StatusSourcePlugin
|
||||
return DefaultStatusSource
|
||||
}
|
||||
|
||||
// WithStatusSource sets the plugin request status source for the context.
|
||||
@ -42,3 +47,13 @@ func WithDownstreamStatusSource(ctx context.Context) error {
|
||||
*v = StatusSourceDownstream
|
||||
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)
|
||||
}
|
||||
|
@ -51,7 +51,7 @@ func (m *LoggerMiddleware) logRequest(ctx context.Context, fn func(ctx context.C
|
||||
logParams = append(logParams, "error", err)
|
||||
}
|
||||
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)
|
||||
@ -81,7 +81,11 @@ func (m *LoggerMiddleware) QueryData(ctx context.Context, req *backend.QueryData
|
||||
ctxLogger := m.logger.FromContext(ctx)
|
||||
for refID, dr := range resp.Responses {
|
||||
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...)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -16,7 +16,7 @@ func NewPluginRequestMetaMiddleware() plugins.ClientMiddleware {
|
||||
return plugins.ClientMiddlewareFunc(func(next plugins.Client) plugins.Client {
|
||||
return &PluginRequestMetaMiddleware{
|
||||
next: next,
|
||||
defaultStatusSource: pluginrequestmeta.StatusSourcePlugin,
|
||||
defaultStatusSource: pluginrequestmeta.DefaultStatusSource,
|
||||
}
|
||||
})
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user