Instrumentation: Add feature toggle for logging requests instrumented as unknown (#50566) (#50670)

Signed-off-by: bergquist <carl.bergquist@gmail.com>
(cherry picked from commit 2d6e69226b)
This commit is contained in:
Carl Bergquist
2022-06-13 15:19:07 +02:00
committed by GitHub
parent 4eeb6750f0
commit 001f3709ae
4 changed files with 22 additions and 6 deletions
@@ -58,4 +58,5 @@ export interface FeatureToggles {
prometheusStreamingJSONParser?: boolean;
validateDashboardsOnSave?: boolean;
prometheusWideSeries?: boolean;
logRequestsInstrumentedAsUnknown?: boolean;
}
+12 -6
View File
@@ -6,6 +6,7 @@ import (
"strings"
"time"
"github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/infra/metrics"
"github.com/grafana/grafana/pkg/infra/tracing"
"github.com/grafana/grafana/pkg/services/featuremgmt"
@@ -46,6 +47,8 @@ func init() {
// RequestMetrics is a middleware handler that instruments the request.
func RequestMetrics(features featuremgmt.FeatureToggles) web.Handler {
log := log.New("middleware.request-metrics")
return func(res http.ResponseWriter, req *http.Request, c *web.Context) {
if strings.HasPrefix(c.Req.URL.Path, "/public/") || c.Req.URL.Path == "robots.txt" || c.Req.URL.Path == "/metrics" {
c.Next()
@@ -59,15 +62,18 @@ func RequestMetrics(features featuremgmt.FeatureToggles) web.Handler {
c.Map(c.Req)
c.Next()
handler := "unknown"
if routeOperation, exists := RouteOperationNameFromContext(c.Req.Context()); exists {
handler = routeOperation
}
status := rw.Status()
code := sanitizeCode(status)
handler := "unknown"
if routeOperation, exists := RouteOperationNameFromContext(c.Req.Context()); exists {
handler = routeOperation
} else {
if features.IsEnabled(featuremgmt.FlagLogRequestsInstrumentedAsUnknown) {
log.Warn("request instrumented as unknown", "path", c.Req.URL.Path, "status_code", status)
}
}
// avoiding the sanitize functions for in the new instrumentation
// since they dont make much sense. We should remove them later.
histogram := httpRequestDurationHistogram.
+5
View File
@@ -237,5 +237,10 @@ var (
Description: "Enable wide series responses in the Prometheus datasource",
State: FeatureStateAlpha,
},
{
Name: "logRequestsInstrumentedAsUnknown",
Description: "Logs the path for requests that are instrumented as unknown",
State: FeatureStateAlpha,
},
}
)
+4
View File
@@ -174,4 +174,8 @@ const (
// FlagPrometheusWideSeries
// Enable wide series responses in the Prometheus datasource
FlagPrometheusWideSeries = "prometheusWideSeries"
// FlagLogRequestsInstrumentedAsUnknown
// Logs the path for requests that are instrumented as unknown
FlagLogRequestsInstrumentedAsUnknown = "logRequestsInstrumentedAsUnknown"
)