|
|
|
@ -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...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|