From 08d24f1eee7a0ef3c13c5666379be2ba0e6a73fe Mon Sep 17 00:00:00 2001 From: Anton Polyakov Date: Mon, 15 Feb 2021 14:58:27 -0800 Subject: [PATCH] checkstyle. registerView takes View as a parameter --- .../sdk/metrics/AbstractAccumulator.java | 12 +++++++----- .../sdk/metrics/SdkMeterProvider.java | 15 ++++----------- .../SynchronousInstrumentAccumulator.java | 4 +++- .../sdk/metrics/ViewRegistry.java | 19 +++++++++---------- .../processor/BaggageLabelsProcessor.java | 5 +++++ .../BaggageMetricsLabelsExtractor.java | 5 +++++ .../metrics/processor/LabelsProcessor.java | 19 +++++++++++++------ .../processor/LabelsProcessorFactory.java | 8 +++++++- .../processor/NoopLabelsProcessor.java | 5 +++++ .../opentelemetry/sdk/metrics/view/View.java | 9 ++++++++- .../sdk/metrics/SdkMeterProviderTest.java | 6 ++++-- .../SynchronousInstrumentAccumulatorTest.java | 6 ++---- .../sdk/metrics/ViewRegistryTest.java | 15 +++++++-------- 13 files changed, 79 insertions(+), 49 deletions(-) diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractAccumulator.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractAccumulator.java index 871ecd40e0f..3b4f91a87d5 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractAccumulator.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/AbstractAccumulator.java @@ -33,10 +33,12 @@ static Aggregator getAggregator( descriptor); } - static LabelsProcessor getLabelsProcessor(MeterProviderSharedState meterProviderSharedState, - InstrumentDescriptor descriptor) { - return meterProviderSharedState.getViewRegistry().findView(descriptor) - .getLabelsProcessorFactory().create(); + static LabelsProcessor getLabelsProcessor( + MeterProviderSharedState meterProviderSharedState, InstrumentDescriptor descriptor) { + return meterProviderSharedState + .getViewRegistry() + .findView(descriptor) + .getLabelsProcessorFactory() + .create(); } - } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java index faec742196c..c1f65dc883f 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SdkMeterProvider.java @@ -9,10 +9,8 @@ import io.opentelemetry.api.metrics.MeterProvider; import io.opentelemetry.sdk.common.Clock; import io.opentelemetry.sdk.internal.ComponentRegistry; -import io.opentelemetry.sdk.metrics.aggregator.AggregatorFactory; import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.export.MetricProducer; -import io.opentelemetry.sdk.metrics.processor.LabelsProcessorFactory; import io.opentelemetry.sdk.metrics.view.InstrumentSelector; import io.opentelemetry.sdk.metrics.view.View; import io.opentelemetry.sdk.resources.Resource; @@ -100,16 +98,11 @@ public static SdkMeterProviderBuilder builder() { * AggregationFactory aggregationFactory = AggregationFactory.minMaxSumCount(); * * //register the view with the MeterSdkProvider - * meterProvider.registerView(instrumentSelector, aggregationFactory); + * meterProvider.registerView(instrumentSelector, View.builder() + * .setAggregatorFactory(factory).build()); * } */ - public void registerView(InstrumentSelector selector, AggregatorFactory aggregatorFactory) { - sharedState.getViewRegistry().registerView(selector, View.builder().setAggregatorFactory(aggregatorFactory).build()); - } - public void registerView(InstrumentSelector selector, AggregatorFactory aggregatorFactory, - LabelsProcessorFactory labelsProcessorFactory) { - sharedState.getViewRegistry().registerView(selector, View.builder() - .setAggregatorFactory(aggregatorFactory) - .setLabelsProcessorFactory(labelsProcessorFactory).build()); + public void registerView(InstrumentSelector selector, View view) { + sharedState.getViewRegistry().registerView(selector, view); } } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulator.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulator.java index 61bb654bfd2..3cc47509869 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulator.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulator.java @@ -38,7 +38,9 @@ static SynchronousInstrumentAccumulator create( } SynchronousInstrumentAccumulator( - Aggregator aggregator, InstrumentProcessor instrumentProcessor, LabelsProcessor labelsProcessor) { + Aggregator aggregator, + InstrumentProcessor instrumentProcessor, + LabelsProcessor labelsProcessor) { aggregatorLabels = new ConcurrentHashMap<>(); collectLock = new ReentrantLock(); this.aggregator = aggregator; 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 fcb520f39d4..0e252727338 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 @@ -24,11 +24,13 @@ * never blocked. */ final class ViewRegistry { - private static final LinkedHashMap EMPTY_CONFIG = - new LinkedHashMap<>(); - static final View CUMULATIVE_SUM = View.builder().setAggregatorFactory(AggregatorFactory.sum(true)).build(); - static final View SUMMARY = View.builder().setAggregatorFactory(AggregatorFactory.minMaxSumCount()).build(); - static final View LAST_VALUE = View.builder().setAggregatorFactory(AggregatorFactory.lastValue()).build(); + private static final LinkedHashMap EMPTY_CONFIG = new LinkedHashMap<>(); + static final View CUMULATIVE_SUM = + View.builder().setAggregatorFactory(AggregatorFactory.sum(true)).build(); + static final View SUMMARY = + View.builder().setAggregatorFactory(AggregatorFactory.minMaxSumCount()).build(); + static final View LAST_VALUE = + View.builder().setAggregatorFactory(AggregatorFactory.lastValue()).build(); // The lock is used to ensure only one updated to the configuration happens at any moment. private final ReentrantLock lock = new ReentrantLock(); @@ -62,8 +64,7 @@ void registerView(InstrumentSelector selector, View aggregatorFactory) { } View findView(InstrumentDescriptor descriptor) { - LinkedHashMap configPerType = - configuration.get(descriptor.getType()); + LinkedHashMap configPerType = configuration.get(descriptor.getType()); for (Map.Entry entry : configPerType.entrySet()) { if (entry.getKey().matcher(descriptor.getName()).matches()) { return entry.getValue(); @@ -89,9 +90,7 @@ private static View getDefaultSpecification(InstrumentDescriptor descriptor) { } private static LinkedHashMap newLinkedHashMap( - Pattern pattern, - View aggregatorFactory, - LinkedHashMap parentConfiguration) { + Pattern pattern, View aggregatorFactory, LinkedHashMap parentConfiguration) { LinkedHashMap result = new LinkedHashMap<>(); result.put(pattern, aggregatorFactory); result.putAll(parentConfiguration); diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageLabelsProcessor.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageLabelsProcessor.java index fb611b11693..e63d94d0e48 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageLabelsProcessor.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageLabelsProcessor.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.sdk.metrics.processor; import io.opentelemetry.api.metrics.common.Labels; diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageMetricsLabelsExtractor.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageMetricsLabelsExtractor.java index 85a12f12b8b..a24fb3f552b 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageMetricsLabelsExtractor.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/BaggageMetricsLabelsExtractor.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.sdk.metrics.processor; import io.opentelemetry.api.metrics.common.Labels; diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessor.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessor.java index dcf230be89b..b06888ad789 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessor.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessor.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.sdk.metrics.processor; import io.opentelemetry.api.metrics.common.Labels; @@ -5,14 +10,16 @@ public interface LabelsProcessor { /** - * Called when bind() method is called. Allows to manipulate labels which this - * instrument is bound to. Particular use case includes enriching lables and/or adding more labels - * depending on the Context + * Called when bind() method is called. Allows to manipulate labels which this instrument is bound + * to. Particular use case includes enriching lables and/or adding more labels depending on the + * Context * * @param ctx context of the operation - * @param labels immutable labels. When processors are chained output labels of the previous one is passed as - * an input to the next one. Last labels returned by a chain of processors are used for bind() operation. - * @return labels to be used as an input to the next processor in chain or bind() operation if this is the last processor + * @param labels immutable labels. When processors are chained output labels of the previous one + * is passed as an input to the next one. Last labels returned by a chain of processors are + * used for bind() operation. + * @return labels to be used as an input to the next processor in chain or bind() operation if + * this is the last processor */ Labels onLabelsBound(Context ctx, Labels labels); } diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessorFactory.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessorFactory.java index d9d3ec195e9..126075653ba 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessorFactory.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/LabelsProcessorFactory.java @@ -1,15 +1,21 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.sdk.metrics.processor; public interface LabelsProcessorFactory { static LabelsProcessorFactory noop() { return NoopLabelsProcessor::new; } + static LabelsProcessorFactory baggageExtractor(BaggageMetricsLabelsExtractor labelsExtractor) { return () -> new BaggageLabelsProcessor(labelsExtractor); } /** - * Returns a new {@link LabelsProcessorFactory} + * Returns a new {@link LabelsProcessorFactory}. * * @return new {@link LabelsProcessorFactory} */ diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/NoopLabelsProcessor.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/NoopLabelsProcessor.java index 2220b418f36..f2c3bf507bb 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/NoopLabelsProcessor.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/processor/NoopLabelsProcessor.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.sdk.metrics.processor; import io.opentelemetry.api.metrics.common.Labels; diff --git a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/View.java b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/View.java index 022e83b8f01..89b837dfd01 100644 --- a/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/View.java +++ b/sdk/metrics/src/main/java/io/opentelemetry/sdk/metrics/view/View.java @@ -1,3 +1,8 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + package io.opentelemetry.sdk.metrics.view; import com.google.auto.value.AutoValue; @@ -19,7 +24,9 @@ public static Builder builder() { @AutoValue.Builder public abstract static class Builder { public abstract Builder setAggregatorFactory(AggregatorFactory aggregatorFactory); - public abstract Builder setLabelsProcessorFactory(LabelsProcessorFactory labelsProcessorFactory); + + public abstract Builder setLabelsProcessorFactory( + LabelsProcessorFactory labelsProcessorFactory); public abstract View build(); } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderTest.java index 4a7b178cf67..2b671cab467 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SdkMeterProviderTest.java @@ -32,6 +32,7 @@ import io.opentelemetry.sdk.metrics.data.MetricData; import io.opentelemetry.sdk.metrics.data.ValueAtPercentile; import io.opentelemetry.sdk.metrics.view.InstrumentSelector; +import io.opentelemetry.sdk.metrics.view.View; import io.opentelemetry.sdk.resources.Resource; import java.util.Arrays; import java.util.Collections; @@ -161,7 +162,7 @@ void collectAllSyncInstruments() { void collectAllSyncInstruments_OverwriteTemporality() { sdkMeterProvider.registerView( InstrumentSelector.builder().setInstrumentType(InstrumentType.COUNTER).build(), - AggregatorFactory.sum(false)); + View.builder().setAggregatorFactory(AggregatorFactory.sum(false)).build()); LongCounter longCounter = sdkMeter.longCounterBuilder("testLongCounter").build(); longCounter.add(10, Labels.empty()); @@ -671,7 +672,8 @@ private static void registerViewForAllTypes( SdkMeterProvider meterProvider, AggregatorFactory factory) { for (InstrumentType instrumentType : InstrumentType.values()) { meterProvider.registerView( - InstrumentSelector.builder().setInstrumentType(instrumentType).build(), factory); + InstrumentSelector.builder().setInstrumentType(instrumentType).build(), + View.builder().setAggregatorFactory(factory).build()); } } } diff --git a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulatorTest.java b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulatorTest.java index 1b96938ed8f..34650bd3540 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulatorTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/SynchronousInstrumentAccumulatorTest.java @@ -5,10 +5,6 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.sdk.metrics.processor.LabelsProcessor; - -import io.opentelemetry.sdk.metrics.processor.LabelsProcessorFactory; - import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.api.metrics.common.Labels; @@ -20,6 +16,8 @@ import io.opentelemetry.sdk.metrics.common.InstrumentDescriptor; import io.opentelemetry.sdk.metrics.common.InstrumentType; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; +import io.opentelemetry.sdk.metrics.processor.LabelsProcessor; +import io.opentelemetry.sdk.metrics.processor.LabelsProcessorFactory; import io.opentelemetry.sdk.resources.Resource; import org.junit.jupiter.api.Test; 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 index ee2d6b0b596..33e4da5b8b9 100644 --- a/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/ViewRegistryTest.java +++ b/sdk/metrics/src/test/java/io/opentelemetry/sdk/metrics/ViewRegistryTest.java @@ -5,8 +5,6 @@ package io.opentelemetry.sdk.metrics; -import io.opentelemetry.sdk.metrics.view.View; - import static org.assertj.core.api.Assertions.assertThat; import io.opentelemetry.sdk.metrics.aggregator.AggregatorFactory; @@ -14,6 +12,7 @@ import io.opentelemetry.sdk.metrics.common.InstrumentType; import io.opentelemetry.sdk.metrics.common.InstrumentValueType; import io.opentelemetry.sdk.metrics.view.InstrumentSelector; +import io.opentelemetry.sdk.metrics.view.View; import org.junit.jupiter.api.Test; class ViewRegistryTest { @@ -33,7 +32,7 @@ void selection_onType() { viewRegistry.findView( InstrumentDescriptor.create( "", "", "", InstrumentType.COUNTER, InstrumentValueType.LONG))) - .isEqualTo(factory); + .isEqualTo(view); // this one hasn't been configured, so it gets the default still. assertThat( viewRegistry.findView( @@ -58,7 +57,7 @@ void selection_onName() { viewRegistry.findView( InstrumentDescriptor.create( "overridden", "", "", InstrumentType.COUNTER, InstrumentValueType.LONG))) - .isSameAs(factory); + .isSameAs(view); // this one hasn't been configured, so it gets the default still. assertThat( viewRegistry.findView( @@ -92,12 +91,12 @@ void selection_LastAddedViewWins() { viewRegistry.findView( InstrumentDescriptor.create( "overridden", "", "", InstrumentType.COUNTER, InstrumentValueType.LONG))) - .isEqualTo(factory2); + .isEqualTo(view2); assertThat( viewRegistry.findView( InstrumentDescriptor.create( "default", "", "", InstrumentType.COUNTER, InstrumentValueType.LONG))) - .isEqualTo(factory1); + .isEqualTo(view1); } @Test @@ -117,12 +116,12 @@ void selection_regex() { viewRegistry.findView( InstrumentDescriptor.create( "overridden", "", "", InstrumentType.COUNTER, InstrumentValueType.LONG))) - .isEqualTo(factory); + .isEqualTo(view); assertThat( viewRegistry.findView( InstrumentDescriptor.create( "overrides", "", "", InstrumentType.COUNTER, InstrumentValueType.LONG))) - .isEqualTo(factory); + .isEqualTo(view); // this one hasn't been configured, so it gets the default still.. assertThat( viewRegistry.findView(