Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 13, 06:45 UTC
tracing: Support placeholders in span name (#5329)
Fixes https://github.com/caddyserver/caddy/issues/5171
David Frickert committed
Jan 25, 2023 at 07:26 UTC
962310204f088a74719a48fa2dc76e76934dfa09
2 files changed
+23
-4
modules/caddyhttp/tracing/module_test.go
+11
-3
@@ -98,7 +98,7 @@ func TestTracing_ServeHTTP_Propagation_Without_Initial_Headers(t *testing.T) {
98
SpanName: "mySpan",
99
}
100
101
- req := httptest.NewRequest("GET", "https://example.com/foo", nil)
101
+ req := createRequestWithContext("GET", "https://example.com/foo")
102
w := httptest.NewRecorder()
103
104
var handler caddyhttp.HandlerFunc = func(writer http.ResponseWriter, request *http.Request) error {
@@ -128,7 +128,7 @@ func TestTracing_ServeHTTP_Propagation_With_Initial_Headers(t *testing.T) {
128
SpanName: "mySpan",
129
}
130
131
- req := httptest.NewRequest("GET", "https://example.com/foo", nil)
131
+ req := createRequestWithContext("GET", "https://example.com/foo")
132
req.Header.Set("traceparent", "00-11111111111111111111111111111111-1111111111111111-01")
133
w := httptest.NewRecorder()
134
@@ -159,7 +159,7 @@ func TestTracing_ServeHTTP_Next_Error(t *testing.T) {
159
SpanName: "mySpan",
160
}
161
162
- req := httptest.NewRequest("GET", "https://example.com/foo", nil)
162
+ req := createRequestWithContext("GET", "https://example.com/foo")
163
w := httptest.NewRecorder()
164
165
expectErr := errors.New("test error")
@@ -180,3 +180,11 @@ func TestTracing_ServeHTTP_Next_Error(t *testing.T) {
180
t.Errorf("expected error, got: %v", err)
181
}
182
}
183
+
184
+func createRequestWithContext(method string, url string) *http.Request {
185
+ r, _ := http.NewRequest(method, url, nil)
186
+ repl := caddy.NewReplacer()
187
+ ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
188
+ r = r.WithContext(ctx)
189
+ return r
190
+}
modules/caddyhttp/tracing/tracer.go
+12
-1
@@ -69,7 +69,13 @@ func newOpenTelemetryWrapper(
69
sdktrace.WithResource(res),
70
)
71
72
- ot.handler = otelhttp.NewHandler(http.HandlerFunc(ot.serveHTTP), ot.spanName, otelhttp.WithTracerProvider(tracerProvider), otelhttp.WithPropagators(ot.propagators))
72
+ ot.handler = otelhttp.NewHandler(http.HandlerFunc(ot.serveHTTP),
73
+ ot.spanName,
74
+ otelhttp.WithTracerProvider(tracerProvider),
75
+ otelhttp.WithPropagators(ot.propagators),
76
+ otelhttp.WithSpanNameFormatter(ot.spanNameFormatter),
77
+ )
78
+
79
return ot, nil
80
}
81
@@ -106,3 +112,8 @@ func (ot *openTelemetryWrapper) newResource(
112
semconv.WebEngineVersionKey.String(webEngineVersion),
113
))
114
}
115
+
116
+// spanNameFormatter performs the replacement of placeholders in the span name
117
+func (ot *openTelemetryWrapper) spanNameFormatter(operation string, r *http.Request) string {
118
+ return r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer).ReplaceAll(operation, "")
119
+}