diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AggregationChooser.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AggregationChooser.java index de2d7e0a7c7..98540f80107 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AggregationChooser.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AggregationChooser.java @@ -55,7 +55,7 @@ && matchesOnName(descriptor, registeredSelector)) { return possibleMatches.get(0).getValue(); } - private boolean matchesOne(InstrumentDescriptor descriptor, InstrumentSelector selector) { + private static boolean matchesOne(InstrumentDescriptor descriptor, InstrumentSelector selector) { if (selector.hasNameRegex() && !matchesOnName(descriptor, selector)) { return false; } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/Batchers.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/Batchers.java index 180531b4a43..ce62008d687 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/Batchers.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/Batchers.java @@ -165,6 +165,66 @@ public final List completeCollectionCycle() { public boolean generatesDeltas() { return delta; } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + + AllLabels allLabels = (AllLabels) o; + + if (startEpochNanos != allLabels.startEpochNanos) { + return false; + } + if (delta != allLabels.delta) { + return false; + } + if (descriptor != null ? !descriptor.equals(allLabels.descriptor) + : allLabels.descriptor != null) { + return false; + } + if (aggregation != null ? !aggregation.equals(allLabels.aggregation) + : allLabels.aggregation != null) { + return false; + } + if (resource != null ? !resource.equals(allLabels.resource) : allLabels.resource != null) { + return false; + } + if (instrumentationLibraryInfo != null ? !instrumentationLibraryInfo + .equals(allLabels.instrumentationLibraryInfo) + : allLabels.instrumentationLibraryInfo != null) { + return false; + } + if (clock != null ? !clock.equals(allLabels.clock) : allLabels.clock != null) { + return false; + } + if (aggregatorFactory != null ? !aggregatorFactory.equals(allLabels.aggregatorFactory) + : allLabels.aggregatorFactory != null) { + return false; + } + return aggregatorMap != null ? aggregatorMap.equals(allLabels.aggregatorMap) + : allLabels.aggregatorMap == null; + } + + @Override + public int hashCode() { + int result = descriptor != null ? descriptor.hashCode() : 0; + result = 31 * result + (aggregation != null ? aggregation.hashCode() : 0); + result = 31 * result + (resource != null ? resource.hashCode() : 0); + result = + 31 * result + (instrumentationLibraryInfo != null ? instrumentationLibraryInfo.hashCode() + : 0); + result = 31 * result + (clock != null ? clock.hashCode() : 0); + result = 31 * result + (aggregatorFactory != null ? aggregatorFactory.hashCode() : 0); + result = 31 * result + (aggregatorMap != null ? aggregatorMap.hashCode() : 0); + result = 31 * result + (int) (startEpochNanos ^ (startEpochNanos >>> 32)); + result = 31 * result + (delta ? 1 : 0); + return result; + } } private Batchers() {} diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ViewRegistry.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ViewRegistry.java index 0c4eb66dd63..d44112ea585 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ViewRegistry.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/ViewRegistry.java @@ -40,6 +40,10 @@ class ViewRegistry { this.aggregationChooser = aggregationChooser; } + void registerView(InstrumentSelector selector, AggregationConfiguration specification) { + aggregationChooser.addView(selector, specification); + } + /** * Create a new {@link io.opentelemetry.sdk.metrics.Batcher} for use in metric recording * aggregation. @@ -62,8 +66,4 @@ Batcher createBatcher( } throw new IllegalStateException("unsupported Temporality: " + specification.temporality()); } - - void registerView(InstrumentSelector selector, AggregationConfiguration specification) { - aggregationChooser.addView(selector, specification); - } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/ViewRegistryTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/ViewRegistryTest.java new file mode 100644 index 00000000000..f206b3915a4 --- /dev/null +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/ViewRegistryTest.java @@ -0,0 +1,102 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.sdk.metrics; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import io.opentelemetry.sdk.common.InstrumentationLibraryInfo; +import io.opentelemetry.sdk.internal.TestClock; +import io.opentelemetry.sdk.metrics.common.InstrumentType; +import io.opentelemetry.sdk.metrics.common.InstrumentValueType; +import io.opentelemetry.sdk.metrics.view.AggregationConfiguration; +import io.opentelemetry.sdk.metrics.view.Aggregations; +import io.opentelemetry.sdk.metrics.view.InstrumentSelector; +import io.opentelemetry.sdk.resources.Resource; +import org.junit.jupiter.api.Test; + +class ViewRegistryTest { + + @Test + void registerView() { + AggregationChooser chooser = mock(AggregationChooser.class); + + ViewRegistry viewRegistry = new ViewRegistry(chooser); + InstrumentSelector selector = + InstrumentSelector.newBuilder().instrumentType(InstrumentType.COUNTER).build(); + AggregationConfiguration specification = + AggregationConfiguration.create( + Aggregations.count(), AggregationConfiguration.Temporality.CUMULATIVE); + + viewRegistry.registerView(selector, specification); + + verify(chooser).addView(selector, specification); + } + + @Test + void createBatcher_cumulative() { + AggregationChooser chooser = mock(AggregationChooser.class); + + ViewRegistry viewRegistry = new ViewRegistry(chooser); + + InstrumentDescriptor descriptor = InstrumentDescriptor.create("name", "description", "unit", + InstrumentType.COUNTER, InstrumentValueType.DOUBLE); + MeterProviderSharedState providerSharedState = MeterProviderSharedState.create( + TestClock.create(), + Resource.getEmpty()); + MeterSharedState meterSharedState = MeterSharedState + .create(InstrumentationLibraryInfo.create("test", "1.0")); + + AggregationConfiguration specification = + AggregationConfiguration.create( + Aggregations.count(), AggregationConfiguration.Temporality.CUMULATIVE); + Batcher expectedBatcher = Batchers + .getCumulativeAllLabels(descriptor, providerSharedState, meterSharedState, + Aggregations.count()); + + when(chooser.chooseAggregation(descriptor)).thenReturn(specification); + + Batcher result = viewRegistry.createBatcher(providerSharedState, meterSharedState, descriptor); + + assertThat(result.generatesDeltas()).isFalse(); + assertThat(result).isEqualTo(expectedBatcher); + + assertThat(result).isNotNull(); + } + + @Test + void createBatcher_delta() { + AggregationChooser chooser = mock(AggregationChooser.class); + + ViewRegistry viewRegistry = new ViewRegistry(chooser); + + InstrumentDescriptor descriptor = InstrumentDescriptor.create("name", "description", "unit", + InstrumentType.COUNTER, InstrumentValueType.DOUBLE); + MeterProviderSharedState providerSharedState = MeterProviderSharedState.create( + TestClock.create(), + Resource.getEmpty()); + MeterSharedState meterSharedState = MeterSharedState + .create(InstrumentationLibraryInfo.create("test", "1.0")); + + AggregationConfiguration specification = + AggregationConfiguration.create( + Aggregations.count(), AggregationConfiguration.Temporality.DELTA); + Batcher expectedBatcher = Batchers + .getDeltaAllLabels(descriptor, providerSharedState, meterSharedState, + Aggregations.count()); + + when(chooser.chooseAggregation(descriptor)).thenReturn(specification); + + Batcher result = viewRegistry.createBatcher(providerSharedState, meterSharedState, descriptor); + + assertThat(result.generatesDeltas()).isTrue(); + assertThat(result).isEqualTo(expectedBatcher); + + assertThat(result).isNotNull(); + } +}