From ddc7ca562294e41199a71444e10f590b1787765f Mon Sep 17 00:00:00 2001 From: Lauri Tulmin Date: Wed, 12 Jul 2023 15:13:40 +0300 Subject: [PATCH] Allow overriding span name in spring web library instrumentation --- .../web/v3_1/SpringWebTelemetryBuilder.java | 27 ++++++++++++++-- .../v5_3/SpringWebMvcTelemetryBuilder.java | 31 +++++++++++++++++-- .../v6_0/SpringWebMvcTelemetryBuilder.java | 31 +++++++++++++++++-- 3 files changed, 80 insertions(+), 9 deletions(-) diff --git a/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/SpringWebTelemetryBuilder.java b/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/SpringWebTelemetryBuilder.java index 19fd6b5ac074..8cd8fc517819 100644 --- a/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/SpringWebTelemetryBuilder.java +++ b/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/SpringWebTelemetryBuilder.java @@ -10,6 +10,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientAttributesExtractorBuilder; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientExperimentalMetrics; @@ -19,6 +20,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.function.Function; +import javax.annotation.Nullable; import org.springframework.http.HttpRequest; import org.springframework.http.client.ClientHttpResponse; @@ -35,6 +38,10 @@ public final class SpringWebTelemetryBuilder { SpringWebHttpAttributesGetter.INSTANCE, new SpringWebNetAttributesGetter()); private boolean emitExperimentalHttpClientMetrics = false; + @Nullable + private Function, ? extends SpanNameExtractor> + spanNameExtractorTransformer; + SpringWebTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; } @@ -72,6 +79,15 @@ public SpringWebTelemetryBuilder setCapturedResponseHeaders(List respons return this; } + /** Sets custom {@link SpanNameExtractor} via transform function. */ + @CanIgnoreReturnValue + public SpringWebTelemetryBuilder setSpanNameExtractor( + Function, ? extends SpanNameExtractor> + spanNameExtractor) { + this.spanNameExtractorTransformer = spanNameExtractor; + return this; + } + /** * Configures the instrumentation to recognize an alternative set of HTTP request methods. * @@ -111,11 +127,16 @@ public SpringWebTelemetryBuilder setEmitExperimentalHttpClientMetrics( public SpringWebTelemetry build() { SpringWebHttpAttributesGetter httpAttributeGetter = SpringWebHttpAttributesGetter.INSTANCE; + SpanNameExtractor originalSpanNameExtractor = + HttpSpanNameExtractor.create(httpAttributeGetter); + SpanNameExtractor spanNameExtractor = originalSpanNameExtractor; + if (spanNameExtractorTransformer != null) { + spanNameExtractor = spanNameExtractorTransformer.apply(originalSpanNameExtractor); + } + InstrumenterBuilder builder = Instrumenter.builder( - openTelemetry, - INSTRUMENTATION_NAME, - HttpSpanNameExtractor.create(httpAttributeGetter)) + openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor) .setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributeGetter)) .addAttributesExtractor(httpAttributesExtractorBuilder.build()) .addAttributesExtractors(additionalExtractors) diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcTelemetryBuilder.java b/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcTelemetryBuilder.java index b4f6e19457e3..d492824b7da4 100644 --- a/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcTelemetryBuilder.java +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/SpringWebMvcTelemetryBuilder.java @@ -9,6 +9,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteHolder; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractorBuilder; @@ -18,6 +19,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.function.Function; +import javax.annotation.Nullable; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -34,6 +37,12 @@ public final class SpringWebMvcTelemetryBuilder { HttpServerAttributesExtractor.builder( SpringWebMvcHttpAttributesGetter.INSTANCE, SpringWebMvcNetAttributesGetter.INSTANCE); + @Nullable + private Function< + SpanNameExtractor, + ? extends SpanNameExtractor> + spanNameExtractorTransformer; + SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; } @@ -71,6 +80,17 @@ public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List resp return this; } + /** Sets custom {@link SpanNameExtractor} via transform function. */ + @CanIgnoreReturnValue + public SpringWebMvcTelemetryBuilder setSpanNameExtractor( + Function< + SpanNameExtractor, + ? extends SpanNameExtractor> + spanNameExtractor) { + this.spanNameExtractorTransformer = spanNameExtractor; + return this; + } + /** * Configures the instrumentation to recognize an alternative set of HTTP request methods. * @@ -98,11 +118,16 @@ public SpringWebMvcTelemetry build() { SpringWebMvcHttpAttributesGetter httpAttributesGetter = SpringWebMvcHttpAttributesGetter.INSTANCE; + SpanNameExtractor originalSpanNameExtractor = + HttpSpanNameExtractor.create(httpAttributesGetter); + SpanNameExtractor spanNameExtractor = originalSpanNameExtractor; + if (spanNameExtractorTransformer != null) { + spanNameExtractor = spanNameExtractorTransformer.apply(originalSpanNameExtractor); + } + Instrumenter instrumenter = Instrumenter.builder( - openTelemetry, - INSTRUMENTATION_NAME, - HttpSpanNameExtractor.create(httpAttributesGetter)) + openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor) .setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter)) .addAttributesExtractor(httpAttributesExtractorBuilder.build()) .addAttributesExtractors(additionalExtractors) diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/SpringWebMvcTelemetryBuilder.java b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/SpringWebMvcTelemetryBuilder.java index 83fb94e97579..f1aabe7f2bc5 100644 --- a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/SpringWebMvcTelemetryBuilder.java +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/SpringWebMvcTelemetryBuilder.java @@ -9,6 +9,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpRouteHolder; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerAttributesExtractorBuilder; @@ -20,6 +21,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.function.Function; +import javax.annotation.Nullable; /** A builder of {@link SpringWebMvcTelemetry}. */ public final class SpringWebMvcTelemetryBuilder { @@ -34,6 +37,12 @@ public final class SpringWebMvcTelemetryBuilder { HttpServerAttributesExtractor.builder( SpringWebMvcHttpAttributesGetter.INSTANCE, SpringWebMvcNetAttributesGetter.INSTANCE); + @Nullable + private Function< + SpanNameExtractor, + ? extends SpanNameExtractor> + spanNameExtractorTransformer; + SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) { this.openTelemetry = openTelemetry; } @@ -71,6 +80,17 @@ public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List resp return this; } + /** Sets custom {@link SpanNameExtractor} via transform function. */ + @CanIgnoreReturnValue + public SpringWebMvcTelemetryBuilder setSpanNameExtractor( + Function< + SpanNameExtractor, + ? extends SpanNameExtractor> + spanNameExtractor) { + this.spanNameExtractorTransformer = spanNameExtractor; + return this; + } + /** * Configures the instrumentation to recognize an alternative set of HTTP request methods. * @@ -98,11 +118,16 @@ public SpringWebMvcTelemetry build() { SpringWebMvcHttpAttributesGetter httpAttributesGetter = SpringWebMvcHttpAttributesGetter.INSTANCE; + SpanNameExtractor originalSpanNameExtractor = + HttpSpanNameExtractor.create(httpAttributesGetter); + SpanNameExtractor spanNameExtractor = originalSpanNameExtractor; + if (spanNameExtractorTransformer != null) { + spanNameExtractor = spanNameExtractorTransformer.apply(originalSpanNameExtractor); + } + Instrumenter instrumenter = Instrumenter.builder( - openTelemetry, - INSTRUMENTATION_NAME, - HttpSpanNameExtractor.create(httpAttributesGetter)) + openTelemetry, INSTRUMENTATION_NAME, spanNameExtractor) .setSpanStatusExtractor(HttpSpanStatusExtractor.create(httpAttributesGetter)) .addAttributesExtractor(httpAttributesExtractorBuilder.build()) .addAttributesExtractors(additionalExtractors)