@ -7,52 +7,48 @@ import (
"github.com/grafana/grafana/pkg/infra/httpclient"
"github.com/grafana/grafana/pkg/infra/metrics/metricutil"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var datasourceRequestCounter = prometheus . NewCounterVec (
prometheus . CounterOpts {
Namespace : "grafana" ,
Name : "datasource_request_total" ,
Help : "A counter for outgoing requests for a datasource" ,
} ,
[ ] string { "datasource" , "code" , "method" } ,
var (
datasourceRequestCounter = promauto . NewCounterVec (
prometheus . CounterOpts {
Namespace : "grafana" ,
Name : "datasource_request_total" ,
Help : "A counter for outgoing requests for a data source" ,
} ,
[ ] string { "datasource" , "code" , "method" } ,
)
datasourceRequestHistogram = promauto . NewHistogramVec (
prometheus . HistogramOpts {
Namespace : "grafana" ,
Name : "datasource_request_duration_seconds" ,
Help : "summary of outgoing data source requests sent from Grafana" ,
Buckets : [ ] float64 { .005 , .01 , .025 , .05 , .1 , .25 , .5 , 1 , 2.5 , 5 , 10 , 25 , 50 , 100 } ,
} , [ ] string { "datasource" , "code" , "method" } ,
)
datasourceResponseHistogram = promauto . NewHistogramVec (
prometheus . HistogramOpts {
Namespace : "grafana" ,
Name : "datasource_response_size_bytes" ,
Help : "summary of data source response sizes returned to Grafana" ,
Buckets : [ ] float64 { 128 , 256 , 512 , 1024 , 2048 , 4096 , 8192 , 16384 , 32768 , 65536 , 131072 , 262144 , 524288 , 1048576 } ,
} , [ ] string { "datasource" } ,
)
datasourceRequestsInFlight = promauto . NewGaugeVec (
prometheus . GaugeOpts {
Namespace : "grafana" ,
Name : "datasource_request_in_flight" ,
Help : "A gauge of outgoing data source requests currently being sent by Grafana" ,
} ,
[ ] string { "datasource" } ,
)
)
var datasourceRequestSummary = prometheus . NewSummaryVec (
prometheus . SummaryOpts {
Namespace : "grafana" ,
Name : "datasource_request_duration_seconds" ,
Help : "summary of outgoing datasource requests sent from Grafana" ,
Objectives : map [ float64 ] float64 { 0.5 : 0.05 , 0.9 : 0.01 , 0.99 : 0.001 } ,
} , [ ] string { "datasource" , "code" , "method" } ,
)
var datasourceResponseSummary = prometheus . NewSummaryVec (
prometheus . SummaryOpts {
Namespace : "grafana" ,
Name : "datasource_response_size_bytes" ,
Help : "summary of datasource response sizes returned to Grafana" ,
Objectives : map [ float64 ] float64 { 0.5 : 0.05 , 0.9 : 0.01 , 0.99 : 0.001 } ,
} , [ ] string { "datasource" } ,
)
var datasourceRequestsInFlight = prometheus . NewGaugeVec (
prometheus . GaugeOpts {
Namespace : "grafana" ,
Name : "datasource_request_in_flight" ,
Help : "A gauge of outgoing datasource requests currently being sent by Grafana" ,
} ,
[ ] string { "datasource" } ,
)
func init ( ) {
prometheus . MustRegister ( datasourceRequestSummary ,
datasourceRequestCounter ,
datasourceRequestsInFlight ,
datasourceResponseSummary )
}
const DataSourceMetricsMiddlewareName = "metrics"
var executeMiddlewareFunc = executeMiddleware
@ -84,11 +80,11 @@ func DataSourceMetricsMiddleware() sdkhttpclient.Middleware {
func executeMiddleware ( next http . RoundTripper , datasourceLabel prometheus . Labels ) http . RoundTripper {
return sdkhttpclient . RoundTripperFunc ( func ( r * http . Request ) ( * http . Response , error ) {
requestCounter := datasourceRequestCounter . MustCurryWith ( datasourceLabel )
requestSummary := datasourceRequestSummary . MustCurryWith ( datasourceLabel )
requestHistogram := datasourceRequestHistogram . MustCurryWith ( datasourceLabel )
requestInFlight := datasourceRequestsInFlight . With ( datasourceLabel )
responseSizeSummary := datasourceResponseSummary . With ( datasourceLabel )
responseSizeHistogram := datasourceResponseHistogram . With ( datasourceLabel )
res , err := promhttp . InstrumentRoundTripperDuration ( requestSummary ,
res , err := promhttp . InstrumentRoundTripperDuration ( requestHistogram ,
promhttp . InstrumentRoundTripperCounter ( requestCounter ,
promhttp . InstrumentRoundTripperInFlight ( requestInFlight , next ) ) ) .
RoundTrip ( r )
@ -98,7 +94,7 @@ func executeMiddleware(next http.RoundTripper, datasourceLabel prometheus.Labels
if res != nil && res . StatusCode != http . StatusSwitchingProtocols {
res . Body = httpclient . CountBytesReader ( res . Body , func ( bytesRead int64 ) {
responseSizeSummary . Observe ( float64 ( bytesRead ) )
responseSizeHistogram . Observe ( float64 ( bytesRead ) )
} )
}