Skip to content

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
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.common.Clock;
import io.opentelemetry.sdk.logs.LogRecordProcessor;
import io.opentelemetry.sdk.logs.SdkLoggerProvider;
import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder;
Expand All @@ -44,6 +45,7 @@
* {@link EnableAutoConfiguration Auto-configuration} for the OpenTelemetry SDK.
*
* @author Moritz Halbritter
* @author Thomas Vitale
* @since 4.0.0
*/
@AutoConfiguration
Expand All @@ -68,6 +70,12 @@ OpenTelemetrySdk openTelemetrySdk(ObjectProvider<SdkTracerProvider> openTelemetr
return builder.build();
}

@Bean
@ConditionalOnMissingBean
Clock clock() {
return Clock.getDefault();
}

@Bean
@ConditionalOnMissingBean
Resource openTelemetryResource(Environment environment, OpenTelemetryProperties properties) {
Expand Down
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);
Copy link
Author

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.

Copy link
Contributor

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.

}
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);
}

}
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

}

}
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
Loading