Skip to content

Commit

Permalink
Merge pull request #1 from ThomasJazz/6195-DelegatingMetricData
Browse files Browse the repository at this point in the history
Delegating metric data (#6195)
  • Loading branch information
ThomasJazz committed Feb 13, 2024
2 parents 48d41d3 + 1ac0012 commit 8d98fd1
Show file tree
Hide file tree
Showing 3 changed files with 294 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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;

protected DelegatingMetricData(MetricData delegate) {
this.delegate = requireNonNull(delegate, "delegate");
}

@Override
public Resource getResource() {
return delegate.getResource();
}

@Override
public InstrumentationScopeInfo getInstrumentationScopeInfo() {
return delegate.getInstrumentationScopeInfo();
}

@Override
public String getName() {
return delegate.getName();
}

@Override
public String getDescription() {
return delegate.getDescription();
}

@Override
public String getUnit() {
return delegate.getUnit();
}

@Override
public MetricDataType getType() {
return delegate.getType();
}

@Override
public Data<?> getData() {
return delegate.getData();
}

@Override
public boolean equals(@Nullable Object o) {
if (o == this) {
return true;
}
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;
}

@Override
public int hashCode() {
int result = 1;
result *= 1000003;
result ^= this.getResource().hashCode();
result *= 1000003;
result ^= this.getInstrumentationScopeInfo().hashCode();
result *= 1000003;
result ^= this.getName().hashCode();
result *= 1000003;
result ^= this.getDescription().hashCode();
result *= 1000003;
result ^= this.getUnit().hashCode();
result *= 1000003;
result ^= this.getType().hashCode();
result *= 1000003;
result ^= this.getData().hashCode();
return result;
}

@Override
public String toString() {
return "DelegatingMetricData{" +
"resource=" + getResource() + ", " +
"instrumentationScopeInfo=" + getInstrumentationScopeInfo() + ", " +
"name=" + getName() + ", " +
"description=" + getDescription() + ", " +
"unit=" + getUnit() + ", " +
"type=" + getType() + ", " +
"data=" + getData() +
"}";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* 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 NoOpDelegatingMetricData(MetricData delegate) {
super(delegate);
}
}

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

private MetricDataWithCustomDescription(MetricData delegate) {
super(delegate);
this.description = "test";
}

@Override
public String getDescription() {
return description;
}

}

@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() {
return TestMetricData.builder()
.setResource(Resource.empty())
.setInstrumentationScopeInfo(InstrumentationScopeInfo.empty())
.setDescription("")
.setUnit("1")
.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
@@ -0,0 +1,108 @@
/*
* 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() {
return new AutoValue_TestMetricData.Builder()
.setResource(Resource.empty())
.setName("name")
.setInstrumentationScopeInfo(InstrumentationScopeInfo.empty())
.setDescription("description")
.setUnit("1")
.setData(ImmutableSummaryData.empty())
.setType(MetricDataType.SUMMARY);
}

/** A builder for {@link TestMetricData}. */
@AutoValue.Builder
public abstract static class Builder {
abstract TestMetricData autoBuild();

/**
* Builds and returns a new {@link MetricData} instance from the data in {@code this}
*
* @return a new {@link MetricData} instance
*/
public TestMetricData build() {
return autoBuild();
}

/**
* Sets the resource of the metric
*
* @param resource the resource of the metric
* @return this
*/
public abstract Builder setResource(Resource resource);

/**
* Sets the name of the metric
*
* @param name the name of the metric
* @return this
*/
public abstract Builder setName(String name);

/**
* Sets the description of the metric
*
* @param description the description of the metric
* @return this
*/
public abstract Builder setDescription(String description);

/**
* Sets the unit of the metric
*
* @param unit the unit of the metric
* @return this
*/
public abstract Builder setUnit(String unit);

/**
* Sets the type of the metric
*
* @param type the type of the metric
* @return this
*/
public abstract Builder setType(MetricDataType type);

/**
* Sets the data of the metric
*
* @param data the data of the metric
* @return this
*/
public abstract Builder setData(Data<?> data);

/**
* Sets the Instrumentation scope of the metric
*
* @param instrumentationScopeInfo the instrumentation scope of the tracer which created this
* metric.
* @return this
*/
public abstract Builder setInstrumentationScopeInfo(
InstrumentationScopeInfo instrumentationScopeInfo);

}

}

0 comments on commit 8d98fd1

Please sign in to comment.