Fix route handling when local root span wasn't created by instrumentation api (#13588)

This commit is contained in:
Lauri Tulmin 2025-03-27 08:32:34 +02:00 committed by GitHub
parent f048a91770
commit 389b153990
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 10 additions and 8 deletions

View File

@ -190,7 +190,8 @@ public class Instrumenter<REQUEST, RESPONSE> {
context = contextCustomizer.onStart(context, request, attributes);
}
boolean localRoot = LocalRootSpan.isLocalRoot(context);
boolean localRoot = LocalRootSpan.isLocalRoot(parentContext);
boolean hasLocalRoot = LocalRootSpan.fromContextOrNull(context) != null;
spanBuilder.setAllAttributes(attributes);
Span span = spanBuilder.setParent(context).startSpan();
@ -216,9 +217,9 @@ public class Instrumenter<REQUEST, RESPONSE> {
if (localRoot) {
context = LocalRootSpan.store(context, span);
if (spanKind == SpanKind.SERVER) {
HttpRouteState.updateSpan(context, span);
}
}
if (!hasLocalRoot && spanKind == SpanKind.SERVER) {
HttpRouteState.updateSpan(context, span);
}
return spanSuppressor.storeInContext(context, spanKind, span);

View File

@ -27,7 +27,7 @@ public final class HttpRouteState implements ImplicitContextKeyed {
public static void updateSpan(Context context, Span span) {
HttpRouteState state = fromContextOrNull(context);
if (state != null) {
if (state != null && state.span == null) {
state.span = span;
}
}

View File

@ -44,7 +44,7 @@ class HttpServerRouteTest {
}
@Test
void noLocalRootSpan() {
void nonInstrumenerParentLocalRootSpan() {
Span parentSpan =
testing.getOpenTelemetry().getTracer("test").spanBuilder("parent").startSpan();
parentSpan.end();
@ -56,10 +56,11 @@ class HttpServerRouteTest {
instrumenter.end(context, "test", null, null);
assertNull(HttpServerRoute.get(context));
assertEquals("/get/:id", HttpServerRoute.get(context));
assertThat(testing.getSpans())
.satisfiesExactly(
span -> assertThat(span).hasName("parent"), span -> assertThat(span).hasName("test"));
span -> assertThat(span).hasName("parent"),
span -> assertThat(span).hasName("HTTP /get/:id"));
}
@Test