Skip to content

Commit 669fc41

Browse files
authored
tracing: Add spanID field to access logs and http.vars.span_id placeholder (#6646)
* logging: Add spanID field to access logs when tracing is enabled Signed-off-by: YifanYang6 <[email protected]> * tracing: add `http.vars.span_id` placeholder when tracing is enabled Signed-off-by: YifanYang6 <[email protected]> --------- Signed-off-by: YifanYang6 <[email protected]>
1 parent 0182fb8 commit 669fc41

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

modules/caddyhttp/server_test.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ func TestServer_LogRequest(t *testing.T) {
6969
}`, buf.String())
7070
}
7171

72-
func TestServer_LogRequest_WithTraceID(t *testing.T) {
72+
func TestServer_LogRequest_WithTrace(t *testing.T) {
7373
s := &Server{}
7474

7575
extra := new(ExtraLogFields)
7676
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
7777
extra.Add(zap.String("traceID", "1234567890abcdef"))
78+
extra.Add(zap.String("spanID", "12345678"))
7879

7980
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
8081
rec := httptest.NewRecorder()
@@ -93,7 +94,8 @@ func TestServer_LogRequest_WithTraceID(t *testing.T) {
9394
"msg":"handled request", "level":"info", "bytes_read":0,
9495
"duration":"50ms", "resp_headers": {}, "size":0,
9596
"status":0, "user_id":"",
96-
"traceID":"1234567890abcdef"
97+
"traceID":"1234567890abcdef",
98+
"spanID":"12345678"
9799
}`, buf.String())
98100
}
99101

@@ -144,12 +146,13 @@ func BenchmarkServer_LogRequest_NopLogger(b *testing.B) {
144146
}
145147
}
146148

147-
func BenchmarkServer_LogRequest_WithTraceID(b *testing.B) {
149+
func BenchmarkServer_LogRequest_WithTrace(b *testing.B) {
148150
s := &Server{}
149151

150152
extra := new(ExtraLogFields)
151153
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
152154
extra.Add(zap.String("traceID", "1234567890abcdef"))
155+
extra.Add(zap.String("spanID", "12345678"))
153156

154157
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
155158
rec := httptest.NewRecorder()

modules/caddyhttp/tracing/tracer.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,15 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
8888
spanCtx := trace.SpanContextFromContext(ctx)
8989
if spanCtx.IsValid() {
9090
traceID := spanCtx.TraceID().String()
91+
spanID := spanCtx.SpanID().String()
9192
// Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
9293
caddyhttp.SetVar(ctx, "trace_id", traceID)
93-
// Add the trace id to the log fields for the request.
94+
// Add a span_id placeholder, accessible via `{http.vars.span_id}`.
95+
caddyhttp.SetVar(ctx, "span_id", spanID)
96+
// Add the traceID and spanID to the log fields for the request.
9497
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
9598
extra.Add(zap.String("traceID", traceID))
99+
extra.Add(zap.String("spanID", spanID))
96100
}
97101
}
98102
next := ctx.Value(nextCallCtxKey).(*nextCall)

0 commit comments

Comments
 (0)