Better span names for spring-webmvc
MODIFY
instrumentation/spring-webmvc-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springweb/DispatcherServletInstrumentation.java
instrumentation/spring-webmvc-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springweb/DispatcherServletInstrumentation.java
@@ -79,7 +79,7 @@ public final class DispatcherServletInstrumentation extends Instrumenter.Default
@Advice.OnMethodEnter(suppress = Throwable.class)
public static SpanWithScope onEnter(@Advice.Argument(0) final ModelAndView mv) {
- final Span span = TRACER.spanBuilder("response.render").startSpan();
+ final Span span = TRACER.spanBuilder(DECORATE_RENDER.spanNameOnRender(mv)).startSpan();
DECORATE_RENDER.afterStart(span);
DECORATE_RENDER.onRender(span, mv);
return new SpanWithScope(span, TRACER.withSpan(span));

MODIFY
instrumentation/spring-webmvc-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springweb/HandlerAdapterInstrumentation.java
instrumentation/spring-webmvc-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springweb/HandlerAdapterInstrumentation.java
@@ -93,9 +93,8 @@ public final class HandlerAdapterInstrumentation extends Instrumenter.Default {
// Now create a span for handler/controller execution.
- final Span span = TRACER.spanBuilder("spring.handler").startSpan();
+ final Span span = TRACER.spanBuilder(DECORATE.spanNameOnHandle(handler)).startSpan();
DECORATE.afterStart(span);
- DECORATE.onHandle(span, handler);
return new SpanWithScope(span, TRACER.withSpan(span));
}

MODIFY
instrumentation/spring-webmvc-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springweb/SpringWebHttpServerDecorator.java
instrumentation/spring-webmvc-3.1/src/main/java/io/opentelemetry/auto/instrumentation/springweb/SpringWebHttpServerDecorator.java
@@ -17,7 +17,6 @@ package io.opentelemetry.auto.instrumentation.springweb;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.HttpServerDecorator;
-import io.opentelemetry.auto.instrumentation.api.MoreTags;
import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Tracer;
import java.lang.reflect.Method;
@@ -31,6 +30,7 @@ import org.springframework.web.HttpRequestHandler;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerMapping;
import org.springframework.web.servlet.ModelAndView;
+import org.springframework.web.servlet.View;
import org.springframework.web.servlet.mvc.Controller;
@Slf4j
@@ -94,13 +94,12 @@ public class SpringWebHttpServerDecorator
if (method != null && bestMatchingPattern != null) {
final String resourceName = method + " " + bestMatchingPattern;
span.updateName(resourceName);
- span.setAttribute(MoreTags.RESOURCE_NAME, resourceName);
}
}
return span;
}
- public void onHandle(final Span span, final Object handler) {
+ public String spanNameOnHandle(final Object handler) {
final Class<?> clazz;
final String methodName;
@@ -127,15 +126,26 @@ public class SpringWebHttpServerDecorator
methodName = "<annotation>";
}
- final String resourceName = DECORATE.spanNameForClass(clazz) + "." + methodName;
- span.setAttribute(MoreTags.RESOURCE_NAME, resourceName);
+ return DECORATE.spanNameForClass(clazz) + "." + methodName;
+ }
+
+ public String spanNameOnRender(final ModelAndView mv) {
+ final String viewName = mv.getViewName();
+ if (viewName != null) {
+ return "Render " + viewName;
+ }
+ final View view = mv.getView();
+ if (view != null) {
+ return "Render " + view.getClass().getSimpleName();
+ }
+ // either viewName or view should be non-null, but just in case
+ return "Render <unknown>";
}
public Span onRender(final Span span, final ModelAndView mv) {
final String viewName = mv.getViewName();
if (viewName != null) {
span.setAttribute("view.name", viewName);
- span.setAttribute(MoreTags.RESOURCE_NAME, viewName);
}
if (mv.getView() != null) {
span.setAttribute("view.type", mv.getView().getClass().getName());

MODIFY
instrumentation/spring-webmvc-3.1/src/test/groovy/test/SpringBootBasedTest.groovy
instrumentation/spring-webmvc-3.1/src/test/groovy/test/SpringBootBasedTest.groovy
@@ -81,7 +81,7 @@ class SpringBootBasedTest extends HttpServerTest<ConfigurableApplicationContext>
@Override
void renderSpan(TraceAssert trace, int index, Object parent, String method = "GET", ServerEndpoint endpoint = SUCCESS) {
trace.span(index) {
- operationName "response.render"
+ operationName "Render RedirectView"
spanKind INTERNAL
errored false
tags {
@@ -95,12 +95,11 @@ class SpringBootBasedTest extends HttpServerTest<ConfigurableApplicationContext>
@Override
void handlerSpan(TraceAssert trace, int index, Object parent, String method = "GET", ServerEndpoint endpoint = SUCCESS) {
trace.span(index) {
- operationName "spring.handler"
+ operationName "TestController.${endpoint.name().toLowerCase()}"
spanKind INTERNAL
errored endpoint == EXCEPTION
childOf((SpanData) parent)
tags {
- "$MoreTags.RESOURCE_NAME" "TestController.${endpoint.name().toLowerCase()}"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" SpringWebHttpServerDecorator.DECORATE.getComponentName()
if (endpoint == EXCEPTION) {
@@ -123,7 +122,6 @@ class SpringBootBasedTest extends HttpServerTest<ConfigurableApplicationContext>
parent()
}
tags {
- "$MoreTags.RESOURCE_NAME" "$method ${endpoint == PATH_PARAM ? "/path/{id}/param" : endpoint.resolvePath(address).path}"
"$MoreTags.SPAN_TYPE" SpanTypes.HTTP_SERVER
"$Tags.COMPONENT" component
"$MoreTags.NET_PEER_IP" { it == null || it == "127.0.0.1" } // Optional

MODIFY
smoke-tests/springboot/src/test/groovy/io/opentelemetry/smoketest/SpringBootSmokeTest.groovy
smoke-tests/springboot/src/test/groovy/io/opentelemetry/smoketest/SpringBootSmokeTest.groovy
@@ -19,7 +19,7 @@ import okhttp3.Request
class SpringBootSmokeTest extends AbstractServerSmokeTest {
- static final HANDLER_SPAN = "LOGGED_SPAN spring.handler"
+ static final HANDLER_SPAN = "LOGGED_SPAN WebController.greeting"
static final SERVLET_SPAN = "LOGGED_SPAN GET /greeting"
@Override

Better span names for spring-webmvc (#245)