Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

howto configure exporter programmatically in spring starter #4282

Merged
merged 5 commits into from
Apr 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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