Better span names for spring-webflux
MODIFY
instrumentation/spring-webflux-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/springwebflux/server/DispatcherHandlerAdvice.java
instrumentation/spring-webflux-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/springwebflux/server/DispatcherHandlerAdvice.java
@@ -17,7 +17,6 @@ package io.opentelemetry.auto.instrumentation.springwebflux.server;
import static io.opentelemetry.auto.instrumentation.springwebflux.server.SpringWebfluxHttpServerDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.springwebflux.server.SpringWebfluxHttpServerDecorator.TRACER;
-import static io.opentelemetry.trace.Span.Kind.SERVER;
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
import io.opentelemetry.auto.instrumentation.reactor.core.ReactorCoreAdviceUtils;
@@ -36,16 +35,16 @@ public class DispatcherHandlerAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static SpanWithScope methodEnter(@Advice.Argument(0) final ServerWebExchange exchange) {
+ final Span parentSpan = TRACER.getCurrentSpan();
+ if (!parentSpan.getContext().isValid()) {
+ return null;
+ }
// Unfortunately Netty EventLoop is not instrumented well enough to attribute all work to the
// right things so we have to store span in request itself. We also store parent (netty's) span
// so we could update resource name.
- final Span parentSpan = TRACER.getCurrentSpan();
- if (parentSpan.getContext().isValid()) {
exchange.getAttributes().put(AdviceUtils.PARENT_SPAN_ATTRIBUTE, parentSpan);
- }
- final Span span =
- TRACER.spanBuilder("DispatcherHandler.handle").setSpanKind(SERVER).startSpan();
+ final Span span = TRACER.spanBuilder("DispatcherHandler.handle").startSpan();
DECORATE.afterStart(span);
exchange.getAttributes().put(AdviceUtils.SPAN_ATTRIBUTE, span);
@@ -58,6 +57,9 @@ public class DispatcherHandlerAdvice {
@Advice.Thrown final Throwable throwable,
@Advice.Argument(0) final ServerWebExchange exchange,
@Advice.Return(readOnly = false) Mono<Object> mono) {
+ if (spanWithScope == null) {
+ return;
+ }
if (throwable == null && mono != null) {
final Function<? super Mono<Object>, ? extends Publisher<Object>> function =
ReactorCoreAdviceUtils.finishSpanNextOrError();

MODIFY
instrumentation/spring-webflux-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/springwebflux/server/HandlerAdapterAdvice.java
instrumentation/spring-webflux-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/springwebflux/server/HandlerAdapterAdvice.java
@@ -18,7 +18,6 @@ package io.opentelemetry.auto.instrumentation.springwebflux.server;
import static io.opentelemetry.auto.instrumentation.springwebflux.server.SpringWebfluxHttpServerDecorator.DECORATE;
import static io.opentelemetry.auto.instrumentation.springwebflux.server.SpringWebfluxHttpServerDecorator.TRACER;
-import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.auto.instrumentation.api.SpanWithScope;
import io.opentelemetry.trace.Span;
import net.bytebuddy.asm.Advice;
@@ -60,10 +59,9 @@ public class HandlerAdapterAdvice {
final PathPattern bestPattern =
exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (parentSpan != null && bestPattern != null) {
- String resourceName =
+ final String resourceName =
exchange.getRequest().getMethodValue() + " " + bestPattern.getPatternString();
parentSpan.updateName(resourceName);
- parentSpan.setAttribute(MoreTags.RESOURCE_NAME, resourceName);
}
return spanWithScope;

MODIFY
instrumentation/spring-webflux-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/springwebflux/server/RouteOnSuccessOrError.java
instrumentation/spring-webflux-5.0/src/main/java8/io/opentelemetry/auto/instrumentation/springwebflux/server/RouteOnSuccessOrError.java
*/
package io.opentelemetry.auto.instrumentation.springwebflux.server;
-import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.trace.Span;
import java.util.function.BiConsumer;
import java.util.regex.Pattern;
@@ -49,9 +48,8 @@ public class RouteOnSuccessOrError implements BiConsumer<HandlerFunction<?>, Thr
final Span parentSpan =
(Span) serverRequest.attributes().get(AdviceUtils.PARENT_SPAN_ATTRIBUTE);
if (parentSpan != null) {
- String resourceName = parseResourceName(predicateString);
+ final String resourceName = parseResourceName(predicateString);
parentSpan.updateName(resourceName);
- parentSpan.setAttribute(MoreTags.RESOURCE_NAME, resourceName);
}
}
}

MODIFY
instrumentation/spring-webflux-5.0/src/test/groovy/SpringWebfluxTest.groovy
instrumentation/spring-webflux-5.0/src/test/groovy/SpringWebfluxTest.groovy
@@ -32,6 +32,7 @@ import server.FooModel
import server.SpringWebFluxTestApplication
import server.TestController
+import static io.opentelemetry.trace.Span.Kind.INTERNAL
import static io.opentelemetry.trace.Span.Kind.SERVER
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = [SpringWebFluxTestApplication, ForceNettyAutoConfiguration])
@@ -71,7 +72,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET $urlPathWithVariables"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -89,7 +89,7 @@ class SpringWebfluxTest extends AgentTestRunner {
// Annotation API
operationName TestController.getSimpleName() + "." + annotatedMethod
}
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
tags {
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
@@ -140,7 +140,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET $urlPathWithVariables"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -158,7 +157,7 @@ class SpringWebfluxTest extends AgentTestRunner {
// Annotation API
operationName TestController.getSimpleName() + "." + annotatedMethod
}
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
tags {
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
@@ -213,7 +212,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET /**"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -225,7 +223,7 @@ class SpringWebfluxTest extends AgentTestRunner {
}
span(1) {
operationName "ResourceWebHandler.handle"
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
errored true
tags {
@@ -259,7 +257,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "POST /echo"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -271,7 +268,7 @@ class SpringWebfluxTest extends AgentTestRunner {
}
span(1) {
operationName EchoHandlerFunction.getSimpleName() + ".handle"
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
tags {
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
@@ -310,7 +307,6 @@ class SpringWebfluxTest extends AgentTestRunner {
errored true
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET $urlPathWithVariables"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -328,7 +324,7 @@ class SpringWebfluxTest extends AgentTestRunner {
// Annotation API
operationName TestController.getSimpleName() + "." + annotatedMethod
}
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
errored true
tags {
@@ -378,7 +374,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET /double-greet-redirect"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -390,7 +385,7 @@ class SpringWebfluxTest extends AgentTestRunner {
}
span(1) {
operationName "RedirectComponent.lambda"
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
tags {
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
@@ -409,7 +404,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET /double-greet"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -421,7 +415,7 @@ class SpringWebfluxTest extends AgentTestRunner {
}
span(1) {
operationNameContains(SpringWebFluxTestApplication.getSimpleName() + "\$", ".handle")
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
tags {
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
@@ -455,7 +449,6 @@ class SpringWebfluxTest extends AgentTestRunner {
spanKind SERVER
parent()
tags {
- "$MoreTags.RESOURCE_NAME" "GET $urlPathWithVariables"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" "netty"
"$MoreTags.NET_PEER_IP" "127.0.0.1"
@@ -473,7 +466,7 @@ class SpringWebfluxTest extends AgentTestRunner {
// Annotation API
operationName TestController.getSimpleName() + "." + annotatedMethod
}
- spanKind SERVER
+ spanKind INTERNAL
childOf span(0)
tags {
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER

Better span names for spring-webflux (#246)