Skip to content

Commit

Permalink
fix: http metric middleware improvements (#3284)
Browse files Browse the repository at this point in the history
* fix: set route as "/" when it's empty

* fix: use attributeSet to add attributes to metric
  • Loading branch information
mathnogueira committed Oct 21, 2023
1 parent 4e51f27 commit 6dea0e3
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion server/http/middleware/metrics.go
Expand Up @@ -23,6 +23,9 @@ func (m *httpMetricMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)

route := mux.CurrentRoute(r)
pathTemplate, _ := route.GetPathTemplate()
if pathTemplate == "" {
pathTemplate = "/"
}

metricAttributes := []attribute.KeyValue{
attribute.String(semconv.AttributeHTTPRoute, pathTemplate),
Expand All @@ -34,7 +37,9 @@ func (m *httpMetricMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request)
metricAttributes = append(metricAttributes, attribute.String("tracetest.tenant_id", tenantID))
}

m.requestDurationHistogram.Record(r.Context(), duration.Milliseconds(), metric.WithAttributes(metricAttributes...))
m.requestDurationHistogram.Record(r.Context(), duration.Milliseconds(), metric.WithAttributeSet(
attribute.NewSet(metricAttributes...),
))
}

var _ http.Handler = &httpMetricMiddleware{}
Expand Down

0 comments on commit 6dea0e3

Please sign in to comment.