Skip to content

Commit

Permalink
use DefaultHttpServerInstrumenterBuilder to configure spring starter
Browse files Browse the repository at this point in the history
  • Loading branch information
zeitlinger committed Jun 27, 2024
1 parent 046b249 commit 25e510f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,12 +243,13 @@ DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE> unwrapBuilder(Object bui
if (builder instanceof DefaultHttpServerInstrumenterBuilder<?, ?>) {
return (DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE>) builder;
}
Class<?> builderClass = builder.getClass();
try {
Field field = builder.getClass().getDeclaredField("serverBuilder");
Field field = builderClass.getDeclaredField("serverBuilder");
field.setAccessible(true);
return (DefaultHttpServerInstrumenterBuilder<REQUEST, RESPONSE>) field.get(builder);
} catch (Exception e) {
throw new IllegalStateException("Could not access builder field", e);
throw new IllegalStateException("Could not access serverBuilder field in " + builderClass, e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class WebClientBeanPostProcessor implements BeanPostProcessor {

static SpringWebfluxTelemetry getWebfluxTelemetry(
OpenTelemetry openTelemetry, ConfigProperties config) {
return InstrumentationConfigUtil.configureBuilder(
return InstrumentationConfigUtil.configureClientAndServerBuilder(
config, SpringWebfluxTelemetry.builder(openTelemetry))
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SpringWebMvc5InstrumentationAutoConfiguration {

@Bean
Filter otelWebMvcFilter(OpenTelemetry openTelemetry, ConfigProperties config) {
return InstrumentationConfigUtil.configureBuilder(
return InstrumentationConfigUtil.configureServerBuilder(
config, SpringWebMvcTelemetry.builder(openTelemetry))
.build()
.createServletFilter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SpringWebMvc6InstrumentationAutoConfiguration {

@Bean
Filter otelWebMvcFilter(OpenTelemetry openTelemetry, ConfigProperties config) {
return InstrumentationConfigUtil.configureBuilder(
return InstrumentationConfigUtil.configureServerBuilder(
config, SpringWebMvcTelemetry.builder(openTelemetry))
.build()
.createServletFilter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpClientInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.builder.internal.DefaultHttpServerInstrumenterBuilder;
import io.opentelemetry.instrumentation.api.incubator.config.internal.CoreCommonConfig;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import java.util.function.Function;
Expand All @@ -19,14 +20,30 @@ public class InstrumentationConfigUtil {
private InstrumentationConfigUtil() {}

@CanIgnoreReturnValue
public static <T, REQUEST, RESPONSE> T configureBuilder(
public static <T, REQUEST, RESPONSE> T configureClientAndServerBuilder(ConfigProperties config, T builder) {
DefaultHttpClientInstrumenterBuilder.unwrapAndConfigure(
new CoreCommonConfig(new ConfigPropertiesBridge(config)), builder);
DefaultHttpServerInstrumenterBuilder.unwrapAndConfigure(
new CoreCommonConfig(new ConfigPropertiesBridge(config)), builder);
return builder;
}

@CanIgnoreReturnValue
public static <T> T configureClientBuilder(
ConfigProperties config,
T builder,
Function<T, DefaultHttpClientInstrumenterBuilder<REQUEST, RESPONSE>> getBuilder) {
getBuilder.apply(builder).configure(new CoreCommonConfig(new ConfigPropertiesBridge(config)));
return builder;
}

@CanIgnoreReturnValue
public static <T> T configureServerBuilder(ConfigProperties config, T builder) {
DefaultHttpServerInstrumenterBuilder.unwrapAndConfigure(
new CoreCommonConfig(new ConfigPropertiesBridge(config)), builder);
return builder;
}

public static boolean isStatementSanitizationEnabled(ConfigProperties config, String key) {
return config.getBoolean(
key, config.getBoolean("otel.instrumentation.common.db-statement-sanitizer.enabled", true));
Expand Down

0 comments on commit 25e510f

Please sign in to comment.