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

configure spring OTLP exporter without using internal classes #394

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions spring-native/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ The example uses the following elements:
and exporting to the standard output with
the [logging exporter](https://github.com/open-telemetry/opentelemetry-collector/tree/main/exporter/loggingexporter).
- A spring configuration to suppress spans for the `/actuator` endpoint
- A spring configuration to set OTLP headers dynamically
(not needed for the example - it shows how to configure exporters programmatically)

# Description of the instrumentation set-up

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,12 @@
package io.opentelemetry.example.graal;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.semconv.SemanticAttributes;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

@Bean
public AutoConfigurationCustomizerProvider otelCustomizer() {
return p ->
p.addSamplerCustomizer(
(fallback, c) ->
RuleBasedRoutingSampler.builder(SpanKind.SERVER, fallback)
.drop(SemanticAttributes.URL_PATH, "^/actuator")
.build());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package io.opentelemetry.example.graal;

import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.contrib.sampler.RuleBasedRoutingSampler;
import io.opentelemetry.exporter.otlp.http.trace.OtlpHttpSpanExporter;
import io.opentelemetry.sdk.autoconfigure.spi.AutoConfigurationCustomizerProvider;
import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
import io.opentelemetry.sdk.trace.export.SpanExporter;
import io.opentelemetry.sdk.trace.samplers.Sampler;
import io.opentelemetry.semconv.SemanticAttributes;
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.addSamplerCustomizer(this::configureSampler)
.addSpanExporterCustomizer(this::configureSpanExporter);
}

/** suppress spans for actuator endpoints */
private RuleBasedRoutingSampler configureSampler(Sampler fallback, ConfigProperties config) {
return RuleBasedRoutingSampler.builder(SpanKind.SERVER, fallback)
.drop(SemanticAttributes.URL_PATH, "^/actuator")
.build();
}

/**
* Configuration for the OTLP exporter. This configuration will replace the default OTLP exporter,
* and will add a custom header to the requests.
*/
private SpanExporter configureSpanExporter(SpanExporter exporter, ConfigProperties 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";
}
}

This file was deleted.

Loading