Skip to content
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

Stabilize metric cardinality limits APIs #6794

Merged
Merged
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
16 changes: 15 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-sdk-metrics.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,16 @@
Comparing source compatibility of opentelemetry-sdk-metrics-1.44.0-SNAPSHOT.jar against opentelemetry-sdk-metrics-1.43.0.jar
No changes.
+++ NEW INTERFACE: PUBLIC(+) ABSTRACT(+) io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector (not serializable)
+++ CLASS FILE FORMAT VERSION: 52.0 <- n.a.
+++ NEW SUPERCLASS: java.lang.Object
+++ NEW METHOD: PUBLIC(+) STATIC(+) io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector defaultCardinalityLimitSelector()
+++ NEW METHOD: PUBLIC(+) ABSTRACT(+) int getCardinalityLimit(io.opentelemetry.sdk.metrics.InstrumentType)
+++ NEW ANNOTATION: java.lang.FunctionalInterface
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder registerMetricReader(io.opentelemetry.sdk.metrics.export.MetricReader, io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector)
*** MODIFIED CLASS: PUBLIC ABSTRACT io.opentelemetry.sdk.metrics.View (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
*** MODIFIED METHOD: PUBLIC (<- PACKAGE_PROTECTED) ABSTRACT int getCardinalityLimit()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.sdk.metrics.ViewBuilder (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
*** MODIFIED METHOD: PUBLIC (<- PACKAGE_PROTECTED) io.opentelemetry.sdk.metrics.ViewBuilder setCardinalityLimit(int)
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ static void configureMeterProvider(
config, spiHelper, metricReaderCustomizer, metricExporterCustomizer, closeables)
.forEach(
reader ->
SdkMeterProviderUtil.registerMetricReaderWithCardinalitySelector(
meterProviderBuilder, reader, instrumentType -> cardinalityLimit));
meterProviderBuilder.registerMetricReader(
reader, instrumentType -> cardinalityLimit));
}

