mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-16 21:56:40 -05:00
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>
This commit is contained in:
parent
0182fb87fa
commit
669fc41e63
2 changed files with 11 additions and 4 deletions
|
@ -69,12 +69,13 @@ func TestServer_LogRequest(t *testing.T) {
|
||||||
}`, buf.String())
|
}`, buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestServer_LogRequest_WithTraceID(t *testing.T) {
|
func TestServer_LogRequest_WithTrace(t *testing.T) {
|
||||||
s := &Server{}
|
s := &Server{}
|
||||||
|
|
||||||
extra := new(ExtraLogFields)
|
extra := new(ExtraLogFields)
|
||||||
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
|
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
|
||||||
extra.Add(zap.String("traceID", "1234567890abcdef"))
|
extra.Add(zap.String("traceID", "1234567890abcdef"))
|
||||||
|
extra.Add(zap.String("spanID", "12345678"))
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
|
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
@ -93,7 +94,8 @@ func TestServer_LogRequest_WithTraceID(t *testing.T) {
|
||||||
"msg":"handled request", "level":"info", "bytes_read":0,
|
"msg":"handled request", "level":"info", "bytes_read":0,
|
||||||
"duration":"50ms", "resp_headers": {}, "size":0,
|
"duration":"50ms", "resp_headers": {}, "size":0,
|
||||||
"status":0, "user_id":"",
|
"status":0, "user_id":"",
|
||||||
"traceID":"1234567890abcdef"
|
"traceID":"1234567890abcdef",
|
||||||
|
"spanID":"12345678"
|
||||||
}`, buf.String())
|
}`, buf.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -144,12 +146,13 @@ func BenchmarkServer_LogRequest_NopLogger(b *testing.B) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkServer_LogRequest_WithTraceID(b *testing.B) {
|
func BenchmarkServer_LogRequest_WithTrace(b *testing.B) {
|
||||||
s := &Server{}
|
s := &Server{}
|
||||||
|
|
||||||
extra := new(ExtraLogFields)
|
extra := new(ExtraLogFields)
|
||||||
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
|
ctx := context.WithValue(context.Background(), ExtraLogFieldsCtxKey, extra)
|
||||||
extra.Add(zap.String("traceID", "1234567890abcdef"))
|
extra.Add(zap.String("traceID", "1234567890abcdef"))
|
||||||
|
extra.Add(zap.String("spanID", "12345678"))
|
||||||
|
|
||||||
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
|
req := httptest.NewRequest(http.MethodGet, "/", nil).WithContext(ctx)
|
||||||
rec := httptest.NewRecorder()
|
rec := httptest.NewRecorder()
|
||||||
|
|
|
@ -88,11 +88,15 @@ func (ot *openTelemetryWrapper) serveHTTP(w http.ResponseWriter, r *http.Request
|
||||||
spanCtx := trace.SpanContextFromContext(ctx)
|
spanCtx := trace.SpanContextFromContext(ctx)
|
||||||
if spanCtx.IsValid() {
|
if spanCtx.IsValid() {
|
||||||
traceID := spanCtx.TraceID().String()
|
traceID := spanCtx.TraceID().String()
|
||||||
|
spanID := spanCtx.SpanID().String()
|
||||||
// Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
|
// Add a trace_id placeholder, accessible via `{http.vars.trace_id}`.
|
||||||
caddyhttp.SetVar(ctx, "trace_id", traceID)
|
caddyhttp.SetVar(ctx, "trace_id", traceID)
|
||||||
// Add the trace id to the log fields for the request.
|
// Add a span_id placeholder, accessible via `{http.vars.span_id}`.
|
||||||
|
caddyhttp.SetVar(ctx, "span_id", spanID)
|
||||||
|
// Add the traceID and spanID to the log fields for the request.
|
||||||
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
|
if extra, ok := ctx.Value(caddyhttp.ExtraLogFieldsCtxKey).(*caddyhttp.ExtraLogFields); ok {
|
||||||
extra.Add(zap.String("traceID", traceID))
|
extra.Add(zap.String("traceID", traceID))
|
||||||
|
extra.Add(zap.String("spanID", spanID))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
next := ctx.Value(nextCallCtxKey).(*nextCall)
|
next := ctx.Value(nextCallCtxKey).(*nextCall)
|
||||||
|
|
Loading…
Reference in a new issue