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
3 changed files with 24 additions and 5 deletions
@@ -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)
}