mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Improve client instrumentation by adding cancelled status and logging errors (#65099)
add cancelled status and log err
This commit is contained in:
parent
89c65e22d3
commit
23e3ac0ac9
@ -3,6 +3,7 @@ package instrumentation
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
@ -30,19 +31,27 @@ var (
|
||||
}, []string{"plugin_id", "endpoint", "target"})
|
||||
)
|
||||
|
||||
const (
|
||||
statusOK = "ok"
|
||||
statusError = "error"
|
||||
statusCancelled = "cancelled"
|
||||
)
|
||||
|
||||
var logger = plog.New("plugin.instrumentation")
|
||||
|
||||
// instrumentPluginRequest instruments success rate and latency of `fn`
|
||||
func instrumentPluginRequest(ctx context.Context, cfg Cfg, pluginCtx *backend.PluginContext, endpoint string, fn func() error) error {
|
||||
status := "ok"
|
||||
status := statusOK
|
||||
|
||||
start := time.Now()
|
||||
|
||||
timeBeforePluginRequest := log.TimeSinceStart(ctx, start)
|
||||
|
||||
err := fn()
|
||||
if err != nil {
|
||||
status = "error"
|
||||
status = statusError
|
||||
if errors.Is(err, context.Canceled) {
|
||||
status = statusCancelled
|
||||
}
|
||||
}
|
||||
|
||||
elapsed := time.Since(start)
|
||||
@ -73,6 +82,10 @@ func instrumentPluginRequest(ctx context.Context, cfg Cfg, pluginCtx *backend.Pl
|
||||
logParams = append(logParams, "dsUID", pluginCtx.DataSourceInstanceSettings.UID)
|
||||
}
|
||||
|
||||
if status == statusError {
|
||||
logParams = append(logParams, "error", err)
|
||||
}
|
||||
|
||||
logger.Info("Plugin Request Completed", logParams...)
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user