-
Notifications
You must be signed in to change notification settings - Fork 858
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement metric identity specification (#4222)
* Implement metric identity specification * PR feedback * Fix build
- Loading branch information
Showing
22 changed files
with
1,375 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...dTest/java/io/opentelemetry/sdk/metrics/internal/descriptor/InstrumentDescriptorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics.internal.descriptor; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.sdk.metrics.common.InstrumentType; | ||
import io.opentelemetry.sdk.metrics.common.InstrumentValueType; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* {@link InstrumentDescriptor#equals(Object)} must ignore {@link | ||
* InstrumentDescriptor#getSourceInfo()}, which only returns a meaningful value when {@code | ||
* otel.experimental.sdk.metrics.debug=true}. | ||
*/ | ||
class InstrumentDescriptorTest { | ||
|
||
@Test | ||
void equals() { | ||
InstrumentDescriptor descriptor = | ||
InstrumentDescriptor.create( | ||
"name", "description", "unit", InstrumentType.COUNTER, InstrumentValueType.LONG); | ||
|
||
assertThat(descriptor) | ||
.isEqualTo( | ||
InstrumentDescriptor.create( | ||
"name", "description", "unit", InstrumentType.COUNTER, InstrumentValueType.LONG)); | ||
|
||
// Validate getSourceInfo() is not equal for otherwise equal descriptors | ||
assertThat(descriptor.getSourceInfo()) | ||
.isNotEqualTo( | ||
InstrumentDescriptor.create( | ||
"name", "description", "unit", InstrumentType.COUNTER, InstrumentValueType.LONG) | ||
.getSourceInfo()); | ||
|
||
// Validate that name, description, unit, type, and value type are considered in equals | ||
assertThat(descriptor) | ||
.isNotEqualTo( | ||
InstrumentDescriptor.create( | ||
"foo", "description", "unit", InstrumentType.COUNTER, InstrumentValueType.LONG)); | ||
assertThat(descriptor) | ||
.isNotEqualTo( | ||
InstrumentDescriptor.create( | ||
"name", "foo", "unit", InstrumentType.COUNTER, InstrumentValueType.LONG)); | ||
assertThat(descriptor) | ||
.isNotEqualTo( | ||
InstrumentDescriptor.create( | ||
"name", "description", "foo", InstrumentType.COUNTER, InstrumentValueType.LONG)); | ||
assertThat(descriptor) | ||
.isNotEqualTo( | ||
InstrumentDescriptor.create( | ||
"name", | ||
"description", | ||
"unit", | ||
InstrumentType.UP_DOWN_COUNTER, | ||
InstrumentValueType.LONG)); | ||
assertThat(descriptor) | ||
.isNotEqualTo( | ||
InstrumentDescriptor.create( | ||
"name", "description", "unit", InstrumentType.COUNTER, InstrumentValueType.DOUBLE)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...trics/src/main/java/io/opentelemetry/sdk/metrics/internal/aggregator/AggregationUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.sdk.metrics.internal.aggregator; | ||
|
||
import io.opentelemetry.sdk.metrics.view.Aggregation; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public class AggregationUtil { | ||
|
||
private static final Map<String, Aggregation> aggregationByName; | ||
private static final Map<Class<? extends Aggregation>, String> nameByAggregation; | ||
|
||
private static final String AGGREGATION_DEFAULT = "default"; | ||
private static final String AGGREGATION_SUM = "sum"; | ||
private static final String AGGREGATION_LAST_VALUE = "last_value"; | ||
private static final String AGGREGATION_DROP = "drop"; | ||
private static final String AGGREGATION_EXPLICIT_BUCKET_HISTOGRAM = "explicit_bucket_histogram"; | ||
|
||
static { | ||
aggregationByName = new HashMap<>(); | ||
aggregationByName.put(AGGREGATION_DEFAULT, Aggregation.defaultAggregation()); | ||
aggregationByName.put(AGGREGATION_SUM, Aggregation.sum()); | ||
aggregationByName.put(AGGREGATION_LAST_VALUE, Aggregation.lastValue()); | ||
aggregationByName.put(AGGREGATION_DROP, Aggregation.drop()); | ||
aggregationByName.put( | ||
AGGREGATION_EXPLICIT_BUCKET_HISTOGRAM, Aggregation.explicitBucketHistogram()); | ||
|
||
nameByAggregation = new HashMap<>(); | ||
nameByAggregation.put(Aggregation.defaultAggregation().getClass(), AGGREGATION_DEFAULT); | ||
nameByAggregation.put(Aggregation.sum().getClass(), AGGREGATION_SUM); | ||
nameByAggregation.put(Aggregation.lastValue().getClass(), AGGREGATION_LAST_VALUE); | ||
nameByAggregation.put(Aggregation.drop().getClass(), AGGREGATION_DROP); | ||
nameByAggregation.put( | ||
Aggregation.explicitBucketHistogram().getClass(), AGGREGATION_EXPLICIT_BUCKET_HISTOGRAM); | ||
} | ||
|
||
private AggregationUtil() {} | ||
|
||
/** | ||
* Return the aggregation for the human-readable {@code name}. | ||
* | ||
* <p>The inverse of {@link #aggregationName(Aggregation)}. | ||
* | ||
* @throws IllegalArgumentException if the name is not recognized | ||
*/ | ||
public static Aggregation forName(String name) { | ||
Aggregation aggregation = aggregationByName.get(name); | ||
if (aggregation == null) { | ||
throw new IllegalArgumentException("Unrecognized aggregation name " + name); | ||
} | ||
return aggregation; | ||
} | ||
|
||
/** | ||
* Return the human-readable name of the {@code aggregation}. | ||
* | ||
* <p>The inverse of {@link #forName(String)}. | ||
* | ||
* @throws IllegalArgumentException if the aggregation is not recognized | ||
*/ | ||
public static String aggregationName(Aggregation aggregation) { | ||
String name = nameByAggregation.get(aggregation.getClass()); | ||
if (name == null) { | ||
throw new IllegalStateException( | ||
"Unrecognized aggregation " + aggregation.getClass().getName()); | ||
} | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.