Skip to content

Commit

Permalink
Merge pull request #146 from Interhyp/RELTEC-11181-more-ctx-cancel-logs
Browse files Browse the repository at this point in the history
feat(RELTEC-11181): add context cancellation logging
  • Loading branch information
StephanHCB authored May 17, 2023
2 parents 9c049da + 63f0d6a commit 685ab01
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/web/middleware/cancellogger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package middleware

import (
"context"
aulogging "github.com/StephanHCB/go-autumn-logging"
"net/http"
)

func ConstructContextCancellationLoggerMiddleware(description string) func(http.Handler) http.Handler {
middleware := func(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() // next might change it so must remember it here

next.ServeHTTP(w, r)

cause := context.Cause(ctx)
if cause != nil {
aulogging.Logger.NoCtx().Warn().WithErr(cause).Printf("context '%s' is closed: %s", description, cause.Error())
}
}
return http.HandlerFunc(fn)
}
return middleware
}
10 changes: 10 additions & 0 deletions internal/web/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (s *Impl) WireUp(ctx context.Context) {
if s.Router == nil {
s.Logging.Logger().Ctx(ctx).Info().Print("creating router and setting up filter chain")
s.Router = chi.NewRouter()
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("Top"))

requestid.RequestIDHeader = apmhttp.W3CTraceparentHeader
loggermiddleware.RequestIdFieldName = "trace.id"
Expand All @@ -69,30 +70,39 @@ func (s *Impl) WireUp(ctx context.Context) {
middleware.TraceContextFetcherForResponseHeaders = middleware.RestoreOrCreateTraceContextWithoutAPM
s.Logging.Logger().NoCtx().Warn().Printf("Elastic APM not configured or disabled, skipping middleware.")
}
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("ElasticApm"))

// build a request specific logger (includes request id and some fields) and add it to the request context
s.Router.Use(loggermiddleware.AddZerologLoggerToContext)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("AddZerologLoggerToContext"))

// request logging
requestlogging.Setup()
s.Router.Use(chimiddleware.Logger)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("Logger"))

// trap panics in requests and log stack trace
s.Router.Use(middleware.PanicRecoverer)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("PanicRecoverer"))

// add request id to response, so it can be found in header
s.Router.Use(requestidinresponse.AddRequestIdHeaderToResponse)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("AddRequestIdHeaderToResponse"))

s.Router.Use(corsheader.CorsHandling)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("CorsHandling"))

requestmetrics.Setup()
s.Router.Use(requestmetrics.RecordRequestMetrics)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("RecordRequestMetrics"))

_ = jwt.Setup(s.IdentityProvider.GetKeySet(ctx), s.CustomConfiguration)
s.Router.Use(jwt.JwtValidator)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("JwtValidator"))

timeout.RequestTimeoutSeconds = s.RequestTimeoutSeconds
s.Router.Use(timeout.AddRequestTimeout)
s.Router.Use(middleware.ConstructContextCancellationLoggerMiddleware("AddRequestTimeout"))
}

s.HealthCtl.WireUp(ctx, s.Router)
Expand Down

0 comments on commit 685ab01

Please sign in to comment.