Skip to content

Commit

Permalink
howto configure exporter programmatically in spring starter (#4282)
Browse files Browse the repository at this point in the history
Co-authored-by: Fabrizio Ferri-Benedetti <[email protected]>
Co-authored-by: Phillip Carter <[email protected]>
  • Loading branch information
3 people authored Apr 19, 2024
1 parent cdb7877 commit a36d7eb
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion content/en/docs/languages/java/automatic/spring-boot.md
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<String, String> 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
Expand Down

0 comments on commit a36d7eb

Please sign in to comment.