-
Notifications
You must be signed in to change notification settings - Fork 41.4k
Add auto-configuration for OpenTelemetry SdkMeterProvider #46640
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
Open
ThomasVitale
wants to merge
2
commits into
spring-projects:main
Choose a base branch
from
ThomasVitale:gh-36546
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...ework/boot/opentelemetry/autoconfigure/metrics/OpenTelemetryMetricsAutoConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2012-present the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.opentelemetry.autoconfigure.metrics; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.metrics.Meter; | ||
import io.opentelemetry.sdk.common.Clock; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; | ||
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector; | ||
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil; | ||
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter; | ||
import io.opentelemetry.sdk.resources.Resource; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for OpenTelemetry Metrics. | ||
* | ||
* @author Thomas Vitale | ||
* @since 4.0.0 | ||
*/ | ||
@AutoConfiguration | ||
@ConditionalOnClass(SdkMeterProvider.class) | ||
@EnableConfigurationProperties(OpenTelemetryMetricsProperties.class) | ||
public final class OpenTelemetryMetricsAutoConfiguration { | ||
|
||
static final String INSTRUMENTATION_SCOPE_NAME = "org.springframework.boot"; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
SdkMeterProvider meterProvider(Clock clock, ExemplarFilter exemplarFilter, | ||
OpenTelemetryMetricsProperties properties, Resource resource, | ||
ObjectProvider<SdkMeterProviderBuilderCustomizer> customizers) { | ||
SdkMeterProviderBuilder builder = SdkMeterProvider.builder().setClock(clock).setResource(resource); | ||
if (properties.getExemplars().isEnabled()) { | ||
SdkMeterProviderUtil.setExemplarFilter(builder, exemplarFilter); | ||
} | ||
customizers.orderedStream().forEach((customizer) -> customizer.customize(builder)); | ||
return builder.build(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
CardinalityLimitSelector cardinalityLimitSelector(OpenTelemetryMetricsProperties properties) { | ||
return (instrumentType) -> properties.getCardinalityLimit(); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
ExemplarFilter exemplarFilter(OpenTelemetryMetricsProperties properties) { | ||
return switch (properties.getExemplars().getFilter()) { | ||
case ALWAYS_ON -> ExemplarFilter.alwaysOn(); | ||
case ALWAYS_OFF -> ExemplarFilter.alwaysOff(); | ||
case TRACE_BASED -> ExemplarFilter.traceBased(); | ||
}; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
Meter meter(OpenTelemetry openTelemetry) { | ||
return openTelemetry.getMeter(INSTRUMENTATION_SCOPE_NAME); | ||
} | ||
|
||
} |
108 changes: 108 additions & 0 deletions
108
...ingframework/boot/opentelemetry/autoconfigure/metrics/OpenTelemetryMetricsProperties.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Copyright 2012-present the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.opentelemetry.autoconfigure.metrics; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* Configuration properties for OpenTelemetry Metrics. | ||
* | ||
* @author Thomas Vitale | ||
* @since 4.0.0 | ||
*/ | ||
@ConfigurationProperties("management.opentelemetry.metrics") | ||
public class OpenTelemetryMetricsProperties { | ||
|
||
/** | ||
* Configuration for exemplars. | ||
*/ | ||
private final Exemplars exemplars = new Exemplars(); | ||
|
||
/** | ||
* Maximum number of distinct points per metric. | ||
*/ | ||
private int cardinalityLimit = 2000; | ||
|
||
public Exemplars getExemplars() { | ||
return this.exemplars; | ||
} | ||
|
||
public int getCardinalityLimit() { | ||
return this.cardinalityLimit; | ||
} | ||
|
||
public void setCardinalityLimit(int cardinalityLimit) { | ||
this.cardinalityLimit = cardinalityLimit; | ||
} | ||
|
||
/** | ||
* Configuration properties for exemplars. | ||
*/ | ||
public static class Exemplars { | ||
|
||
/** | ||
* Whether exemplars should be enabled. | ||
*/ | ||
private boolean enabled = false; | ||
|
||
/** | ||
* Determines which measurements are eligible to become Exemplars. | ||
*/ | ||
private ExemplarFilter filter = ExemplarFilter.TRACE_BASED; | ||
|
||
public boolean isEnabled() { | ||
return this.enabled; | ||
} | ||
|
||
public void setEnabled(boolean enabled) { | ||
this.enabled = enabled; | ||
} | ||
|
||
public ExemplarFilter getFilter() { | ||
return this.filter; | ||
} | ||
|
||
public void setFilter(ExemplarFilter filter) { | ||
this.filter = filter; | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Filter for which measurements are eligible to become Exemplars. | ||
*/ | ||
public enum ExemplarFilter { | ||
|
||
/** | ||
* Filter which makes all measurements eligible for being an exemplar. | ||
*/ | ||
ALWAYS_ON, | ||
|
||
/** | ||
* Filter which makes no measurements eligible for being an exemplar. | ||
*/ | ||
ALWAYS_OFF, | ||
|
||
/** | ||
* Filter that only accepts measurements where there is a span in context that is | ||
* being sampled. | ||
*/ | ||
TRACE_BASED | ||
|
||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
...framework/boot/opentelemetry/autoconfigure/metrics/SdkMeterProviderBuilderCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2012-present the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.opentelemetry.autoconfigure.metrics; | ||
|
||
import io.opentelemetry.sdk.metrics.SdkMeterProvider; | ||
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder; | ||
|
||
/** | ||
* Callback that can be used to customize the {@link SdkMeterProviderBuilder} used to | ||
* build the auto-configured {@link SdkMeterProvider}. | ||
* | ||
* @author Thomas Vitale | ||
* @since 4.0.0 | ||
*/ | ||
@FunctionalInterface | ||
public interface SdkMeterProviderBuilderCustomizer { | ||
|
||
/** | ||
* Customize the given {@code builder}. | ||
* @param builder the builder to customize | ||
*/ | ||
void customize(SdkMeterProviderBuilder builder); | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
.../main/java/org/springframework/boot/opentelemetry/autoconfigure/metrics/package-info.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2012-present the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Auto-configuration for OpenTelemetry Metrics. | ||
*/ | ||
package org.springframework.boot.opentelemetry.autoconfigure.metrics; |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
org.springframework.boot.opentelemetry.autoconfigure.OpenTelemetrySdkAutoConfiguration | ||
org.springframework.boot.opentelemetry.autoconfigure.logging.OpenTelemetryLoggingExportAutoConfiguration | ||
org.springframework.boot.opentelemetry.autoconfigure.metrics.OpenTelemetryMetricsAutoConfiguration |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exemplars are disabled by default because even though the Spec/API is GA, the method in the Java SDK to configure them is not exposed publicly, but through this util. Is that acceptable for Spring Boot?
The API (mapped closely by the configuration properties) won't change as it's stable. So, any change to the SDK internals will not require any code/configuration change from Spring Boot users (a.k.a. stable feature). See: https://opentelemetry.io/docs/specs/otel/metrics/sdk/#exemplar (stable API). More context: open-telemetry/opentelemetry-java#7503.
My recommendation would be to keep this functionality, but disabled by default. Considering that the OTLP Meter Registry doesn't support exemplars yet, this would be the only way in Spring Boot to support exemplars in OpenTelemetry.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine for me.