From c73f16550a193727615dae380e497e9225b02a34 Mon Sep 17 00:00:00 2001 From: Nico Pazos <32206519+npazosmendez@users.noreply.github.com> Date: Mon, 10 Jun 2024 07:26:46 -0300 Subject: [PATCH] logging middleware: log `trace_id` only if sampled (#529) * logging middleware: log `trace_id` only if sampled * if not sampled, log it as `trace_id_unsampled` * update changelog --- CHANGELOG.md | 1 + middleware/logging.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d65882366..3b05bd7cb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -211,6 +211,7 @@ * [ENHANCEMENT] Expose `InstancesWithTokensCount` and `InstancesWithTokensInZoneCount` in `ring.ReadRing` interface. #516 * [ENHANCEMENT] Middleware: determine route name in a single place, and add `middleware.ExtractRouteName()` method to allow consuming applications to retrieve the route name. #527 * [ENHANCEMENT] SpanProfiler: do less work on unsampled traces. #528 +* [ENHANCEMENT] Log Middleware: if the trace is not sampled, log its ID as `trace_id_unsampled` instead of `trace_id`. #529 * [BUGFIX] spanlogger: Support multiple tenant IDs. #59 * [BUGFIX] Memberlist: fixed corrupted packets when sending compound messages with more than 255 messages or messages bigger than 64KB. #85 * [BUGFIX] Ring: `ring_member_ownership_percent` and `ring_tokens_owned` metrics are not updated on scale down. #109 diff --git a/middleware/logging.go b/middleware/logging.go index fe00d3a82..c2306292b 100644 --- a/middleware/logging.go +++ b/middleware/logging.go @@ -56,9 +56,11 @@ func NewLogMiddleware(log log.Logger, logRequestHeaders bool, logRequestAtInfoLe // logWithRequest information from the request and context as fields. func (l Log) logWithRequest(r *http.Request) log.Logger { localLog := l.Log - traceID, ok := tracing.ExtractTraceID(r.Context()) + traceID, ok := tracing.ExtractSampledTraceID(r.Context()) if ok { localLog = log.With(localLog, "trace_id", traceID) + } else if traceID != "" { + localLog = log.With(localLog, "trace_id_unsampled", traceID) } if l.SourceIPs != nil {