static List<MetricReader> configureMetricReaders(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,8 @@ public void setup() {
// Effectively disable periodic reading so reading is only done on #flush()
.setInterval(Duration.ofSeconds(Integer.MAX_VALUE))
.build();
SdkMeterProviderBuilder builder = SdkMeterProvider.builder();
SdkMeterProviderUtil.registerMetricReaderWithCardinalitySelector(
builder, metricReader, unused -> cardinality + 1);
SdkMeterProviderBuilder builder =
SdkMeterProvider.builder().registerMetricReader(metricReader, unused -> cardinality + 1);

attributesList = AttributesGenerator.generate(cardinality);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import io.opentelemetry.sdk.internal.ComponentRegistry;
import io.opentelemetry.sdk.internal.ScopeConfigurator;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.export.CollectionRegistration;
import io.opentelemetry.sdk.metrics.export.MetricProducer;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.MeterConfig;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.internal.export.RegisteredReader;
import io.opentelemetry.sdk.metrics.internal.state.MeterProviderSharedState;
import io.opentelemetry.sdk.metrics.internal.view.RegisteredView;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.internal.ScopeConfigurator;
import io.opentelemetry.sdk.internal.ScopeConfiguratorBuilder;
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.export.MetricProducer;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.MeterConfig;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.metrics.internal.debug.SourceInfo;
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.internal.view.RegisteredView;
import io.opentelemetry.sdk.resources.Resource;
import java.util.ArrayList;
Expand Down Expand Up @@ -137,15 +137,10 @@ public SdkMeterProviderBuilder registerMetricReader(MetricReader reader) {
/**
* Registers a {@link MetricReader} with a {@link CardinalityLimitSelector}.
*
* <p>This method is experimental so not public. You may reflectively call it using {@link
* SdkMeterProviderUtil#registerMetricReaderWithCardinalitySelector(SdkMeterProviderBuilder,
* MetricReader, CardinalityLimitSelector)}
*
* <p>Note: not currently stable but available for experimental use via {@link
* SdkMeterProviderUtil#registerMetricReaderWithCardinalitySelector(SdkMeterProviderBuilder,
* MetricReader, CardinalityLimitSelector)}.
* <p>If {@link #registerMetricReader(MetricReader)} is used, the {@link
* CardinalityLimitSelector#defaultCardinalityLimitSelector()} is used.
*/
SdkMeterProviderBuilder registerMetricReader(
public SdkMeterProviderBuilder registerMetricReader(
MetricReader reader, CardinalityLimitSelector cardinalityLimitSelector) {
metricReaders.put(reader, cardinalityLimitSelector);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ static View create(
abstract AttributesProcessor getAttributesProcessor();

/** Returns the cardinality limit for this view. */
abstract int getCardinalityLimit();
public abstract int getCardinalityLimit();

@Override
public final String toString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,12 @@ ViewBuilder addAttributesProcessor(AttributesProcessor attributesProcessor) {
/**
* Set the cardinality limit.
*
* <p>This method is experimental so not public. You may reflectively call it using {@link
* SdkMeterProviderUtil#setCardinalityLimit(ViewBuilder, int)}
*
* <p>Note: not currently stable but cardinality limit can be configured via
* SdkMeterProviderUtil#setCardinalityLimit(ViewBuilder, int).
*
* <p>Read {@link MemoryMode} to understand the memory usage behavior of reaching cardinality
* limit.
*
* @param cardinalityLimit the maximum number of series for a metric
*/
ViewBuilder setCardinalityLimit(int cardinalityLimit) {
public ViewBuilder setCardinalityLimit(int cardinalityLimit) {
if (cardinalityLimit <= 0) {
throw new IllegalArgumentException("cardinalityLimit must be > 0");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,16 @@
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.internal.export;
package io.opentelemetry.sdk.metrics.export;

import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.metrics.internal.state.MetricStorage;

/**
* Customize the {@link io.opentelemetry.sdk.metrics.export.MetricReader} cardinality limit as a
* function of {@link InstrumentType}. Register via {@link
* SdkMeterProviderUtil#registerMetricReaderWithCardinalitySelector(SdkMeterProviderBuilder,
* MetricReader, CardinalityLimitSelector)}.
* SdkMeterProviderBuilder#registerMetricReader(MetricReader, CardinalityLimitSelector)}.
*
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
* at any time.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.ViewBuilder;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.exemplar.ExemplarFilter;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.internal.view.AttributesProcessor;
import io.opentelemetry.sdk.metrics.internal.view.StringPredicates;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -49,28 +47,6 @@ public static void setExemplarFilter(
}
}

/**
* Reflectively add a {@link MetricReader} with the {@link CardinalityLimitSelector} to the {@link
* SdkMeterProviderBuilder}.
*
* @param sdkMeterProviderBuilder the builder
*/
public static void registerMetricReaderWithCardinalitySelector(
SdkMeterProviderBuilder sdkMeterProviderBuilder,
MetricReader metricReader,
CardinalityLimitSelector cardinalityLimitSelector) {
try {
Method method =
SdkMeterProviderBuilder.class.getDeclaredMethod(
"registerMetricReader", MetricReader.class, CardinalityLimitSelector.class);
method.setAccessible(true);
method.invoke(sdkMeterProviderBuilder, metricReader, cardinalityLimitSelector);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(
"Error calling addMetricReader on SdkMeterProviderBuilder", e);
}
}

/** Reflectively set the {@link ScopeConfigurator} to the {@link SdkMeterProviderBuilder}. */
public static void setMeterConfigurator(
SdkMeterProviderBuilder sdkMeterProviderBuilder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.SdkMeterProviderBuilder;
import io.opentelemetry.sdk.metrics.View;
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.internal.aggregator.AggregationUtil;
import io.opentelemetry.sdk.metrics.internal.aggregator.AggregatorFactory;
import io.opentelemetry.sdk.metrics.internal.debug.SourceInfo;
import io.opentelemetry.sdk.metrics.internal.descriptor.Advice;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.internal.state.MetricStorage;
import java.util.ArrayList;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
import io.opentelemetry.sdk.metrics.data.LongPointData;
import io.opentelemetry.sdk.metrics.data.PointData;
import io.opentelemetry.sdk.metrics.data.SumData;
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.export.MetricReader;
import io.opentelemetry.sdk.metrics.internal.SdkMeterProviderUtil;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.internal.state.DefaultSynchronousMetricStorage;
import io.opentelemetry.sdk.metrics.internal.state.MetricStorage;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
Expand Down Expand Up @@ -315,28 +314,24 @@ void readerAndViewCardinalityConfiguration() {
// other instrument kinds
CardinalityLimitSelector cardinalityLimitSelector =
instrumentType -> instrumentType == InstrumentType.COUNTER ? counterLimit : generalLimit;
SdkMeterProviderBuilder builder = SdkMeterProvider.builder();

// Register both the delta and cumulative reader with the customized cardinality selector
SdkMeterProviderUtil.registerMetricReaderWithCardinalitySelector(
builder, deltaReader, cardinalityLimitSelector);
SdkMeterProviderUtil.registerMetricReaderWithCardinalitySelector(
builder, cumulativeReader, cardinalityLimitSelector);

// Register a view which defines a custom cardinality limit for instrumented named "counter2"
ViewBuilder viewBuilder1 = View.builder();
SdkMeterProviderUtil.setCardinalityLimit(viewBuilder1, counter2Limit);
builder.registerView(
InstrumentSelector.builder().setName("counter2").build(), viewBuilder1.build());

// Register a view which defines a custom cardinality limit for instrumented named
// "asyncCounter"
ViewBuilder viewBuilder2 = View.builder();
SdkMeterProviderUtil.setCardinalityLimit(viewBuilder2, asyncCounterLimit);
builder.registerView(
InstrumentSelector.builder().setName("asyncCounter").build(), viewBuilder2.build());
SdkMeterProvider sdkMeterProvider =
SdkMeterProvider.builder()
// Register both the delta and cumulative reader with the customized cardinality
// selector
.registerMetricReader(deltaReader, cardinalityLimitSelector)
.registerMetricReader(cumulativeReader, cardinalityLimitSelector)
// Register a view which defines a custom cardinality limit for instrumented named
// "counter2"
.registerView(
InstrumentSelector.builder().setName("counter2").build(),
View.builder().setCardinalityLimit(counter2Limit).build())
// Register a view which defines a custom cardinality limit for instrumented named
// "asyncCounter"
.registerView(
InstrumentSelector.builder().setName("asyncCounter").build(),
View.builder().setCardinalityLimit(asyncCounterLimit).build())
.build();

SdkMeterProvider sdkMeterProvider = builder.build();
meter = sdkMeterProvider.get(CardinalityTest.class.getName());

LongCounter counter1 = meter.counterBuilder("counter1").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
import io.opentelemetry.sdk.metrics.InstrumentType;
import io.opentelemetry.sdk.metrics.InstrumentValueType;
import io.opentelemetry.sdk.metrics.View;
import io.opentelemetry.sdk.metrics.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.export.DefaultAggregationSelector;
import io.opentelemetry.sdk.metrics.internal.debug.SourceInfo;
import io.opentelemetry.sdk.metrics.internal.descriptor.Advice;
import io.opentelemetry.sdk.metrics.internal.descriptor.InstrumentDescriptor;
import io.opentelemetry.sdk.metrics.internal.export.CardinalityLimitSelector;
import io.opentelemetry.sdk.metrics.internal.state.MetricStorage;
import java.util.Arrays;
import java.util.Collections;
Expand Down
Loading