|
|
|
@ -2,8 +2,13 @@ |
|
|
|
|
package instrumentation |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend" |
|
|
|
|
"github.com/grafana/grafana/pkg/infra/log" |
|
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing" |
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/config" |
|
|
|
|
"github.com/prometheus/client_golang/prometheus" |
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promauto" |
|
|
|
|
) |
|
|
|
@ -23,8 +28,10 @@ var ( |
|
|
|
|
}, []string{"plugin_id", "endpoint"}) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
var logger log.Logger = log.New("plugin.instrumentation") |
|
|
|
|
|
|
|
|
|
// instrumentPluginRequest instruments success rate and latency of `fn`
|
|
|
|
|
func instrumentPluginRequest(pluginID string, endpoint string, fn func() error) error { |
|
|
|
|
func instrumentPluginRequest(ctx context.Context, cfg *config.Cfg, pluginCtx *backend.PluginContext, endpoint string, fn func() error) error { |
|
|
|
|
status := "ok" |
|
|
|
|
|
|
|
|
|
start := time.Now() |
|
|
|
@ -34,29 +41,50 @@ func instrumentPluginRequest(pluginID string, endpoint string, fn func() error) |
|
|
|
|
status = "error" |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
elapsed := time.Since(start) / time.Millisecond |
|
|
|
|
pluginRequestDuration.WithLabelValues(pluginID, endpoint).Observe(float64(elapsed)) |
|
|
|
|
pluginRequestCounter.WithLabelValues(pluginID, endpoint, status).Inc() |
|
|
|
|
elapsed := time.Since(start) |
|
|
|
|
pluginRequestDuration.WithLabelValues(pluginCtx.PluginID, endpoint).Observe(float64(elapsed / time.Millisecond)) |
|
|
|
|
pluginRequestCounter.WithLabelValues(pluginCtx.PluginID, endpoint, status).Inc() |
|
|
|
|
|
|
|
|
|
if cfg.LogDatasourceRequests { |
|
|
|
|
logParams := []interface{}{ |
|
|
|
|
"status", status, |
|
|
|
|
"duration", elapsed, |
|
|
|
|
"pluginId", pluginCtx.PluginID, |
|
|
|
|
"endpoint", endpoint, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
traceID := tracing.TraceIDFromContext(ctx, false) |
|
|
|
|
if traceID != "" { |
|
|
|
|
logParams = append(logParams, "traceID", traceID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if pluginCtx.DataSourceInstanceSettings != nil { |
|
|
|
|
logParams = append(logParams, "dsName", pluginCtx.DataSourceInstanceSettings.Name) |
|
|
|
|
logParams = append(logParams, "dsUID", pluginCtx.DataSourceInstanceSettings.UID) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
logger.Info("Plugin Request Completed", logParams...) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// InstrumentCollectMetrics instruments collectMetrics.
|
|
|
|
|
func InstrumentCollectMetrics(pluginID string, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(pluginID, "collectMetrics", fn) |
|
|
|
|
func InstrumentCollectMetrics(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(ctx, cfg, req, "collectMetrics", fn) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// InstrumentCheckHealthRequest instruments checkHealth.
|
|
|
|
|
func InstrumentCheckHealthRequest(pluginID string, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(pluginID, "checkHealth", fn) |
|
|
|
|
func InstrumentCheckHealthRequest(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(ctx, cfg, req, "checkHealth", fn) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// InstrumentCallResourceRequest instruments callResource.
|
|
|
|
|
func InstrumentCallResourceRequest(pluginID string, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(pluginID, "callResource", fn) |
|
|
|
|
func InstrumentCallResourceRequest(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(ctx, cfg, req, "callResource", fn) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// InstrumentQueryDataRequest instruments success rate and latency of query data requests.
|
|
|
|
|
func InstrumentQueryDataRequest(pluginID string, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(pluginID, "queryData", fn) |
|
|
|
|
func InstrumentQueryDataRequest(ctx context.Context, req *backend.PluginContext, cfg *config.Cfg, fn func() error) error { |
|
|
|
|
return instrumentPluginRequest(ctx, cfg, req, "queryData", fn) |
|
|
|
|
} |
|
|
|
|