Skip to content

Commit

Permalink
checkstyle. registerView takes View as a parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Polyakov committed Feb 15, 2021
1 parent c02d042 commit 08d24f1
Show file tree
Hide file tree
Showing 13 changed files with 79 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ static <T> Aggregator<T> 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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
* }</pre>
*/
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ static <T> SynchronousInstrumentAccumulator<T> create(
}

SynchronousInstrumentAccumulator(
Aggregator<T> aggregator, InstrumentProcessor<T> instrumentProcessor, LabelsProcessor labelsProcessor) {
Aggregator<T> aggregator,
InstrumentProcessor<T> instrumentProcessor,
LabelsProcessor labelsProcessor) {
aggregatorLabels = new ConcurrentHashMap<>();
collectLock = new ReentrantLock();
this.aggregator = aggregator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
* never blocked.
*/
final class ViewRegistry {
private static final LinkedHashMap<Pattern, View> 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<Pattern, View> 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();
Expand Down Expand Up @@ -62,8 +64,7 @@ void registerView(InstrumentSelector selector, View aggregatorFactory) {
}

View findView(InstrumentDescriptor descriptor) {
LinkedHashMap<Pattern, View> configPerType =
configuration.get(descriptor.getType());
LinkedHashMap<Pattern, View> configPerType = configuration.get(descriptor.getType());
for (Map.Entry<Pattern, View> entry : configPerType.entrySet()) {
if (entry.getKey().matcher(descriptor.getName()).matches()) {
return entry.getValue();
Expand All @@ -89,9 +90,7 @@ private static View getDefaultSpecification(InstrumentDescriptor descriptor) {
}

private static LinkedHashMap<Pattern, View> newLinkedHashMap(
Pattern pattern,
View aggregatorFactory,
LinkedHashMap<Pattern, View> parentConfiguration) {
Pattern pattern, View aggregatorFactory, LinkedHashMap<Pattern, View> parentConfiguration) {
LinkedHashMap<Pattern, View> result = new LinkedHashMap<>();
result.put(pattern, aggregatorFactory);
result.putAll(parentConfiguration);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.sdk.metrics.processor;

import io.opentelemetry.api.metrics.common.Labels;
import io.opentelemetry.context.Context;

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);
}
Original file line number Diff line number Diff line change
@@ -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}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@

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;
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.view.InstrumentSelector;
import io.opentelemetry.sdk.metrics.view.View;
import org.junit.jupiter.api.Test;

class ViewRegistryTest {
Expand All @@ -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(
Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand All @@ -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(
Expand Down

0 comments on commit 08d24f1

Please sign in to comment.