mirror of
https://github.com/caddyserver/caddy.git
synced 2024-12-26 21:53:48 +03:00
tracing: Support placeholders in span name (#5329)
Fixes https://github.com/caddyserver/caddy/issues/5171
This commit is contained in:
parent
98867ac346
commit
962310204f
2 changed files with 23 additions and 4 deletions
|
@ -98,7 +98,7 @@ func TestTracing_ServeHTTP_Propagation_Without_Initial_Headers(t *testing.T) {
|
|||
SpanName: "mySpan",
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "https://example.com/foo", nil)
|
||||
req := createRequestWithContext("GET", "https://example.com/foo")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
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) {
|
|||
SpanName: "mySpan",
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "https://example.com/foo", nil)
|
||||
req := createRequestWithContext("GET", "https://example.com/foo")
|
||||
req.Header.Set("traceparent", "00-11111111111111111111111111111111-1111111111111111-01")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
|
@ -159,7 +159,7 @@ func TestTracing_ServeHTTP_Next_Error(t *testing.T) {
|
|||
SpanName: "mySpan",
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("GET", "https://example.com/foo", nil)
|
||||
req := createRequestWithContext("GET", "https://example.com/foo")
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
expectErr := errors.New("test error")
|
||||
|
@ -180,3 +180,11 @@ func TestTracing_ServeHTTP_Next_Error(t *testing.T) {
|
|||
t.Errorf("expected error, got: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func createRequestWithContext(method string, url string) *http.Request {
|
||||
r, _ := http.NewRequest(method, url, nil)
|
||||
repl := caddy.NewReplacer()
|
||||
ctx := context.WithValue(r.Context(), caddy.ReplacerCtxKey, repl)
|
||||
r = r.WithContext(ctx)
|
||||
return r
|
||||
}
|
||||
|
|
|
@ -69,7 +69,13 @@ func newOpenTelemetryWrapper(
|
|||
sdktrace.WithResource(res),
|
||||
)
|
||||
|
||||
ot.handler = otelhttp.NewHandler(http.HandlerFunc(ot.serveHTTP), ot.spanName, otelhttp.WithTracerProvider(tracerProvider), otelhttp.WithPropagators(ot.propagators))
|
||||
ot.handler = otelhttp.NewHandler(http.HandlerFunc(ot.serveHTTP),
|
||||
ot.spanName,
|
||||
otelhttp.WithTracerProvider(tracerProvider),
|
||||
otelhttp.WithPropagators(ot.propagators),
|
||||
otelhttp.WithSpanNameFormatter(ot.spanNameFormatter),
|
||||
)
|
||||
|
||||
return ot, nil
|
||||
}
|
||||
|
||||
|
@ -106,3 +112,8 @@ func (ot *openTelemetryWrapper) newResource(
|
|||
semconv.WebEngineVersionKey.String(webEngineVersion),
|
||||
))
|
||||
}
|
||||
|
||||
// spanNameFormatter performs the replacement of placeholders in the span name
|
||||
func (ot *openTelemetryWrapper) spanNameFormatter(operation string, r *http.Request) string {
|
||||
return r.Context().Value(caddy.ReplacerCtxKey).(*caddy.Replacer).ReplaceAll(operation, "")
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue