|
38 | 38 | import org.springframework.boot.SpringBootVersion;
|
39 | 39 | import org.springframework.boot.autoconfigure.AutoConfiguration;
|
40 | 40 | import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
| 41 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
41 | 42 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
42 | 43 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
43 | 44 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
44 | 45 | import org.springframework.context.annotation.Bean;
|
45 | 46 | import org.springframework.context.annotation.Configuration;
|
| 47 | +import org.springframework.context.annotation.Import; |
46 | 48 |
|
47 | 49 | /**
|
48 | 50 | * {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry.
|
|
51 | 53 | * @since 3.0.0
|
52 | 54 | */
|
53 | 55 | @AutoConfiguration(after = ZipkinAutoConfiguration.class)
|
54 |
| -@ConditionalOnClass(Tracer.class) |
55 |
| -@EnableConfigurationProperties(TracingProperties.class) |
56 | 56 | public class OpenTelemetryAutoConfiguration {
|
57 | 57 |
|
58 |
| - @Bean |
59 |
| - @ConditionalOnMissingBean |
60 |
| - Tracer tracer(OpenTelemetry openTelemetry) { |
61 |
| - // TODO MH: Decide on a name |
62 |
| - return openTelemetry.getTracer("org.springframework.boot", SpringBootVersion.getVersion()); |
63 |
| - } |
| 58 | + @Configuration(proxyBeanMethods = false) |
| 59 | + @ConditionalOnClass(SdkTracerProvider.class) |
| 60 | + @EnableConfigurationProperties(TracingProperties.class) |
| 61 | + static class Sdk { |
64 | 62 |
|
65 |
| - @Bean |
66 |
| - @ConditionalOnMissingBean |
67 |
| - OpenTelemetry openTelemetry(SdkTracerProvider sdkTracerProvider, ContextPropagators contextPropagators) { |
68 |
| - return OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider).setPropagators(contextPropagators) |
69 |
| - .buildAndRegisterGlobal(); |
70 |
| - } |
| 63 | + @Bean |
| 64 | + @ConditionalOnMissingBean |
| 65 | + OpenTelemetry openTelemetry(SdkTracerProvider sdkTracerProvider, ContextPropagators contextPropagators) { |
| 66 | + return OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider).setPropagators(contextPropagators) |
| 67 | + .build(); |
| 68 | + } |
71 | 69 |
|
72 |
| - @Bean |
73 |
| - @ConditionalOnMissingBean |
74 |
| - ContextPropagators contextPropagators(List<TextMapPropagator> textMapPropagators) { |
75 |
| - return ContextPropagators.create(TextMapPropagator.composite(textMapPropagators)); |
76 |
| - } |
| 70 | + @Bean |
| 71 | + @ConditionalOnMissingBean |
| 72 | + SdkTracerProvider sdkTracerProvider(List<SpanProcessor> spanProcessors, Sampler sampler) { |
| 73 | + SdkTracerProviderBuilder builder = SdkTracerProvider.builder().setSampler(sampler); |
| 74 | + for (SpanProcessor spanProcessor : spanProcessors) { |
| 75 | + builder.addSpanProcessor(spanProcessor); |
| 76 | + } |
| 77 | + return builder.build(); |
| 78 | + } |
77 | 79 |
|
78 |
| - @Bean |
79 |
| - @ConditionalOnMissingBean |
80 |
| - SdkTracerProvider sdkTracerProvider(List<SpanProcessor> spanProcessors, Sampler sampler) { |
81 |
| - SdkTracerProviderBuilder builder = SdkTracerProvider.builder().setSampler(sampler); |
82 |
| - for (SpanProcessor spanProcessor : spanProcessors) { |
83 |
| - builder.addSpanProcessor(spanProcessor); |
| 80 | + @Bean |
| 81 | + @ConditionalOnMissingBean |
| 82 | + ContextPropagators contextPropagators(List<TextMapPropagator> textMapPropagators) { |
| 83 | + return ContextPropagators.create(TextMapPropagator.composite(textMapPropagators)); |
84 | 84 | }
|
85 |
| - return builder.build(); |
86 |
| - } |
87 | 85 |
|
88 |
| - @Bean |
89 |
| - @ConditionalOnMissingBean |
90 |
| - Sampler sampler(TracingProperties properties) { |
91 |
| - return Sampler.traceIdRatioBased(properties.getSampling().getProbability()); |
| 86 | + @Bean |
| 87 | + @ConditionalOnMissingBean |
| 88 | + Sampler sampler(TracingProperties properties) { |
| 89 | + return Sampler.traceIdRatioBased(properties.getSampling().getProbability()); |
| 90 | + } |
| 91 | + |
| 92 | + @Bean |
| 93 | + @ConditionalOnMissingBean |
| 94 | + SpanProcessor spanProcessor(List<SpanExporter> spanExporter) { |
| 95 | + return SpanProcessor.composite(spanExporter.stream() |
| 96 | + .map(exporter -> BatchSpanProcessor.builder(exporter).build()).collect(Collectors.toSet())); |
| 97 | + } |
92 | 98 | }
|
93 | 99 |
|
94 |
| - @Bean |
95 |
| - @ConditionalOnMissingBean |
96 |
| - SpanProcessor spanProcessor(List<SpanExporter> spanExporter) { |
97 |
| - return SpanProcessor.composite(spanExporter.stream() |
98 |
| - .map(exporter -> BatchSpanProcessor.builder(exporter).build()).collect(Collectors.toSet())); |
| 100 | + @Configuration(proxyBeanMethods = false) |
| 101 | + @Import(Sdk.class) |
| 102 | + @ConditionalOnClass(Tracer.class) |
| 103 | + static class TracerConfiguration { |
| 104 | + |
| 105 | + @Bean |
| 106 | + @ConditionalOnMissingBean |
| 107 | + @ConditionalOnBean(OpenTelemetry.class) |
| 108 | + Tracer tracer(OpenTelemetry openTelemetry) { |
| 109 | + // TODO MH: Decide on a name |
| 110 | + return openTelemetry.getTracer("org.springframework.boot", SpringBootVersion.getVersion()); |
| 111 | + } |
99 | 112 | }
|
100 | 113 |
|
101 | 114 | @Configuration(proxyBeanMethods = false)
|
102 | 115 | @ConditionalOnClass(OtelTracer.class)
|
| 116 | + @Import(TracerConfiguration.class) |
103 | 117 | static class OpenTelemetryMicrometer {
|
104 | 118 |
|
105 | 119 | @Bean
|
106 | 120 | @ConditionalOnMissingBean
|
| 121 | + @ConditionalOnBean(Tracer.class) |
107 | 122 | OtelTracer otelTracer(Tracer tracer, EventPublisher eventPublisher,
|
108 | 123 | OtelCurrentTraceContext otelCurrentTraceContext) {
|
109 | 124 | return new OtelTracer(tracer, otelCurrentTraceContext, eventPublisher,
|
|
0 commit comments