Frontend service: Improve logging to include hostname/traceid (#112889)

improve frontend service logging to include hostname/traceid
This commit is contained in:
Ashley Harrison
2025-10-24 13:55:44 +01:00
committed by GitHub
parent 83ec5e6789
commit 466f1b8271
2 changed files with 13 additions and 4 deletions
+11 -2
View File
@@ -23,7 +23,7 @@ func (s *frontendService) contextMiddleware() web.Middleware {
ctx := r.Context()
span := trace.SpanFromContext(ctx)
ctx = setRequestContext(ctx)
ctx = setRequestContext(ctx, w, r)
// Preserve the original span so the setRequestContext span doesn't get propagated as a parent of the rest of the request
ctx = trace.ContextWithSpan(ctx, span)
@@ -33,7 +33,7 @@ func (s *frontendService) contextMiddleware() web.Middleware {
}
}
func setRequestContext(ctx context.Context) context.Context {
func setRequestContext(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context {
ctx, span := tracing.Start(ctx, "setRequestContext")
defer span.End()
@@ -50,9 +50,18 @@ func setRequestContext(ctx context.Context) context.Context {
// This modifies both r and reqContext.Req since they point to the same value
*reqContext.Req = *reqContext.Req.WithContext(ctx)
// add traceID to logger context
traceID := tracing.TraceIDFromContext(ctx, false)
if traceID != "" {
reqContext.Logger = reqContext.Logger.New("traceID", traceID)
// set trace ID in response headers as well
w.Header().Set("Trace-ID", traceID)
}
// add hostname to logger context
hostname := r.Host
if hostname != "" {
reqContext.Logger = reqContext.Logger.New("hostname", hostname)
}
return ctx
+2 -2
View File
@@ -133,10 +133,10 @@ func (s *frontendService) addMiddlewares(m *web.Mux) {
loggermiddleware := loggermw.Provide(s.cfg, s.features)
m.Use(requestmeta.SetupRequestMetadata())
m.UseMiddleware(s.contextMiddleware())
m.Use(middleware.RequestTracing(s.tracer, middleware.TraceAllPaths))
m.Use(middleware.RequestMetrics(s.features, s.cfg, s.promRegister))
m.UseMiddleware(s.contextMiddleware())
m.UseMiddleware(loggermiddleware.Middleware())
m.UseMiddleware(middleware.Recovery(s.cfg, s.license))