Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskorn committed May 28, 2024
1 parent 1b878b2 commit 3996871
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion middleware/http_tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func HTTPGRPCTracingInterceptor(router *mux.Router) grpc.UnaryServerInterceptor
}

func httpOperationNameFunc(r *http.Request) string {
routeName := ExtractRouteName(r)
routeName := ExtractRouteName(r.Context())
return getOperationName(routeName, r)
}

Expand Down
2 changes: 1 addition & 1 deletion middleware/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (i Instrument) Wrap(next http.Handler) http.Handler {
// We do all this as we do not wish to emit high cardinality labels to
// prometheus.
func (i Instrument) getRouteName(r *http.Request) string {
route := ExtractRouteName(r)
route := ExtractRouteName(r.Context())
if route == "" {
route = "other"
}
Expand Down
8 changes: 5 additions & 3 deletions middleware/route_injector.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ func WithRouteName(r *http.Request, routeName string) *http.Request {
return r.WithContext(ctx)
}

// ExtractRouteName returns the route name associated with this request.
// ExtractRouteName returns the route name associated with this request that was previously injected by the
// RouteInjector middleware or WithRouteName.
//
// This is the same route name used for trace and metric names, and is already suitable for use as a Prometheus label
// value.
func ExtractRouteName(r *http.Request) string {
routeName, ok := r.Context().Value(contextKeyRouteName).(string)
func ExtractRouteName(ctx context.Context) string {
routeName, ok := ctx.Value(contextKeyRouteName).(string)
if !ok {
return ""
}
Expand Down
2 changes: 1 addition & 1 deletion middleware/route_injector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRouteInjector(t *testing.T) {
actualRouteName := ""

handler := func(_ http.ResponseWriter, r *http.Request) {
actualRouteName = ExtractRouteName(r)
actualRouteName = ExtractRouteName(r.Context())
}

router := mux.NewRouter()
Expand Down

0 comments on commit 3996871

Please sign in to comment.