Skip to content

Commit 771eac8

Browse files
committed
Add auto-configuration for OpenTelemetry SdkMeterProvider
Signed-off-by: Thomas Vitale <[email protected]>
1 parent 0f2e0d6 commit 771eac8

File tree

8 files changed

+451
-0
lines changed

8 files changed

+451
-0
lines changed

module/spring-boot-opentelemetry/src/main/java/org/springframework/boot/opentelemetry/autoconfigure/OpenTelemetrySdkAutoConfiguration.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opentelemetry.context.propagation.ContextPropagators;
2121
import io.opentelemetry.sdk.OpenTelemetrySdk;
2222
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
23+
import io.opentelemetry.sdk.common.Clock;
2324
import io.opentelemetry.sdk.logs.LogRecordProcessor;
2425
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
2526
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
@@ -44,6 +45,7 @@
4445
* {@link EnableAutoConfiguration Auto-configuration} for the OpenTelemetry SDK.
4546
*
4647
* @author Moritz Halbritter
48+
* @author Thomas Vitale
4749
* @since 4.0.0
4850
*/
4951
@AutoConfiguration
@@ -68,6 +70,12 @@ OpenTelemetrySdk openTelemetrySdk(ObjectProvider<SdkTracerProvider> openTelemetr
6870
return builder.build();
6971
}
7072

