From f1e8291bc5dbc282b06963da8db7208f1d81a3f7 Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Thu, 27 Jun 2024 09:09:18 +0200 Subject: [PATCH] remove need for reflection --- .../DefaultHttpServerInstrumenterBuilder.java | 38 ++++--------------- .../ApacheHttpClientTelemetryBuilder.java | 19 +++++----- .../ApacheHttpClient5TelemetryBuilder.java | 18 ++++----- .../web/RestClientBeanPostProcessor.java | 2 +- .../web/RestTemplateInstrumentation.java | 2 +- .../webflux/WebClientBeanPostProcessor.java | 6 ++- ...bMvc5InstrumentationAutoConfiguration.java | 3 +- ...bMvc6InstrumentationAutoConfiguration.java | 3 +- .../properties/InstrumentationConfigUtil.java | 32 +++++++++++----- .../web/v3_1/SpringWebTelemetryBuilder.java | 18 ++++----- .../v5_3/SpringWebfluxTelemetryBuilder.java | 15 ++++++++ .../v5_3/internal/SpringWebfluxUtil.java | 34 +++++++++++++++++ .../v5_3/SpringWebMvcTelemetryBuilder.java | 28 +++++++++----- .../webmvc/v5_3/internal/SpringMvcUtil.java | 26 +++++++++++++ .../v6_0/SpringWebMvcTelemetryBuilder.java | 28 +++++++++----- .../webmvc/v6_0/internal/SpringMvcUtil.java | 26 +++++++++++++ .../JavaagentHttpClientInstrumenters.java | 1 - 17 files changed, 206 insertions(+), 93 deletions(-) create mode 100644 instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/internal/SpringWebfluxUtil.java create mode 100644 instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/internal/SpringMvcUtil.java create mode 100644 instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/internal/SpringMvcUtil.java diff --git a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/builder/internal/DefaultHttpServerInstrumenterBuilder.java b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/builder/internal/DefaultHttpServerInstrumenterBuilder.java index 41a7139d47e8..22ba811cb6d4 100644 --- a/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/builder/internal/DefaultHttpServerInstrumenterBuilder.java +++ b/instrumentation-api-incubator/src/main/java/io/opentelemetry/instrumentation/api/incubator/builder/internal/DefaultHttpServerInstrumenterBuilder.java @@ -26,7 +26,6 @@ import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractor; import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanNameExtractorBuilder; import io.opentelemetry.instrumentation.api.semconv.http.HttpSpanStatusExtractor; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -212,17 +211,15 @@ public OpenTelemetry getOpenTelemetry() { } @CanIgnoreReturnValue - public static - DefaultHttpServerInstrumenterBuilder unwrapAndConfigure( - CoreCommonConfig config, Object builder) { - DefaultHttpServerInstrumenterBuilder defaultBuilder = unwrapBuilder(builder); - set(config::getKnownHttpRequestMethods, defaultBuilder::setKnownMethods); - set(config::getServerRequestHeaders, defaultBuilder::setCapturedRequestHeaders); - set(config::getServerResponseHeaders, defaultBuilder::setCapturedResponseHeaders); + public DefaultHttpServerInstrumenterBuilder configure( + CoreCommonConfig config) { + set(config::getKnownHttpRequestMethods, this::setKnownMethods); + set(config::getServerRequestHeaders, this::setCapturedRequestHeaders); + set(config::getServerResponseHeaders, this::setCapturedResponseHeaders); set( config::shouldEmitExperimentalHttpServerTelemetry, - defaultBuilder::setEmitExperimentalHttpServerMetrics); - return defaultBuilder; + this::setEmitExperimentalHttpServerMetrics); + return this; } private static void set(Supplier supplier, Consumer consumer) { @@ -231,25 +228,4 @@ private static void set(Supplier supplier, Consumer consumer) { consumer.accept(t); } } - - /** - * This method is used to access the builder field of the builder object. - * - *

This approach allows us to re-use the existing builder classes from the library modules - */ - @SuppressWarnings("unchecked") - private static - DefaultHttpServerInstrumenterBuilder unwrapBuilder(Object builder) { - if (builder instanceof DefaultHttpServerInstrumenterBuilder) { - return (DefaultHttpServerInstrumenterBuilder) builder; - } - Class builderClass = builder.getClass(); - try { - Field field = builderClass.getDeclaredField("serverBuilder"); - field.setAccessible(true); - return (DefaultHttpServerInstrumenterBuilder) field.get(builder); - } catch (Exception e) { - throw new IllegalStateException("Could not access serverBuilder field in " + builderClass, e); - } - } } diff --git a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientTelemetryBuilder.java b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientTelemetryBuilder.java index 1d0c440192cb..3526ca01436a 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientTelemetryBuilder.java +++ b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/ApacheHttpClientTelemetryBuilder.java @@ -20,11 +20,10 @@ public final class ApacheHttpClientTelemetryBuilder { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-4.3"; - private final DefaultHttpClientInstrumenterBuilder - clientBuilder; + private final DefaultHttpClientInstrumenterBuilder builder; ApacheHttpClientTelemetryBuilder(OpenTelemetry openTelemetry) { - clientBuilder = + builder = new DefaultHttpClientInstrumenterBuilder<>( INSTRUMENTATION_NAME, openTelemetry, ApacheHttpClientHttpAttributesGetter.INSTANCE); } @@ -37,7 +36,7 @@ public final class ApacheHttpClientTelemetryBuilder { public ApacheHttpClientTelemetryBuilder addAttributeExtractor( AttributesExtractor attributesExtractor) { - clientBuilder.addAttributeExtractor(attributesExtractor); + builder.addAttributeExtractor(attributesExtractor); return this; } @@ -48,7 +47,7 @@ public ApacheHttpClientTelemetryBuilder addAttributeExtractor( */ @CanIgnoreReturnValue public ApacheHttpClientTelemetryBuilder setCapturedRequestHeaders(List requestHeaders) { - clientBuilder.setCapturedRequestHeaders(requestHeaders); + builder.setCapturedRequestHeaders(requestHeaders); return this; } @@ -59,7 +58,7 @@ public ApacheHttpClientTelemetryBuilder setCapturedRequestHeaders(List r */ @CanIgnoreReturnValue public ApacheHttpClientTelemetryBuilder setCapturedResponseHeaders(List responseHeaders) { - clientBuilder.setCapturedResponseHeaders(responseHeaders); + builder.setCapturedResponseHeaders(responseHeaders); return this; } @@ -78,7 +77,7 @@ public ApacheHttpClientTelemetryBuilder setCapturedResponseHeaders(List */ @CanIgnoreReturnValue public ApacheHttpClientTelemetryBuilder setKnownMethods(Set knownMethods) { - clientBuilder.setKnownMethods(knownMethods); + builder.setKnownMethods(knownMethods); return this; } @@ -91,7 +90,7 @@ public ApacheHttpClientTelemetryBuilder setKnownMethods(Set knownMethods @CanIgnoreReturnValue public ApacheHttpClientTelemetryBuilder setEmitExperimentalHttpClientMetrics( boolean emitExperimentalHttpClientMetrics) { - clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics); + builder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics); return this; } @@ -102,7 +101,7 @@ public ApacheHttpClientTelemetryBuilder setSpanNameExtractor( SpanNameExtractor, ? extends SpanNameExtractor> spanNameExtractorTransformer) { - clientBuilder.setSpanNameExtractor(spanNameExtractorTransformer); + builder.setSpanNameExtractor(spanNameExtractorTransformer); return this; } @@ -112,6 +111,6 @@ public ApacheHttpClientTelemetryBuilder setSpanNameExtractor( */ public ApacheHttpClientTelemetry build() { return new ApacheHttpClientTelemetry( - clientBuilder.build(), clientBuilder.getOpenTelemetry().getPropagators()); + builder.build(), builder.getOpenTelemetry().getPropagators()); } } diff --git a/instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5TelemetryBuilder.java b/instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5TelemetryBuilder.java index 8dbf7215c8be..e9a5eb273de0 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5TelemetryBuilder.java +++ b/instrumentation/apache-httpclient/apache-httpclient-5.2/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v5_2/ApacheHttpClient5TelemetryBuilder.java @@ -21,10 +21,10 @@ public final class ApacheHttpClient5TelemetryBuilder { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.apache-httpclient-5.2"; private final DefaultHttpClientInstrumenterBuilder - clientBuilder; + builder; ApacheHttpClient5TelemetryBuilder(OpenTelemetry openTelemetry) { - clientBuilder = + builder = new DefaultHttpClientInstrumenterBuilder<>( INSTRUMENTATION_NAME, openTelemetry, ApacheHttpClient5HttpAttributesGetter.INSTANCE); } @@ -37,7 +37,7 @@ public final class ApacheHttpClient5TelemetryBuilder { public ApacheHttpClient5TelemetryBuilder addAttributeExtractor( AttributesExtractor attributesExtractor) { - clientBuilder.addAttributeExtractor(attributesExtractor); + builder.addAttributeExtractor(attributesExtractor); return this; } @@ -48,7 +48,7 @@ public ApacheHttpClient5TelemetryBuilder addAttributeExtractor( */ @CanIgnoreReturnValue public ApacheHttpClient5TelemetryBuilder setCapturedRequestHeaders(List requestHeaders) { - clientBuilder.setCapturedRequestHeaders(requestHeaders); + builder.setCapturedRequestHeaders(requestHeaders); return this; } @@ -60,7 +60,7 @@ public ApacheHttpClient5TelemetryBuilder setCapturedRequestHeaders(List @CanIgnoreReturnValue public ApacheHttpClient5TelemetryBuilder setCapturedResponseHeaders( List responseHeaders) { - clientBuilder.setCapturedResponseHeaders(responseHeaders); + builder.setCapturedResponseHeaders(responseHeaders); return this; } @@ -79,7 +79,7 @@ public ApacheHttpClient5TelemetryBuilder setCapturedResponseHeaders( */ @CanIgnoreReturnValue public ApacheHttpClient5TelemetryBuilder setKnownMethods(Set knownMethods) { - clientBuilder.setKnownMethods(knownMethods); + builder.setKnownMethods(knownMethods); return this; } @@ -92,7 +92,7 @@ public ApacheHttpClient5TelemetryBuilder setKnownMethods(Set knownMethod @CanIgnoreReturnValue public ApacheHttpClient5TelemetryBuilder setEmitExperimentalHttpClientMetrics( boolean emitExperimentalHttpClientMetrics) { - clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics); + builder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics); return this; } @@ -103,7 +103,7 @@ public ApacheHttpClient5TelemetryBuilder setSpanNameExtractor( SpanNameExtractor, ? extends SpanNameExtractor> spanNameExtractorTransformer) { - clientBuilder.setSpanNameExtractor(spanNameExtractorTransformer); + builder.setSpanNameExtractor(spanNameExtractorTransformer); return this; } @@ -113,6 +113,6 @@ public ApacheHttpClient5TelemetryBuilder setSpanNameExtractor( */ public ApacheHttpClient5Telemetry build() { return new ApacheHttpClient5Telemetry( - clientBuilder.build(), clientBuilder.getOpenTelemetry().getPropagators()); + builder.build(), builder.getOpenTelemetry().getPropagators()); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure-3/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestClientBeanPostProcessor.java b/instrumentation/spring/spring-boot-autoconfigure-3/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestClientBeanPostProcessor.java index 74b7eaf86646..e551a31fe1b3 100644 --- a/instrumentation/spring/spring-boot-autoconfigure-3/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestClientBeanPostProcessor.java +++ b/instrumentation/spring/spring-boot-autoconfigure-3/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestClientBeanPostProcessor.java @@ -56,7 +56,7 @@ private static RestClient addRestClientInterceptorIfNotPresent( static ClientHttpRequestInterceptor getInterceptor( OpenTelemetry openTelemetry, ConfigProperties config) { - return InstrumentationConfigUtil.configureBuilder( + return InstrumentationConfigUtil.configureClientBuilder( config, SpringWebTelemetry.builder(openTelemetry), WebTelemetryUtil.GET_BUILDER) .build() .newInterceptor(); diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateInstrumentation.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateInstrumentation.java index 55e6bc68ba67..b800f3357961 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateInstrumentation.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/web/RestTemplateInstrumentation.java @@ -24,7 +24,7 @@ static RestTemplate addIfNotPresent( RestTemplate restTemplate, OpenTelemetry openTelemetry, ConfigProperties config) { ClientHttpRequestInterceptor instrumentationInterceptor = - InstrumentationConfigUtil.configureBuilder( + InstrumentationConfigUtil.configureClientBuilder( config, SpringWebTelemetry.builder(openTelemetry), WebTelemetryUtil.GET_BUILDER) .build() .newInterceptor(); diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java index aa40bfd52e63..d7b6f30ff3d9 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webflux/WebClientBeanPostProcessor.java @@ -8,6 +8,7 @@ import io.opentelemetry.api.OpenTelemetry; import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.InstrumentationConfigUtil; import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetry; +import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.SpringWebfluxUtil; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import org.springframework.beans.factory.ObjectProvider; import org.springframework.beans.factory.config.BeanPostProcessor; @@ -33,7 +34,10 @@ final class WebClientBeanPostProcessor implements BeanPostProcessor { static SpringWebfluxTelemetry getWebfluxTelemetry( OpenTelemetry openTelemetry, ConfigProperties config) { return InstrumentationConfigUtil.configureClientAndServerBuilder( - config, SpringWebfluxTelemetry.builder(openTelemetry)) + config, + SpringWebfluxTelemetry.builder(openTelemetry), + SpringWebfluxUtil.GET_CLIENT_BUILDER, + SpringWebfluxUtil.GET_SERVER_BUILDER) .build(); } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc5InstrumentationAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc5InstrumentationAutoConfiguration.java index 6f33f4d41980..c237b2a46afb 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc5InstrumentationAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc5InstrumentationAutoConfiguration.java @@ -9,6 +9,7 @@ import io.opentelemetry.instrumentation.spring.autoconfigure.internal.ConditionalOnEnabledInstrumentation; import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.InstrumentationConfigUtil; import io.opentelemetry.instrumentation.spring.webmvc.v5_3.SpringWebMvcTelemetry; +import io.opentelemetry.instrumentation.spring.webmvc.v5_3.internal.SpringMvcUtil; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import javax.servlet.Filter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -26,7 +27,7 @@ public class SpringWebMvc5InstrumentationAutoConfiguration { @Bean Filter otelWebMvcFilter(OpenTelemetry openTelemetry, ConfigProperties config) { return InstrumentationConfigUtil.configureServerBuilder( - config, SpringWebMvcTelemetry.builder(openTelemetry)) + config, SpringWebMvcTelemetry.builder(openTelemetry), SpringMvcUtil.GET_BUILDER) .build() .createServletFilter(); } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc6InstrumentationAutoConfiguration.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc6InstrumentationAutoConfiguration.java index ea9e0bfe82ad..0c5b38bbaf04 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc6InstrumentationAutoConfiguration.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/instrumentation/webmvc/SpringWebMvc6InstrumentationAutoConfiguration.java @@ -9,6 +9,7 @@ import io.opentelemetry.instrumentation.spring.autoconfigure.internal.ConditionalOnEnabledInstrumentation; import io.opentelemetry.instrumentation.spring.autoconfigure.internal.properties.InstrumentationConfigUtil; import io.opentelemetry.instrumentation.spring.webmvc.v6_0.SpringWebMvcTelemetry; +import io.opentelemetry.instrumentation.spring.webmvc.v6_0.internal.SpringMvcUtil; import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties; import jakarta.servlet.Filter; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -26,7 +27,7 @@ public class SpringWebMvc6InstrumentationAutoConfiguration { @Bean Filter otelWebMvcFilter(OpenTelemetry openTelemetry, ConfigProperties config) { return InstrumentationConfigUtil.configureServerBuilder( - config, SpringWebMvcTelemetry.builder(openTelemetry)) + config, SpringWebMvcTelemetry.builder(openTelemetry), SpringMvcUtil.GET_BUILDER) .build() .createServletFilter(); } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/InstrumentationConfigUtil.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/InstrumentationConfigUtil.java index 9d8e90bc8b5a..f46b78cc72d3 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/InstrumentationConfigUtil.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/properties/InstrumentationConfigUtil.java @@ -20,30 +20,42 @@ public class InstrumentationConfigUtil { private InstrumentationConfigUtil() {} @CanIgnoreReturnValue - public static T configureClientAndServerBuilder(ConfigProperties config, T builder) { - DefaultHttpClientInstrumenterBuilder.unwrapAndConfigure( - new CoreCommonConfig(new ConfigPropertiesBridge(config)), builder); - DefaultHttpServerInstrumenterBuilder.unwrapAndConfigure( - new CoreCommonConfig(new ConfigPropertiesBridge(config)), builder); + public static + T configureClientAndServerBuilder( + ConfigProperties config, + T builder, + Function> + getClientBuilder, + Function> + getServerBuilder) { + CoreCommonConfig commonConfig = getConfig(config); + getClientBuilder.apply(builder).configure(commonConfig); + getServerBuilder.apply(builder).configure(commonConfig); return builder; } @CanIgnoreReturnValue - public static T configureClientBuilder( + public static T configureClientBuilder( ConfigProperties config, T builder, Function> getBuilder) { - getBuilder.apply(builder).configure(new CoreCommonConfig(new ConfigPropertiesBridge(config))); + getBuilder.apply(builder).configure(getConfig(config)); return builder; } @CanIgnoreReturnValue - public static T configureServerBuilder(ConfigProperties config, T builder) { - DefaultHttpServerInstrumenterBuilder.unwrapAndConfigure( - new CoreCommonConfig(new ConfigPropertiesBridge(config)), builder); + public static T configureServerBuilder( + ConfigProperties config, + T builder, + Function> getBuilder) { + getBuilder.apply(builder).configure(getConfig(config)); return builder; } + private static CoreCommonConfig getConfig(ConfigProperties config) { + return new CoreCommonConfig(new ConfigPropertiesBridge(config)); + } + public static boolean isStatementSanitizationEnabled(ConfigProperties config, String key) { return config.getBoolean( key, config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true)); 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 ac23cccc8264..a4edb71a0aa8 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 @@ -21,14 +21,14 @@ /** A builder of {@link SpringWebTelemetry}. */ public final class SpringWebTelemetryBuilder { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-web-3.1"; - private final DefaultHttpClientInstrumenterBuilder clientBuilder; + private final DefaultHttpClientInstrumenterBuilder builder; static { WebTelemetryUtil.GET_BUILDER = SpringWebTelemetryBuilder::getBuilder; } SpringWebTelemetryBuilder(OpenTelemetry openTelemetry) { - clientBuilder = + builder = new DefaultHttpClientInstrumenterBuilder<>( INSTRUMENTATION_NAME, openTelemetry, SpringWebHttpAttributesGetter.INSTANCE) .setHeaderSetter(HttpRequestSetter.INSTANCE); @@ -45,7 +45,7 @@ private DefaultHttpClientInstrumenterBuilder ge @CanIgnoreReturnValue public SpringWebTelemetryBuilder addAttributeExtractor( AttributesExtractor attributesExtractor) { - clientBuilder.addAttributeExtractor(attributesExtractor); + builder.addAttributeExtractor(attributesExtractor); return this; } @@ -56,7 +56,7 @@ public SpringWebTelemetryBuilder addAttributeExtractor( */ @CanIgnoreReturnValue public SpringWebTelemetryBuilder setCapturedRequestHeaders(List requestHeaders) { - clientBuilder.setCapturedRequestHeaders(requestHeaders); + builder.setCapturedRequestHeaders(requestHeaders); return this; } @@ -67,7 +67,7 @@ public SpringWebTelemetryBuilder setCapturedRequestHeaders(List requestH */ @CanIgnoreReturnValue public SpringWebTelemetryBuilder setCapturedResponseHeaders(List responseHeaders) { - clientBuilder.setCapturedResponseHeaders(responseHeaders); + builder.setCapturedResponseHeaders(responseHeaders); return this; } @@ -76,7 +76,7 @@ public SpringWebTelemetryBuilder setCapturedResponseHeaders(List respons public SpringWebTelemetryBuilder setSpanNameExtractor( Function, ? extends SpanNameExtractor> spanNameExtractorTransformer) { - clientBuilder.setSpanNameExtractor(spanNameExtractorTransformer); + builder.setSpanNameExtractor(spanNameExtractorTransformer); return this; } @@ -95,7 +95,7 @@ public SpringWebTelemetryBuilder setSpanNameExtractor( */ @CanIgnoreReturnValue public SpringWebTelemetryBuilder setKnownMethods(Set knownMethods) { - clientBuilder.setKnownMethods(knownMethods); + builder.setKnownMethods(knownMethods); return this; } @@ -108,7 +108,7 @@ public SpringWebTelemetryBuilder setKnownMethods(Set knownMethods) { @CanIgnoreReturnValue public SpringWebTelemetryBuilder setEmitExperimentalHttpClientMetrics( boolean emitExperimentalHttpClientMetrics) { - clientBuilder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics); + builder.setEmitExperimentalHttpClientMetrics(emitExperimentalHttpClientMetrics); return this; } @@ -117,6 +117,6 @@ public SpringWebTelemetryBuilder setEmitExperimentalHttpClientMetrics( * SpringWebTelemetryBuilder}. */ public SpringWebTelemetry build() { - return new SpringWebTelemetry(clientBuilder.build()); + return new SpringWebTelemetry(builder.build()); } } diff --git a/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxTelemetryBuilder.java b/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxTelemetryBuilder.java index 139d3c9d8c18..e417c9497e5b 100644 --- a/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxTelemetryBuilder.java +++ b/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/SpringWebfluxTelemetryBuilder.java @@ -13,6 +13,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesExtractorBuilder; import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder; +import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.SpringWebfluxUtil; import io.opentelemetry.instrumentation.spring.webflux.v5_3.internal.WebClientHttpAttributesGetter; import java.util.List; import java.util.Set; @@ -29,6 +30,11 @@ public final class SpringWebfluxTelemetryBuilder { private final DefaultHttpServerInstrumenterBuilder serverBuilder; + static { + SpringWebfluxUtil.GET_CLIENT_BUILDER = SpringWebfluxTelemetryBuilder::getClientBuilder; + SpringWebfluxUtil.GET_SERVER_BUILDER = SpringWebfluxTelemetryBuilder::getServerBuilder; + } + SpringWebfluxTelemetryBuilder(OpenTelemetry openTelemetry) { clientBuilder = new DefaultHttpClientInstrumenterBuilder<>( @@ -188,4 +194,13 @@ public SpringWebfluxTelemetry build() { serverBuilder.build(), clientBuilder.getOpenTelemetry().getPropagators()); } + + private DefaultHttpClientInstrumenterBuilder getClientBuilder() { + return clientBuilder; + } + + private DefaultHttpServerInstrumenterBuilder + getServerBuilder() { + return serverBuilder; + } } diff --git a/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/internal/SpringWebfluxUtil.java b/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/internal/SpringWebfluxUtil.java new file mode 100644 index 000000000000..4a4d8bb16a24 --- /dev/null +++ b/instrumentation/spring/spring-webflux/spring-webflux-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webflux/v5_3/internal/SpringWebfluxUtil.java @@ -0,0 +1,34 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.webflux.v5_3.internal; + +import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder; +import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder; +import io.opentelemetry.instrumentation.spring.webflux.v5_3.SpringWebfluxTelemetryBuilder; +import java.util.function.Function; +import org.springframework.web.reactive.function.client.ClientRequest; +import org.springframework.web.reactive.function.client.ClientResponse; +import org.springframework.web.server.ServerWebExchange; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public class SpringWebfluxUtil { + private SpringWebfluxUtil() {} + + @SuppressWarnings("ConstantField") + public static Function< + SpringWebfluxTelemetryBuilder, + DefaultHttpClientInstrumenterBuilder> + GET_CLIENT_BUILDER; + + @SuppressWarnings("ConstantField") + public static Function< + SpringWebfluxTelemetryBuilder, + DefaultHttpServerInstrumenterBuilder> + GET_SERVER_BUILDER; +} 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 500ea16b1b8b..ad699ef8e99a 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 @@ -11,6 +11,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder; +import io.opentelemetry.instrumentation.spring.webmvc.v5_3.internal.SpringMvcUtil; import java.util.List; import java.util.Set; import java.util.function.Function; @@ -23,10 +24,14 @@ public final class SpringWebMvcTelemetryBuilder { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-webmvc-5.3"; private final DefaultHttpServerInstrumenterBuilder - serverBuilder; + builder; + + static { + SpringMvcUtil.GET_BUILDER = SpringWebMvcTelemetryBuilder::getBuilder; + } SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) { - serverBuilder = + builder = new DefaultHttpServerInstrumenterBuilder<>( INSTRUMENTATION_NAME, openTelemetry, SpringWebMvcHttpAttributesGetter.INSTANCE) .setHeaderGetter(JavaxHttpServletRequestGetter.INSTANCE); @@ -39,7 +44,7 @@ public final class SpringWebMvcTelemetryBuilder { @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder addAttributesExtractor( AttributesExtractor attributesExtractor) { - serverBuilder.addAttributesExtractor(attributesExtractor); + builder.addAttributesExtractor(attributesExtractor); return this; } @@ -50,7 +55,7 @@ public SpringWebMvcTelemetryBuilder addAttributesExtractor( */ @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setCapturedRequestHeaders(List requestHeaders) { - serverBuilder.setCapturedRequestHeaders(requestHeaders); + builder.setCapturedRequestHeaders(requestHeaders); return this; } @@ -61,7 +66,7 @@ public SpringWebMvcTelemetryBuilder setCapturedRequestHeaders(List reque */ @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List responseHeaders) { - serverBuilder.setCapturedResponseHeaders(responseHeaders); + builder.setCapturedResponseHeaders(responseHeaders); return this; } @@ -72,7 +77,7 @@ public SpringWebMvcTelemetryBuilder setSpanNameExtractor( SpanNameExtractor, ? extends SpanNameExtractor> spanNameExtractor) { - serverBuilder.setSpanNameExtractor(spanNameExtractor); + builder.setSpanNameExtractor(spanNameExtractor); return this; } @@ -91,7 +96,7 @@ public SpringWebMvcTelemetryBuilder setSpanNameExtractor( */ @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setKnownMethods(Set knownMethods) { - serverBuilder.setKnownMethods(knownMethods); + builder.setKnownMethods(knownMethods); return this; } @@ -104,7 +109,7 @@ public SpringWebMvcTelemetryBuilder setKnownMethods(Set knownMethods) { @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setEmitExperimentalHttpServerMetrics( boolean emitExperimentalHttpServerMetrics) { - serverBuilder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpServerMetrics); + builder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpServerMetrics); return this; } @@ -113,6 +118,11 @@ public SpringWebMvcTelemetryBuilder setEmitExperimentalHttpServerMetrics( * SpringWebMvcTelemetryBuilder}. */ public SpringWebMvcTelemetry build() { - return new SpringWebMvcTelemetry(serverBuilder.build()); + return new SpringWebMvcTelemetry(builder.build()); + } + + public DefaultHttpServerInstrumenterBuilder + getBuilder() { + return builder; } } diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/internal/SpringMvcUtil.java b/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/internal/SpringMvcUtil.java new file mode 100644 index 000000000000..547f99e96bbd --- /dev/null +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-5.3/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v5_3/internal/SpringMvcUtil.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.webmvc.v5_3.internal; + +import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder; +import io.opentelemetry.instrumentation.spring.webmvc.v5_3.SpringWebMvcTelemetryBuilder; +import java.util.function.Function; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public class SpringMvcUtil { + private SpringMvcUtil() {} + + @SuppressWarnings("ConstantField") + public static Function< + SpringWebMvcTelemetryBuilder, + DefaultHttpServerInstrumenterBuilder> + GET_BUILDER; +} 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 801c01b05375..a8361c014672 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 @@ -11,6 +11,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; import io.opentelemetry.instrumentation.api.semconv.http.HttpServerAttributesExtractorBuilder; +import io.opentelemetry.instrumentation.spring.webmvc.v6_0.internal.SpringMvcUtil; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import java.util.List; @@ -22,10 +23,14 @@ public final class SpringWebMvcTelemetryBuilder { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.spring-webmvc-6.0"; private final DefaultHttpServerInstrumenterBuilder - serverBuilder; + builder; + + static { + SpringMvcUtil.GET_BUILDER = SpringWebMvcTelemetryBuilder::getBuilder; + } SpringWebMvcTelemetryBuilder(OpenTelemetry openTelemetry) { - serverBuilder = + builder = new DefaultHttpServerInstrumenterBuilder<>( INSTRUMENTATION_NAME, openTelemetry, SpringWebMvcHttpAttributesGetter.INSTANCE) .setHeaderGetter(JakartaHttpServletRequestGetter.INSTANCE); @@ -38,7 +43,7 @@ public final class SpringWebMvcTelemetryBuilder { @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder addAttributesExtractor( AttributesExtractor attributesExtractor) { - serverBuilder.addAttributesExtractor(attributesExtractor); + builder.addAttributesExtractor(attributesExtractor); return this; } @@ -49,7 +54,7 @@ public SpringWebMvcTelemetryBuilder addAttributesExtractor( */ @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setCapturedRequestHeaders(List requestHeaders) { - serverBuilder.setCapturedRequestHeaders(requestHeaders); + builder.setCapturedRequestHeaders(requestHeaders); return this; } @@ -60,7 +65,7 @@ public SpringWebMvcTelemetryBuilder setCapturedRequestHeaders(List reque */ @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setCapturedResponseHeaders(List responseHeaders) { - serverBuilder.setCapturedResponseHeaders(responseHeaders); + builder.setCapturedResponseHeaders(responseHeaders); return this; } @@ -71,7 +76,7 @@ public SpringWebMvcTelemetryBuilder setSpanNameExtractor( SpanNameExtractor, ? extends SpanNameExtractor> spanNameExtractor) { - serverBuilder.setSpanNameExtractor(spanNameExtractor); + builder.setSpanNameExtractor(spanNameExtractor); return this; } @@ -90,7 +95,7 @@ public SpringWebMvcTelemetryBuilder setSpanNameExtractor( */ @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setKnownMethods(Set knownMethods) { - serverBuilder.setKnownMethods(knownMethods); + builder.setKnownMethods(knownMethods); return this; } @@ -103,7 +108,7 @@ public SpringWebMvcTelemetryBuilder setKnownMethods(Set knownMethods) { @CanIgnoreReturnValue public SpringWebMvcTelemetryBuilder setEmitExperimentalHttpServerMetrics( boolean emitExperimentalHttpServerMetrics) { - serverBuilder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpServerMetrics); + builder.setEmitExperimentalHttpServerMetrics(emitExperimentalHttpServerMetrics); return this; } @@ -112,6 +117,11 @@ public SpringWebMvcTelemetryBuilder setEmitExperimentalHttpServerMetrics( * SpringWebMvcTelemetryBuilder}. */ public SpringWebMvcTelemetry build() { - return new SpringWebMvcTelemetry(serverBuilder.build()); + return new SpringWebMvcTelemetry(builder.build()); + } + + public DefaultHttpServerInstrumenterBuilder + getBuilder() { + return builder; } } diff --git a/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/internal/SpringMvcUtil.java b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/internal/SpringMvcUtil.java new file mode 100644 index 000000000000..41499a45f0cc --- /dev/null +++ b/instrumentation/spring/spring-webmvc/spring-webmvc-6.0/library/src/main/java/io/opentelemetry/instrumentation/spring/webmvc/v6_0/internal/SpringMvcUtil.java @@ -0,0 +1,26 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.instrumentation.spring.webmvc.v6_0.internal; + +import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder; +import io.opentelemetry.instrumentation.spring.webmvc.v6_0.SpringWebMvcTelemetryBuilder; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import java.util.function.Function; + +/** + * This class is internal and is hence not for public use. Its APIs are unstable and can change at + * any time. + */ +public class SpringMvcUtil { + private SpringMvcUtil() {} + + @SuppressWarnings("ConstantField") + public static Function< + SpringWebMvcTelemetryBuilder, + DefaultHttpServerInstrumenterBuilder> + GET_BUILDER; +} diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/JavaagentHttpClientInstrumenters.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/JavaagentHttpClientInstrumenters.java index 7b2817f90a55..8d5c995ac4b4 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/JavaagentHttpClientInstrumenters.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/JavaagentHttpClientInstrumenters.java @@ -8,7 +8,6 @@ import io.opentelemetry.api.GlobalOpenTelemetry; import io.opentelemetry.context.propagation.TextMapSetter; import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder; -import io.opentelemetry.instrumentation.api.incubator.builder.internal.HttpClientInstrumenterBuilder; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; import io.opentelemetry.instrumentation.api.semconv.http.HttpClientAttributesGetter;