Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@

import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter;
import javax.annotation.Nullable;
import org.springframework.web.method.HandlerMethod;

public class HandlerCodeAttributesGetter implements CodeAttributesGetter<Object> {
@Nullable
@Override
public Class<?> getCodeClass(Object handler) {
return handler.getClass();
if (handler instanceof HandlerMethod) {
// Special case for requests mapped with annotations
HandlerMethod handlerMethod = (HandlerMethod) handler;
return handlerMethod.getMethod().getDeclaringClass();
} else {
return handler.getClass();
}
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
package io.opentelemetry.javaagent.instrumentation.spring.webflux.v5_0.server;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder;
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteGetter;
import io.opentelemetry.javaagent.bootstrap.internal.ExperimentalConfig;
import io.opentelemetry.javaagent.instrumentation.spring.webflux.v5_0.SpringWebfluxConfig;
import org.springframework.web.reactive.HandlerMapping;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.pattern.PathPattern;
Expand All @@ -25,13 +25,11 @@ public final class WebfluxSingletons {
Instrumenter.builder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, new WebfluxSpanNameExtractor());

if (SpringWebfluxConfig.captureExperimentalSpanAttributes()) {
builder.addAttributesExtractor(new ExperimentalAttributesExtractor());
}

INSTRUMENTER =
builder
.setEnabled(ExperimentalConfig.get().controllerTelemetryEnabled())
.addAttributesExtractor(
CodeAttributesExtractor.create(new HandlerCodeAttributesGetter()))
.buildInstrumenter();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package io.opentelemetry.javaagent.instrumentation.spring.webflux.v5_0.server;

import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.satisfies;
Expand All @@ -25,6 +24,8 @@
import static io.opentelemetry.semconv.UrlAttributes.URL_PATH;
import static io.opentelemetry.semconv.UrlAttributes.URL_SCHEME;
import static io.opentelemetry.semconv.UserAgentAttributes.USER_AGENT_ORIGINAL;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_FUNCTION;
import static io.opentelemetry.semconv.incubating.CodeIncubatingAttributes.CODE_NAMESPACE;
import static org.junit.jupiter.api.Named.named;

import io.opentelemetry.api.trace.SpanKind;
Expand Down Expand Up @@ -138,8 +139,9 @@ void basicGetTest(Parameter parameter) {
span.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
parameter.annotatedMethod == null
? val -> val.contains(INNER_HANDLER_FUNCTION_CLASS_TAG_PREFIX)
: val -> val.isEqualTo(TestController.class.getName())));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously used TestController is more useful than org.springframework.web.method.HandlerMethod that is reported after these changes

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now it reports TestController as class name and method name as handle, TestController does not have this method

Expand Down Expand Up @@ -257,8 +259,9 @@ void getAsyncResponseTest(Parameter parameter) {
span.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
parameter.annotatedMethod == null
? val -> val.contains(INNER_HANDLER_FUNCTION_CLASS_TAG_PREFIX)
: val -> val.isEqualTo(TestController.class.getName())));
Expand Down Expand Up @@ -363,8 +366,9 @@ void createSpanDuringHandlerFunctionTest(Parameter parameter) {
span.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
parameter.annotatedMethod == null
? val -> val.contains(INNER_HANDLER_FUNCTION_CLASS_TAG_PREFIX)
: val -> val.isEqualTo(TestController.class.getName())));
Expand Down Expand Up @@ -428,8 +432,9 @@ void get404Test() {
.hasStatus(StatusData.error())
.hasEventsSatisfyingExactly(SpringWebfluxTest::resource404Exception)
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
equalTo(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
"org.springframework.web.reactive.resource.ResourceWebHandler"))));
}

Expand Down Expand Up @@ -485,9 +490,8 @@ void basicPostTest() {
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
satisfies(
stringKey("spring-webflux.handler.type"),
val -> val.contains(EchoHandlerFunction.class.getName()))),
equalTo(CODE_FUNCTION, "handle"),
equalTo(CODE_NAMESPACE, "server.EchoHandlerFunction")),
span ->
span.hasName("echo").hasParent(trace.getSpan(1)).hasTotalAttributeCount(0)));
}
Expand Down Expand Up @@ -544,8 +548,9 @@ void getToBadEndpointTest(Parameter parameter) {
EXCEPTION_STACKTRACE,
val -> val.isInstanceOf(String.class))))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
parameter.annotatedMethod == null
? val -> val.contains(INNER_HANDLER_FUNCTION_CLASS_TAG_PREFIX)
: val -> val.isEqualTo(TestController.class.getName())));
Expand Down Expand Up @@ -603,8 +608,9 @@ void redirectTest() {
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
val -> val.startsWith("server.RedirectComponent$$Lambda")))),
trace ->
trace.hasSpansSatisfyingExactly(
Expand All @@ -631,8 +637,9 @@ void redirectTest() {
span.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
val -> val.contains(INNER_HANDLER_FUNCTION_CLASS_TAG_PREFIX)));
}));
}
Expand Down Expand Up @@ -688,8 +695,9 @@ void multipleGetsToDelayingRoute(Parameter parameter) {
span.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
CODE_NAMESPACE,
parameter.annotatedMethod == null
? val -> val.contains(INNER_HANDLER_FUNCTION_CLASS_TAG_PREFIX)
: val -> val.isEqualTo(TestController.class.getName())));
Expand Down Expand Up @@ -760,10 +768,11 @@ void cancelRequestTest() throws Exception {
.hasKind(SpanKind.INTERNAL)
.hasParent(trace.getSpan(0))
.hasAttributesSatisfyingExactly(
equalTo(CODE_FUNCTION, "handle"),
satisfies(
stringKey("spring-webflux.handler.type"),
value ->
value.startsWith(
CODE_NAMESPACE,
val ->
val.startsWith(
"server.SpringWebFluxTestApplication$$Lambda")))));

SpringWebFluxTestApplication.resumeSlowRequest();
Expand Down
Loading