73+
@Bean
74+
@ConditionalOnMissingBean
75+
Clock clock() {
76+
return Clock.getDefault();
77+
}
78+
7179
@Bean
7280
@ConditionalOnMissingBean
7381
Resource openTelemetryResource(Environment environment, OpenTelemetryProperties properties) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2012-present 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.opentelemetry.autoconfigure.metrics;
18+
19+
import io.opentelemetry.api.OpenTelemetry;
20+
import io.opentelemetry.api.metrics.Meter;
21+
import io.opentelemetry.sdk.common.Clock;
22+
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
23+
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
24+
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
25+
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
26+
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter;
27+
import io.opentelemetry.sdk.resources.Resource;
28+
29+
import org.springframework.beans.factory.ObjectProvider;
30+
import org.springframework.boot.autoconfigure.AutoConfiguration;
31+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
32+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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+
38+
/**
39+
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry Metrics.
40+
*
41+
* @author Thomas Vitale
42+
* @since 4.0.0
43+
*/
44+
@AutoConfiguration
45+
@ConditionalOnClass(SdkMeterProvider.class)
46+
@EnableConfigurationProperties(OpenTelemetryMetricsProperties.class)
47+
public final class OpenTelemetryMetricsAutoConfiguration {
48+
49+
static final String INSTRUMENTATION_SCOPE_NAME = "org.springframework.boot";
50+
51+
@Bean
52+
@ConditionalOnMissingBean
53+
@ConditionalOnBean({ Clock.class, Resource.class })
54+
SdkMeterProvider meterProvider(Clock clock, ExemplarFilter exemplarFilter,
55+
OpenTelemetryMetricsProperties properties, Resource resource,
56+
ObjectProvider<SdkMeterProviderBuilderCustomizer> customizers) {
57+
SdkMeterProviderBuilder builder = SdkMeterProvider.builder().setClock(clock).setResource(resource);
58+
if (properties.getExemplars().isEnabled()) {
59+
SdkMeterProviderUtil.setExemplarFilter(builder, exemplarFilter);
60+
}
61+
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder));
62+
return builder.build();
63+
}
64+
65+
@Bean
66+
@ConditionalOnMissingBean
67+
CardinalityLimitSelector cardinalityLimitSelector(OpenTelemetryMetricsProperties properties) {
68+
return (instrumentType) -> properties.getCardinalityLimit();
69+
}
70+
71+
@Bean
72+
@ConditionalOnMissingBean
73+
ExemplarFilter exemplarFilter(OpenTelemetryMetricsProperties properties) {
74+
return switch (properties.getExemplars().getFilter()) {
75+
case ALWAYS_ON -> ExemplarFilter.alwaysOn();
76+
case ALWAYS_OFF -> ExemplarFilter.alwaysOff();
77+
case TRACE_BASED -> ExemplarFilter.traceBased();
78+
};
79+
}
80+
81+
@Bean
82+
@ConditionalOnMissingBean
83+
@ConditionalOnBean(OpenTelemetry.class)
84+
Meter meter(OpenTelemetry openTelemetry) {
85+
return openTelemetry.getMeter(INSTRUMENTATION_SCOPE_NAME);
86+
}
87+
88+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2012-present 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.opentelemetry.autoconfigure.metrics;
18+
19+
import org.springframework.boot.context.properties.ConfigurationProperties;
20+
21+
/**
22+
* Configuration properties for OpenTelemetry Metrics.
23+
*
24+
* @author Thomas Vitale
25+
* @since 4.0.0
26+
*/
27+
@ConfigurationProperties("management.opentelemetry.metrics")
28+
public class OpenTelemetryMetricsProperties {
29+
30+
/**
31+
* Configuration for exemplars.
32+
*/
33+
private final Exemplars exemplars = new Exemplars();
34+
35+
/**
36+
* Maximum number of distinct points per metric.
37+
*/
38+
private Integer cardinalityLimit = 2000;
39+
40+
public Exemplars getExemplars() {
41+
return this.exemplars;
42+
}
43+
44+
public Integer getCardinalityLimit() {
45+
return this.cardinalityLimit;
46+
}
47+
48+
public void setCardinalityLimit(Integer cardinalityLimit) {
49+
this.cardinalityLimit = cardinalityLimit;
50+
}
51+
52+
/**
53+
* Configuration properties for exemplars.
54+
*/
55+
public static class Exemplars {
56+
57+
/**
58+
* Whether exemplars should be enabled.
59+
*/
60+
private boolean enabled = false;
61+
62+
/**
63+
* Determines which measurements are eligible to become Exemplars.
64+
*/
65+
private ExemplarFilter filter = ExemplarFilter.TRACE_BASED;
66+
67+
public boolean isEnabled() {
68+
return this.enabled;
69+
}
70+
71+
public void setEnabled(boolean enabled) {
72+
this.enabled = enabled;
73+
}
74+
75+
public ExemplarFilter getFilter() {
76+
return this.filter;
77+
}
78+
79+
public void setFilter(ExemplarFilter filter) {
80+
this.filter = filter;
81+
}
82+
83+
}
84+
85+
/**
86+
* Filter for which measurements are eligible to become Exemplars.
87+
*/
88+
public enum ExemplarFilter {
89+
90+
/**
91+
* Filter which makes all measurements eligible for being an exemplar.
92+
*/
93+
ALWAYS_ON,
94+
95+
/**
96+
* Filter which makes no measurements eligible for being an exemplar.
97+
*/
98+
ALWAYS_OFF,
99+
100+
/**
101+
* Filter that only accepts measurements where there is a span in context that is
102+
* being sampled.
103+
*/
104+
TRACE_BASED
105+
106+
}
107+
108+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2012-present 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.opentelemetry.autoconfigure.metrics;
18+
19+
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
20+
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
21+
22+
/**
23+
* Callback that can be used to customize the {@link SdkMeterProviderBuilder} used to
24+
* build the auto-configured {@link SdkMeterProvider}.
25+
*
26+
* @author Thomas Vitale
27+
* @since 4.0.0
28+
*/
29+
@FunctionalInterface
30+
public interface SdkMeterProviderBuilderCustomizer {
31+
32+
/**
33+
* Customize the given {@code builder}.
34+
* @param builder the builder to customize
35+
*/
36+
void customize(SdkMeterProviderBuilder builder);
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-present 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 Metrics.
19+
*/
20+
package org.springframework.boot.opentelemetry.autoconfigure.metrics;
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
org.springframework.boot.opentelemetry.autoconfigure.OpenTelemetrySdkAutoConfiguration
22
org.springframework.boot.opentelemetry.autoconfigure.logging.OpenTelemetryLoggingExportAutoConfiguration
3+
org.springframework.boot.opentelemetry.autoconfigure.metrics.OpenTelemetryMetricsAutoConfiguration

0 commit comments

Comments
 (0)