Skip to content

Commit

Permalink
Adding more tags for panic metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhraja committed Feb 27, 2024
1 parent 95dc0b2 commit 1e556c2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config/production.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 11 additions & 8 deletions runtime/router.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Uber Technologies, Inc.
// Copyright (c) 2024 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -138,7 +138,6 @@ type httpRouter struct {
httpRouter *zrouter.Router
notFoundEndpoint *RouterEndpoint
methodNotAllowedEndpoint *RouterEndpoint
panicCount tally.Counter
routeMap map[string]*RouterEndpoint

requestUUIDHeaderKey string
Expand All @@ -165,9 +164,8 @@ func NewHTTPRouter(gateway *Gateway) HTTPRouter {
gateway.ContextExtractor, deps,
methodNotAllowed, methodNotAllowed, nil,
),
gateway: gateway,
panicCount: gateway.RootScope.Counter("runtime.router.panic"),
routeMap: make(map[string]*RouterEndpoint),
gateway: gateway,
routeMap: make(map[string]*RouterEndpoint),

requestUUIDHeaderKey: gateway.requestUUIDHeaderKey,
}
Expand Down Expand Up @@ -228,8 +226,14 @@ func (router *httpRouter) handlePanic(
}
logger := router.gateway.ContextLogger
logger.Error(r.Context(), "A http request handler paniced", zap.Error(err), zap.Int(logFieldResponseStatusCode, http.StatusInternalServerError))
router.panicCount.Inc(1)

m := router.gateway.ContextExtractor.ExtractScopeTags(r.Context())
endpointId := m[scopeTagEndpoint]
handlerId := m[scopeTagHandler]
scopeTags := map[string]string{
scopeTagEndpoint: endpointId,
scopeTagHandler: handlerId,
}
router.gateway.RootScope.Tagged(scopeTags).Counter("runtime.router.panic")
http.Error(w,
http.StatusText(http.StatusInternalServerError),
http.StatusInternalServerError,
Expand All @@ -245,7 +249,6 @@ func (router *httpRouter) handleNotFound(
scopeTagHandler: router.notFoundEndpoint.HandlerName,
scopeTagProtocol: scopeTagHTTP,
}

ctx := r.Context()
ctx = WithScopeTagsDefault(ctx, scopeTags, router.gateway.RootScope)
r = r.WithContext(ctx)
Expand Down
7 changes: 6 additions & 1 deletion runtime/router_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2023 Uber Technologies, Inc.
// Copyright (c) 2024 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
Expand Down Expand Up @@ -55,6 +55,11 @@ func (s *routerSuite) SetupTest() {
},
)
s.router = NewHTTPRouter(s.gw).(*httpRouter)
extractors = &ContextExtractors{
ScopeTagsExtractors: []ContextScopeTagsExtractor{scopeExtractor},
LogFieldsExtractors: []ContextLogFieldsExtractor{logFieldsExtractors},
}
s.router.gateway.ContextExtractor = extractors
}

func (s *routerSuite) TestRouter() {
Expand Down

0 comments on commit 1e556c2

Please sign in to comment.