2016-09-09 06:28:19 -05:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2017-09-06 15:24:10 -05:00
|
|
|
"strconv"
|
2016-09-09 06:28:19 -05:00
|
|
|
"strings"
|
2017-09-06 15:24:10 -05:00
|
|
|
"time"
|
2016-09-09 06:28:19 -05:00
|
|
|
|
2023-01-30 02:26:42 -06:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
|
2022-06-10 03:56:55 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2019-02-23 16:35:26 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/metrics"
|
2022-04-14 10:54:49 -05:00
|
|
|
"github.com/grafana/grafana/pkg/infra/tracing"
|
2023-08-16 08:05:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/middleware/requestmeta"
|
2022-01-26 11:44:20 -06:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
2023-08-16 08:05:19 -05:00
|
|
|
"github.com/grafana/grafana/pkg/setting"
|
2021-10-11 07:30:59 -05:00
|
|
|
"github.com/grafana/grafana/pkg/web"
|
2016-09-09 06:28:19 -05:00
|
|
|
)
|
|
|
|
|
2020-02-19 11:29:47 -06:00
|
|
|
var (
|
2020-12-01 08:04:59 -06:00
|
|
|
// DefBuckets are histogram buckets for the response time (in seconds)
|
|
|
|
// of a network service, including one that is responding very slowly.
|
2022-07-26 04:38:59 -05:00
|
|
|
defBuckets = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10, 25}
|
2020-02-19 11:29:47 -06:00
|
|
|
)
|
|
|
|
|
2023-08-16 08:05:19 -05:00
|
|
|
// RequestMetrics is a middleware handler that instruments the request.
|
|
|
|
func RequestMetrics(features featuremgmt.FeatureToggles, cfg *setting.Cfg, promRegister prometheus.Registerer) web.Middleware {
|
|
|
|
log := log.New("middleware.request-metrics")
|
|
|
|
|
|
|
|
httpRequestsInFlight := prometheus.NewGauge(
|
2020-02-19 11:29:47 -06:00
|
|
|
prometheus.GaugeOpts{
|
2021-05-11 07:37:03 -05:00
|
|
|
Namespace: "grafana",
|
|
|
|
Name: "http_request_in_flight",
|
|
|
|
Help: "A gauge of requests currently being served by Grafana.",
|
2020-02-19 11:29:47 -06:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2024-02-06 02:29:41 -06:00
|
|
|
histogramLabels := []string{"handler", "status_code", "method", "status_source", "slo_group"}
|
2023-09-11 05:13:13 -05:00
|
|
|
|
2023-08-16 08:05:19 -05:00
|
|
|
if cfg.MetricsIncludeTeamLabel {
|
2023-10-03 05:25:01 -05:00
|
|
|
histogramLabels = append(histogramLabels, "grafana_team")
|
2023-08-16 08:05:19 -05:00
|
|
|
}
|
|
|
|
|
2023-10-03 13:23:55 -05:00
|
|
|
histogramOptions := prometheus.HistogramOpts{
|
|
|
|
Namespace: "grafana",
|
|
|
|
Name: "http_request_duration_seconds",
|
|
|
|
Help: "Histogram of latencies for HTTP requests.",
|
|
|
|
Buckets: defBuckets,
|
|
|
|
}
|
|
|
|
|
2023-11-14 14:50:27 -06:00
|
|
|
if features.IsEnabledGlobally(featuremgmt.FlagEnableNativeHTTPHistogram) {
|
2023-10-03 13:23:55 -05:00
|
|
|
// the recommended default value from the prom_client
|
|
|
|
// https://github.com/prometheus/client_golang/blob/main/prometheus/histogram.go#L411
|
2024-06-18 14:37:44 -05:00
|
|
|
// Giving this variable a value means the client will expose a native
|
|
|
|
// histogram.
|
2023-10-03 13:23:55 -05:00
|
|
|
histogramOptions.NativeHistogramBucketFactor = 1.1
|
|
|
|
// The default value in OTel. It probably good enough for us as well.
|
|
|
|
histogramOptions.NativeHistogramMaxBucketNumber = 160
|
|
|
|
histogramOptions.NativeHistogramMinResetDuration = time.Hour
|
2024-06-18 14:37:44 -05:00
|
|
|
|
|
|
|
if features.IsEnabledGlobally(featuremgmt.FlagDisableClassicHTTPHistogram) {
|
|
|
|
// setting Buckets to nil with native options set means the classic
|
|
|
|
// histogram will no longer be exposed - this can be a good way to
|
|
|
|
// reduce cardinality in the exposed metrics
|
|
|
|
histogramOptions.Buckets = nil
|
|
|
|
}
|
2023-10-03 13:23:55 -05:00
|
|
|
}
|
|
|
|
|
2023-08-16 08:05:19 -05:00
|
|
|
httpRequestDurationHistogram := prometheus.NewHistogramVec(
|
2023-10-03 13:23:55 -05:00
|
|
|
histogramOptions,
|
2023-08-16 08:05:19 -05:00
|
|
|
histogramLabels,
|
2020-10-20 02:44:38 -05:00
|
|
|
)
|
|
|
|
|
2023-08-16 08:05:19 -05:00
|
|
|
promRegister.MustRegister(httpRequestsInFlight, httpRequestDurationHistogram)
|
2022-06-10 03:56:55 -05:00
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
rw := web.Rw(w, r)
|
|
|
|
now := time.Now()
|
|
|
|
httpRequestsInFlight.Inc()
|
|
|
|
defer httpRequestsInFlight.Dec()
|
|
|
|
next.ServeHTTP(w, r)
|
2022-02-02 11:48:46 -06:00
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
status := rw.Status()
|
|
|
|
code := sanitizeCode(status)
|
2022-02-02 11:48:46 -06:00
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
handler := "unknown"
|
|
|
|
// TODO: do not depend on web.Context from the future
|
2023-03-31 08:38:09 -05:00
|
|
|
if routeOperation, exists := RouteOperationName(web.FromContext(r.Context()).Req); exists {
|
2022-08-09 07:58:50 -05:00
|
|
|
handler = routeOperation
|
2022-06-14 00:58:20 -05:00
|
|
|
} else {
|
2022-08-09 07:58:50 -05:00
|
|
|
// if grafana does not recognize the handler and returns 404 we should register it as `notfound`
|
|
|
|
if status == http.StatusNotFound {
|
|
|
|
handler = "notfound"
|
|
|
|
} else {
|
|
|
|
// log requests where we could not identify handler so we can register them.
|
2023-11-14 14:50:27 -06:00
|
|
|
if features.IsEnabled(r.Context(), featuremgmt.FlagLogRequestsInstrumentedAsUnknown) {
|
2022-08-09 07:58:50 -05:00
|
|
|
log.Warn("request instrumented as unknown", "path", r.URL.Path, "status_code", status)
|
|
|
|
}
|
2022-06-14 00:58:20 -05:00
|
|
|
}
|
2022-06-10 03:56:55 -05:00
|
|
|
}
|
2017-09-06 15:24:10 -05:00
|
|
|
|
2023-08-16 08:05:19 -05:00
|
|
|
labelValues := []string{handler, code, r.Method}
|
2023-09-11 05:13:13 -05:00
|
|
|
rmd := requestmeta.GetRequestMetaData(r.Context())
|
|
|
|
|
2024-02-06 02:29:41 -06:00
|
|
|
labelValues = append(labelValues, string(rmd.StatusSource), string(rmd.SLOGroup))
|
2023-09-11 05:13:13 -05:00
|
|
|
|
2023-08-16 08:05:19 -05:00
|
|
|
if cfg.MetricsIncludeTeamLabel {
|
|
|
|
labelValues = append(labelValues, rmd.Team)
|
|
|
|
}
|
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
// avoiding the sanitize functions for in the new instrumentation
|
|
|
|
// since they dont make much sense. We should remove them later.
|
|
|
|
histogram := httpRequestDurationHistogram.
|
2023-08-16 08:05:19 -05:00
|
|
|
WithLabelValues(labelValues...)
|
|
|
|
|
2023-10-03 13:23:55 -05:00
|
|
|
elapsedTime := time.Since(now).Seconds()
|
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
if traceID := tracing.TraceIDFromContext(r.Context(), true); traceID != "" {
|
|
|
|
// Need to type-convert the Observer to an
|
|
|
|
// ExemplarObserver. This will always work for a
|
|
|
|
// HistogramVec.
|
|
|
|
histogram.(prometheus.ExemplarObserver).ObserveWithExemplar(
|
2023-10-03 13:23:55 -05:00
|
|
|
elapsedTime, prometheus.Labels{"traceID": traceID},
|
2022-08-09 07:58:50 -05:00
|
|
|
)
|
2023-10-03 13:23:55 -05:00
|
|
|
} else {
|
|
|
|
histogram.Observe(elapsedTime)
|
2022-08-09 07:58:50 -05:00
|
|
|
}
|
2022-02-02 11:48:46 -06:00
|
|
|
|
2022-08-09 07:58:50 -05:00
|
|
|
switch {
|
|
|
|
case strings.HasPrefix(r.RequestURI, "/api/datasources/proxy"):
|
|
|
|
countProxyRequests(status)
|
|
|
|
case strings.HasPrefix(r.RequestURI, "/api/"):
|
|
|
|
countApiRequests(status)
|
|
|
|
default:
|
|
|
|
countPageRequests(status)
|
|
|
|
}
|
|
|
|
})
|
2016-09-09 06:28:19 -05:00
|
|
|
}
|
|
|
|
}
|
2016-09-12 06:29:31 -05:00
|
|
|
|
|
|
|
func countApiRequests(status int) {
|
|
|
|
switch status {
|
|
|
|
case 200:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiStatus.WithLabelValues("200").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
case 404:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiStatus.WithLabelValues("404").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
case 500:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiStatus.WithLabelValues("500").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
default:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MApiStatus.WithLabelValues("unknown").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func countPageRequests(status int) {
|
|
|
|
switch status {
|
|
|
|
case 200:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MPageStatus.WithLabelValues("200").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
case 404:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MPageStatus.WithLabelValues("404").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
case 500:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MPageStatus.WithLabelValues("500").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
default:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MPageStatus.WithLabelValues("unknown").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func countProxyRequests(status int) {
|
|
|
|
switch status {
|
|
|
|
case 200:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MProxyStatus.WithLabelValues("200").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
case 404:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MProxyStatus.WithLabelValues("400").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
case 500:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MProxyStatus.WithLabelValues("500").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
default:
|
2019-07-16 09:58:46 -05:00
|
|
|
metrics.MProxyStatus.WithLabelValues("unknown").Inc()
|
2016-09-12 06:29:31 -05:00
|
|
|
}
|
|
|
|
}
|
2017-09-06 15:24:10 -05:00
|
|
|
|
|
|
|
// If the wrapped http.Handler has not set a status code, i.e. the value is
|
2020-06-01 10:11:25 -05:00
|
|
|
// currently 0, sanitizeCode will return 200, for consistency with behavior in
|
2017-09-06 15:24:10 -05:00
|
|
|
// the stdlib.
|
|
|
|
func sanitizeCode(s int) string {
|
2021-05-26 05:18:54 -05:00
|
|
|
if s == 0 {
|
2017-09-06 15:24:10 -05:00
|
|
|
return "200"
|
|
|
|
}
|
2021-05-26 05:18:54 -05:00
|
|
|
return strconv.Itoa(s)
|
2017-09-06 15:24:10 -05:00
|
|
|
}
|