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 2bf04b1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 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.

17 changes: 10 additions & 7 deletions runtime/router.go
Original file line number Diff line number Diff line change
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
5 changes: 5 additions & 0 deletions runtime/router_test.go
Original file line number Diff line number Diff line change
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 2bf04b1

Please sign in to comment.