2021-03-08 07:02:49 +01:00
|
|
|
// Package instrumentation contains backend plugin instrumentation logic.
|
|
|
|
|
package instrumentation
|
2020-04-09 14:47:46 +02:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
pluginRequestCounter *prometheus.CounterVec
|
|
|
|
|
pluginRequestDuration *prometheus.SummaryVec
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
pluginRequestCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
|
|
|
|
|
Namespace: "grafana",
|
|
|
|
|
Name: "plugin_request_total",
|
|
|
|
|
Help: "The total amount of plugin requests",
|
|
|
|
|
}, []string{"plugin_id", "endpoint", "status"})
|
|
|
|
|
|
|
|
|
|
pluginRequestDuration = prometheus.NewSummaryVec(prometheus.SummaryOpts{
|
|
|
|
|
Namespace: "grafana",
|
|
|
|
|
Name: "plugin_request_duration_milliseconds",
|
|
|
|
|
Help: "Plugin request duration",
|
|
|
|
|
Objectives: map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
|
|
|
|
|
}, []string{"plugin_id", "endpoint"})
|
|
|
|
|
|
|
|
|
|
prometheus.MustRegister(pluginRequestCounter, pluginRequestDuration)
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-11 16:14:05 +02:00
|
|
|
// instrumentPluginRequest instruments success rate and latency of `fn`
|
|
|
|
|
func instrumentPluginRequest(pluginID string, endpoint string, fn func() error) error {
|
2020-04-09 14:47:46 +02:00
|
|
|
status := "ok"
|
|
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
|
|
err := fn()
|
|
|
|
|
if err != nil {
|
|
|
|
|
status = "error"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
elapsed := time.Since(start) / time.Millisecond
|
|
|
|
|
pluginRequestDuration.WithLabelValues(pluginID, endpoint).Observe(float64(elapsed))
|
|
|
|
|
pluginRequestCounter.WithLabelValues(pluginID, endpoint, status).Inc()
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|
2020-06-11 16:14:05 +02:00
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
// InstrumentCollectMetrics instruments collectMetrics.
|
|
|
|
|
func InstrumentCollectMetrics(pluginID string, fn func() error) error {
|
2020-06-11 16:14:05 +02:00
|
|
|
return instrumentPluginRequest(pluginID, "collectMetrics", fn)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
// InstrumentCheckHealthRequest instruments checkHealth.
|
|
|
|
|
func InstrumentCheckHealthRequest(pluginID string, fn func() error) error {
|
2020-06-11 16:14:05 +02:00
|
|
|
return instrumentPluginRequest(pluginID, "checkHealth", fn)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
// InstrumentCallResourceRequest instruments callResource.
|
|
|
|
|
func InstrumentCallResourceRequest(pluginID string, fn func() error) error {
|
2020-06-11 16:14:05 +02:00
|
|
|
return instrumentPluginRequest(pluginID, "callResource", fn)
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 07:02:49 +01:00
|
|
|
// InstrumentQueryDataRequest instruments success rate and latency of query data requests.
|
2020-06-11 16:14:05 +02:00
|
|
|
func InstrumentQueryDataRequest(pluginID string, fn func() error) error {
|
|
|
|
|
return instrumentPluginRequest(pluginID, "queryData", fn)
|
|
|
|
|
}
|