Skip to content

Commit 6d14596

Browse files
committed
Split OpenTelemetry auto-configuration
The OpenTelemetry bean is now configured in the OpenTelemetryAutoConfiguration. This method also applies SdkLoggerProvider and SdkMeterProvider. Additionally, the OpenTelemetry Resource is now a bean. Resource attributes can now be configured through properties The resourceAttributes in OtlpProperties have been deprecated in favor of the new one in OpenTelemetryProperties. Closes spring-projectsgh-36544 Closes spring-projectsgh-36545
1 parent 346ebbc commit 6d14596

File tree

15 files changed

+413
-71
lines changed

15 files changed

+413
-71
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/otlp/OtlpMetricsExportAutoConfiguration.java

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
2424
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
2525
import org.springframework.boot.actuate.autoconfigure.metrics.export.ConditionalOnEnabledMetricsExport;
2626
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
27+
import org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryProperties;
2728
import org.springframework.boot.autoconfigure.AutoConfiguration;
2829
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2930
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@@ -36,6 +37,7 @@
3637
* {@link EnableAutoConfiguration Auto-configuration} for exporting metrics to OTLP.
3738
*
3839
* @author Eddú Meléndez
40+
* @author Moritz Halbritter
3941
* @since 3.0.0
4042
*/
4143
@AutoConfiguration(
@@ -44,19 +46,23 @@
4446
@ConditionalOnBean(Clock.class)
4547
@ConditionalOnClass(OtlpMeterRegistry.class)
4648
@ConditionalOnEnabledMetricsExport("otlp")
47-
@EnableConfigurationProperties(OtlpProperties.class)
49+
@EnableConfigurationProperties({ OtlpProperties.class, OpenTelemetryProperties.class })
4850
public class OtlpMetricsExportAutoConfiguration {
4951

5052
private final OtlpProperties properties;
5153

52-
public OtlpMetricsExportAutoConfiguration(OtlpProperties properties) {
54+
private final OpenTelemetryProperties openTelemetryProperties;
55+
56+
public OtlpMetricsExportAutoConfiguration(OtlpProperties properties,
57+
OpenTelemetryProperties openTelemetryProperties) {
5358
this.properties = properties;
59+
this.openTelemetryProperties = openTelemetryProperties;
5460
}
5561

5662
@Bean
5763
@ConditionalOnMissingBean
5864
public OtlpConfig otlpConfig() {
59-
return new OtlpPropertiesConfigAdapter(this.properties);
65+
return new OtlpPropertiesConfigAdapter(this.properties, this.openTelemetryProperties);
6066
}
6167

6268
@Bean

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/otlp/OtlpProperties.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryProperties;
2525
import org.springframework.boot.context.properties.ConfigurationProperties;
26+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2627

2728
/**
2829
* {@link ConfigurationProperties @ConfigurationProperties} for configuring OTLP metrics
@@ -77,6 +78,7 @@ public void setAggregationTemporality(AggregationTemporality aggregationTemporal
7778
this.aggregationTemporality = aggregationTemporality;
7879
}
7980

81+
@DeprecatedConfigurationProperty(replacement = "management.opentelemetry.resource-attributes")
8082
public Map<String, String> getResourceAttributes() {
8183
return this.resourceAttributes;
8284
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/export/otlp/OtlpPropertiesConfigAdapter.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,23 @@
2323
import io.micrometer.registry.otlp.OtlpConfig;
2424

2525
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.StepRegistryPropertiesConfigAdapter;
26+
import org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryProperties;
27+
import org.springframework.util.CollectionUtils;
2628

2729
/**
2830
* Adapter to convert {@link OtlpProperties} to an {@link OtlpConfig}.
2931
*
3032
* @author Eddú Meléndez
3133
* @author Jonatan Ivanov
34+
* @author Moritz Halbritter
3235
*/
3336
class OtlpPropertiesConfigAdapter extends StepRegistryPropertiesConfigAdapter<OtlpProperties> implements OtlpConfig {
3437

35-
OtlpPropertiesConfigAdapter(OtlpProperties properties) {
38+
private final OpenTelemetryProperties openTelemetryProperties;
39+
40+
OtlpPropertiesConfigAdapter(OtlpProperties properties, OpenTelemetryProperties openTelemetryProperties) {
3641
super(properties);
42+
this.openTelemetryProperties = openTelemetryProperties;
3743
}
3844

3945
@Override
@@ -53,6 +59,9 @@ public AggregationTemporality aggregationTemporality() {
5359

5460
@Override
5561
public Map<String, String> resourceAttributes() {
62+
if (!CollectionUtils.isEmpty(this.openTelemetryProperties.getResourceAttributes())) {
63+
return this.openTelemetryProperties.getResourceAttributes();
64+
}
5665
return get(OtlpProperties::getResourceAttributes, OtlpConfig.super::resourceAttributes);
5766
}
5867

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.opentelemetry;
18+
19+
import io.opentelemetry.api.OpenTelemetry;
20+
import io.opentelemetry.api.common.Attributes;
21+
import io.opentelemetry.context.propagation.ContextPropagators;
22+
import io.opentelemetry.sdk.OpenTelemetrySdk;
23+
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
24+
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
25+
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
26+
import io.opentelemetry.sdk.resources.Resource;
27+
import io.opentelemetry.sdk.trace.SdkTracerProvider;
28+
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
29+
30+
import org.springframework.beans.factory.ObjectProvider;
31+
import org.springframework.boot.autoconfigure.AutoConfiguration;
32+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
33+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
34+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
35+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
36+
import org.springframework.context.annotation.Bean;
37+
import org.springframework.core.env.Environment;
38+
39+
/**
40+
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry.
41+
*
42+
* @author Moritz Halbritter
43+
* @since 3.2.0
44+
*/
45+
@AutoConfiguration
46+
@ConditionalOnClass(OpenTelemetrySdk.class)
47+
@EnableConfigurationProperties(OpenTelemetryProperties.class)
48+
public class OpenTelemetryInfrastructureAutoConfiguration {
49+
50+
/**
51+
* Default value for application name if {@code spring.application.name} is not set.
52+
*/
53+
private static final String DEFAULT_APPLICATION_NAME = "application";
54+
55+
@Bean
56+
@ConditionalOnMissingBean(OpenTelemetry.class)
57+
OpenTelemetrySdk openTelemetry(ObjectProvider<SdkTracerProvider> tracerProvider,
58+
ObjectProvider<ContextPropagators> propagators, ObjectProvider<SdkLoggerProvider> loggerProvider,
59+
ObjectProvider<SdkMeterProvider> meterProvider) {
60+
OpenTelemetrySdkBuilder builder = OpenTelemetrySdk.builder();
61+
tracerProvider.ifAvailable(builder::setTracerProvider);
62+
propagators.ifAvailable(builder::setPropagators);
63+
loggerProvider.ifAvailable(builder::setLoggerProvider);
64+
meterProvider.ifAvailable(builder::setMeterProvider);
65+
return builder.build();
66+
}
67+
68+
@Bean
69+
@ConditionalOnMissingBean
70+
Resource openTelemetryResource(Environment environment, OpenTelemetryProperties properties) {
71+
String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
72+
return Resource.getDefault()
73+
.merge(Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName)))
74+
.merge(properties.toResource());
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.autoconfigure.opentelemetry;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Map.Entry;
22+
23+
import io.opentelemetry.sdk.resources.Resource;
24+
import io.opentelemetry.sdk.resources.ResourceBuilder;
25+
26+
import org.springframework.boot.context.properties.ConfigurationProperties;
27+
28+
/**
29+
* Configuration properties for OpenTelemetry.
30+
*
31+
* @author Moritz Halbritter
32+
* @since 3.2.0
33+
*/
34+
@ConfigurationProperties(prefix = "management.opentelemetry")
35+
public class OpenTelemetryProperties {
36+
37+
/**
38+
* Resource attributes.
39+
*/
40+
private Map<String, String> resourceAttributes = new HashMap<>();
41+
42+
public Map<String, String> getResourceAttributes() {
43+
return this.resourceAttributes;
44+
}
45+
46+
public void setResourceAttributes(Map<String, String> resourceAttributes) {
47+
this.resourceAttributes = resourceAttributes;
48+
}
49+
50+
Resource toResource() {
51+
ResourceBuilder builder = Resource.builder();
52+
for (Entry<String, String> entry : this.resourceAttributes.entrySet()) {
53+
builder.put(entry.getKey(), entry.getValue());
54+
}
55+
return builder.build();
56+
}
57+
58+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Auto-configuration for OpenTelemetry.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.opentelemetry;

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/tracing/OpenTelemetryAutoConfiguration.java

+3-25
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@
3636
import io.micrometer.tracing.otel.bridge.Slf4JEventListener;
3737
import io.micrometer.tracing.otel.propagation.BaggageTextMapPropagator;
3838
import io.opentelemetry.api.OpenTelemetry;
39-
import io.opentelemetry.api.common.Attributes;
4039
import io.opentelemetry.api.metrics.MeterProvider;
4140
import io.opentelemetry.api.trace.Tracer;
4241
import io.opentelemetry.context.ContextStorage;
4342
import io.opentelemetry.context.propagation.ContextPropagators;
4443
import io.opentelemetry.context.propagation.TextMapPropagator;
45-
import io.opentelemetry.sdk.OpenTelemetrySdk;
4644
import io.opentelemetry.sdk.resources.Resource;
4745
import io.opentelemetry.sdk.trace.SdkTracerProvider;
4846
import io.opentelemetry.sdk.trace.SdkTracerProviderBuilder;
@@ -51,7 +49,6 @@
5149
import io.opentelemetry.sdk.trace.export.BatchSpanProcessorBuilder;
5250
import io.opentelemetry.sdk.trace.export.SpanExporter;
5351
import io.opentelemetry.sdk.trace.samplers.Sampler;
54-
import io.opentelemetry.semconv.resource.attributes.ResourceAttributes;
5552

5653
import org.springframework.beans.factory.ObjectProvider;
5754
import org.springframework.boot.SpringBootVersion;
@@ -63,10 +60,9 @@
6360
import org.springframework.boot.context.properties.EnableConfigurationProperties;
6461
import org.springframework.context.annotation.Bean;
6562
import org.springframework.context.annotation.Configuration;
66-
import org.springframework.core.env.Environment;
6763

6864
/**
69-
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry.
65+
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry tracing.
7066
*
7167
* @author Moritz Halbritter
7268
* @author Marcin Grzejszczak
@@ -78,11 +74,6 @@
7874
@EnableConfigurationProperties(TracingProperties.class)
7975
public class OpenTelemetryAutoConfiguration {
8076

81-
/**
82-
* Default value for application name if {@code spring.application.name} is not set.
83-
*/
84-
private static final String DEFAULT_APPLICATION_NAME = "application";
85-
8677
private final TracingProperties tracingProperties;
8778

8879
OpenTelemetryAutoConfiguration(TracingProperties tracingProperties) {
@@ -91,22 +82,9 @@ public class OpenTelemetryAutoConfiguration {
9182

9283
@Bean
9384
@ConditionalOnMissingBean
94-
OpenTelemetry openTelemetry(SdkTracerProvider sdkTracerProvider, ContextPropagators contextPropagators) {
95-
return OpenTelemetrySdk.builder()
96-
.setTracerProvider(sdkTracerProvider)
97-
.setPropagators(contextPropagators)
98-
.build();
99-
}
100-
101-
@Bean
102-
@ConditionalOnMissingBean
103-
SdkTracerProvider otelSdkTracerProvider(Environment environment, SpanProcessors spanProcessors, Sampler sampler,
85+
SdkTracerProvider otelSdkTracerProvider(Resource resource, SpanProcessors spanProcessors, Sampler sampler,
10486
ObjectProvider<SdkTracerProviderBuilderCustomizer> customizers) {
105-
String applicationName = environment.getProperty("spring.application.name", DEFAULT_APPLICATION_NAME);
106-
Resource springResource = Resource.create(Attributes.of(ResourceAttributes.SERVICE_NAME, applicationName));
107-
SdkTracerProviderBuilder builder = SdkTracerProvider.builder()
108-
.setSampler(sampler)
109-
.setResource(Resource.getDefault().merge(springResource));
87+
SdkTracerProviderBuilder builder = SdkTracerProvider.builder().setSampler(sampler).setResource(resource);
11088
spanProcessors.forEach(builder::addSpanProcessor);
11189
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
11290
return builder.build();

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports

+2-1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ org.springframework.boot.actuate.autoconfigure.data.mongo.MongoReactiveHealthCon
8888
org.springframework.boot.actuate.autoconfigure.neo4j.Neo4jHealthContributorAutoConfiguration
8989
org.springframework.boot.actuate.autoconfigure.observation.ObservationAutoConfiguration
9090
org.springframework.boot.actuate.autoconfigure.observation.web.servlet.WebMvcObservationAutoConfiguration
91+
org.springframework.boot.actuate.autoconfigure.opentelemetry.OpenTelemetryInfrastructureAutoConfiguration
9192
org.springframework.boot.actuate.autoconfigure.quartz.QuartzEndpointAutoConfiguration
9293
org.springframework.boot.actuate.autoconfigure.r2dbc.ConnectionFactoryHealthContributorAutoConfiguration
9394
org.springframework.boot.actuate.autoconfigure.data.redis.RedisHealthContributorAutoConfiguration
@@ -112,4 +113,4 @@ org.springframework.boot.actuate.autoconfigure.web.exchanges.HttpExchangesEndpoi
112113
org.springframework.boot.actuate.autoconfigure.web.mappings.MappingsEndpointAutoConfiguration
113114
org.springframework.boot.actuate.autoconfigure.web.reactive.ReactiveManagementContextAutoConfiguration
114115
org.springframework.boot.actuate.autoconfigure.web.server.ManagementContextAutoConfiguration
115-
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration
116+
org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration

0 commit comments

Comments
 (0)