forked from open-telemetry/opentelemetry-java-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenTelemetryConfig.java
More file actions
52 lines (45 loc) · 1.88 KB
/
OpenTelemetryConfig.java
File metadata and controls
52 lines (45 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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.UrlAttributes;
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(UrlAttributes.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";
}
}