-
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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.ConditionalOnBean; | ||
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 | ||
@ConditionalOnBean({ Clock.class, Resource.class }) | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. That's fine for me. |
||
} | ||
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 | ||
@ConditionalOnBean(OpenTelemetry.class) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same ordering issue. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed |
||
Meter meter(OpenTelemetry openTelemetry) { | ||
return openTelemetry.getMeter(INSTRUMENTATION_SCOPE_NAME); | ||
} | ||
|
||
} |
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 Integer cardinalityLimit = 2000; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's true. I'll change it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
|
||
public Exemplars getExemplars() { | ||
return this.exemplars; | ||
} | ||
|
||
public Integer getCardinalityLimit() { | ||
return this.cardinalityLimit; | ||
} | ||
|
||
public void setCardinalityLimit(Integer 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 | ||
|
||
} | ||
|
||
} |
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); | ||
|
||
} |
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; |
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 |
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.
This won't work reliably without auto-configuration ordering, e.g.
The logging auto-configuration doesn't have those conditionals, so i think it's okay to drop them here too.
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.
Ok, I'll remove all those conditionals
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.
Removed