Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 12, 07:00 UTC
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 <yifanyang6@link.cuhk.edu.cn> * tracing: add `http.vars.span_id` placeholder when tracing is enabled Signed-off-by: YifanYang6 <yifanyang6@link.cuhk.edu.cn> --------- Signed-off-by: YifanYang6 <yifanyang6@link.cuhk.edu.cn>
Yifan Yang committed
Oct 22, 2024 at 01:06 UTC
669fc41e6321115df554d5f7d8eb55fdfbdaab18
2 files changed
+11
-4
modules/caddyhttp/server_test.go
+6
-3
@@ -69,12 +69,13 @@ func TestServer_LogRequest(t *testing.T) {
69
}`, buf.String())
70
}
71
72
-func TestServer_LogRequest_WithTraceID(t *testing.T) {
72
+func TestServer_LogRequest_WithTrace(t *testing.T) {
73
s := &Server{}
74
75
extra := new(ExtraLogFields)
76
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
77
extra.Add(zap.String("traceID", "1234567890abcdef"))
78
+ extra.Add(zap.String("spanID", "12345678"))
79
80
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
81
rec := httptest.NewRecorder()
@@ -93,7 +94,8 @@ func TestServer_LogRequest_WithTraceID(t *testing.T) {
94
"msg":"handled request", "level":"info", "bytes_read":0,
95
"duration":"50ms", "resp_headers": {}, "size":0,
96
"status":0, "user_id":"",
96
- "traceID":"1234567890abcdef"
97
+ "traceID":"1234567890abcdef",
98
+ "spanID":"12345678"
99
}`, buf.String())
100
}
101
@@ -144,12 +146,13 @@ func BenchmarkServer_LogRequest_NopLogger(b *testing.B) {
146
}
147
}
148
147
-func BenchmarkServer_LogRequest_WithTraceID(b *testing.B) {
149
+func BenchmarkServer_LogRequest_WithTrace(b *testing.B) {
150
s := &Server{}
151
152
extra := new(ExtraLogFields)
153
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
154
extra.Add(zap.String("traceID", "1234567890abcdef"))
155
+ extra.Add(zap.String("spanID", "12345678"))
156
157
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
158
rec := httptest.NewRecorder()
modules/caddyhttp/tracing/tracer.go
+5
-1
@@ -88,11 +88,15 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
88
spanCtx := trace.SpanContextFromContext(ctx)
89
if spanCtx.IsValid() {
90
traceID := spanCtx.TraceID().String()
91
+ spanID := spanCtx.SpanID().String()
92
// Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
93
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.
97
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
98
extra.Add(zap.String("traceID", traceID))
99
+ extra.Add(zap.String("spanID", spanID))
100
}
101
}
102
next := ctx.Value(nextCallCtxKey).(*nextCall)