Skip to content

Commit

Permalink
Finishing test cases
Browse files Browse the repository at this point in the history
- Fixed builder to setName as well
- Populating TestMetricData.data with an empty ImmutableSummaryData
  • Loading branch information
ThomasJazz committed Feb 13, 2024
1 parent 1288322 commit 1ac0012
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.metrics.data;

import static java.util.Objects.requireNonNull;

import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.resources.Resource;
import javax.annotation.Nullable;

/**
* A {@link MetricData} which delegates all methods to another {@link MetricData}. Extend this class to
* modify the {@link MetricData} that will be exported.
*/
public abstract class DelegatingMetricData implements MetricData {

private final MetricData delegate;
Expand Down Expand Up @@ -49,20 +58,19 @@ public Data<?> getData() {
}

@Override
public boolean equals(Object o) {
public boolean equals(@Nullable Object o) {
if (o == this) {
return true;
}
if (o instanceof DelegatingMetricData) {
DelegatingMetricData that = (DelegatingMetricData) o;
return this.delegate.equals(that.delegate) &&
this.getResource().equals(that.getResource()) &&
this.getInstrumentationScopeInfo().equals(that.getInstrumentationScopeInfo()) &&
this.getName().equals(that.getName()) &&
this.getDescription().equals(that.getDescription()) &&
this.getUnit().equals(that.getUnit()) &&
this.getType().equals(that.getType()) &&
this.getData().equals(that.getData());
if (o instanceof MetricData) {
MetricData that = (MetricData) o;
return getResource().equals(that.getResource()) &&
getInstrumentationScopeInfo().equals(that.getInstrumentationScopeInfo()) &&
getName().equals(that.getName()) &&
getDescription().equals(that.getDescription()) &&
getUnit().equals(that.getUnit()) &&
getType().equals(that.getType()) &&
getData().equals(that.getData());
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.metrics.data;

import static org.assertj.core.api.Assertions.assertThat;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData;
import io.opentelemetry.sdk.resources.Resource;
import io.opentelemetry.sdk.testing.metrics.TestMetricData;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.junit.jupiter.api.Test;

class DelegatingMetricDataTest {

private static final class NoOpDelegatingMetricData extends DelegatingMetricData {
private String description;
private NoOpDelegatingMetricData(MetricData delegate) {
super(delegate);
}
}

private static final class MetricDataWithClientDescription extends DelegatingMetricData {
private static final class MetricDataWithCustomDescription extends DelegatingMetricData {
private final String description;

private MetricDataWithClientDescription(MetricData delegate) {
private MetricDataWithCustomDescription(MetricData delegate) {
super(delegate);
this.description = "test";
}
Expand All @@ -26,20 +33,34 @@ public String getDescription() {
return description;
}

private static String parseUserAgent(String userAgent) {
if (userAgent.startsWith("Mozilla/")) {
return "browser";
} else if (userAgent.startsWith("Phone/")) {
return "phone";
}
return "unknown";
}
}

@Test
void delegates() {
MetricData metricData = createBasicMetricBuilder().build();
MetricData noOpWrapper = new NoOpDelegatingMetricData(metricData);

assertThat(noOpWrapper)
.usingRecursiveComparison(
RecursiveComparisonConfiguration.builder().withIgnoredFields("delegate").build())
.isEqualTo(metricData);
}

@Test
void overrideDelegate() {
MetricData metricData = createBasicMetricBuilder().build();
MetricData withCustomDescription = new MetricDataWithCustomDescription(metricData);

assertThat(withCustomDescription.getDescription()).isEqualTo("test");
}
@Test
void equals() {
MetricData metricData = createBasicMetricBuilder().build();
MetricData noOpWrapper = new NoOpDelegatingMetricData(metricData);
MetricData withCustomDescription = new MetricDataWithCustomDescription(metricData);

assertThat(noOpWrapper).isEqualTo(metricData);
assertThat(metricData).isNotEqualTo(withCustomDescription);
}

private static TestMetricData.Builder createBasicMetricBuilder() {
Expand All @@ -48,7 +69,8 @@ private static TestMetricData.Builder createBasicMetricBuilder() {
.setInstrumentationScopeInfo(InstrumentationScopeInfo.empty())
.setDescription("")
.setUnit("1")
.setData(null) // TODO: Need to figure out how to set the Data value
.setName("name")
.setData(ImmutableSummaryData.empty())
.setType(MetricDataType.SUMMARY); // Not sure what type should be here
}
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.sdk.testing.metrics;

import com.google.auto.value.AutoValue;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
import io.opentelemetry.sdk.metrics.data.Data;
import io.opentelemetry.sdk.metrics.data.MetricData;
import io.opentelemetry.sdk.metrics.data.MetricDataType;
import io.opentelemetry.sdk.metrics.internal.data.ImmutableSummaryData;
import io.opentelemetry.sdk.resources.Resource;
import javax.annotation.concurrent.Immutable;

/**
* Immutable representation of all data collected by the {@link io.opentelemetry.sdk.metrics.data.MetricData}
* class.
*/
@Immutable
@AutoValue
public abstract class TestMetricData implements MetricData {
public static Builder builder() {
// TODO: Need to figure out how to set the Data value
return new AutoValue_TestMetricData.Builder()
.setResource(Resource.empty())
.setName("name")
.setInstrumentationScopeInfo(InstrumentationScopeInfo.empty())
.setDescription("description")
.setUnit("1")
.setData(null)
.setData(ImmutableSummaryData.empty())
.setType(MetricDataType.SUMMARY);
}

Expand Down

0 comments on commit 1ac0012

Please sign in to comment.