Skip to content

Commit

Permalink
tests for the ViewRegistry
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwatson committed Nov 12, 2020
1 parent 9678531 commit 77f5c7f
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,66 @@ public final List<MetricData> 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() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -62,8 +66,4 @@ Batcher createBatcher(
}
throw new IllegalStateException("unsupported Temporality: " + specification.temporality());
}

void registerView(InstrumentSelector selector, AggregationConfiguration specification) {
aggregationChooser.addView(selector, specification);
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}

0 comments on commit 77f5c7f

Please sign in to comment.