From a36d7ebd9f3975e3f202c9362aa687cc64696add Mon Sep 17 00:00:00 2001 From: Gregor Zeitlinger Date: Sat, 20 Apr 2024 01:12:53 +0200 Subject: [PATCH] howto configure exporter programmatically in spring starter (#4282) Co-authored-by: Fabrizio Ferri-Benedetti Co-authored-by: Phillip Carter --- .../languages/java/automatic/spring-boot.md | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/content/en/docs/languages/java/automatic/spring-boot.md b/content/en/docs/languages/java/automatic/spring-boot.md index fa648effefb0..e49c23d413f1 100644 --- a/content/en/docs/languages/java/automatic/spring-boot.md +++ b/content/en/docs/languages/java/automatic/spring-boot.md @@ -280,7 +280,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration -public class Application { +public class OpenTelemetryConfig { @Bean public AutoConfigurationCustomizerProvider otelCustomizer() { @@ -294,6 +294,46 @@ public class Application { } ``` +##### Configure the exporter programmatically + +You can also configure OTLP exporters programmatically. This configuration +replaces the default OTLP exporter and adds a custom header to the requests. + +```java +import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter; +import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider; +import java.util.Collections; +import java.util.Map; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class OpenTelemetryConfig { + + @Bean + public AutoConfigurationCustomizerProvider otelCustomizer() { + return p -> + p.addSpanExporterCustomizer( + (exporter, config) -> { + if (exporter instanceof OtlpHttpSpanExporter) { + return ((OtlpHttpSpanExporter) exporter) + .toBuilder().setHeaders(this::headers).build(); + } + return exporter; + }); + } + + private Map headers() { + return Collections.singletonMap("Authorization", "Bearer " + refreshToken()); + } + + private String refreshToken() { + // e.g. read the token from a kubernetes secret + return "token"; + } +} +``` + #### Resource Providers The OpenTelemetry Starter includes