mirror of
https://github.com/grafana/grafana.git
synced 2025-07-31 18:42:27 +08:00
Instrument backend plugin requests (#23346)
This commit is contained in:
47
pkg/plugins/backendplugin/instrumentation.go
Normal file
47
pkg/plugins/backendplugin/instrumentation.go
Normal file
@ -0,0 +1,47 @@
|
||||
package backendplugin
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// InstrumentPluginRequest instruments success rate and latency of `fn`
|
||||
func InstrumentPluginRequest(pluginID string, endpoint string, fn func() error) error {
|
||||
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
|
||||
}
|
Reference in New Issue
Block a user