-
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.
Add AttributeKeyValue abstraction to common otlp exporters (#7026)
- Loading branch information
Showing
5 changed files
with
143 additions
and
0 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
50 changes: 50 additions & 0 deletions
50
.../otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AttributeKeyValue.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,50 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.otlp; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.Attributes; | ||
import io.opentelemetry.api.common.KeyValue; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Key-value pair of {@link AttributeKey} key and its corresponding value. | ||
* | ||
* <p>Conceptually if {@link Attributes} is a Map, then this is a Map.Entry. Note that whilst {@link | ||
* KeyValue} is similar, this class holds type information on the Key rather than the value. | ||
* | ||
* <p>NOTE: This class is only used in the profiling signal, and exists in this module and package | ||
* because its a common dependency of the modules that use it. Long term, it probably makes more | ||
* sense to live in {@code opentelemetry-sdk-profiles} once such a module exists. | ||
* | ||
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change | ||
* at any time. | ||
*/ | ||
public interface AttributeKeyValue<T> { | ||
|
||
/** Returns a {@link AttributeKeyValue} for the given {@link AttributeKey} and {@code value}. */ | ||
static <T> AttributeKeyValue<T> of(AttributeKey<T> attributeKey, T value) { | ||
return AttributeKeyValueImpl.create(attributeKey, value); | ||
} | ||
|
||
/** Returns a List corresponding to the provided Map. This is a copy, not a view. */ | ||
@SuppressWarnings("unchecked") | ||
static <T> List<AttributeKeyValue<?>> of(Attributes attributes) { | ||
List<AttributeKeyValue<?>> result = new ArrayList<>(attributes.size()); | ||
attributes.forEach( | ||
(key, value) -> { | ||
result.add(of((AttributeKey<T>) key, (T) value)); | ||
}); | ||
return result; | ||
} | ||
|
||
/** Returns the key. */ | ||
AttributeKey<T> getAttributeKey(); | ||
|
||
/** Returns the value. */ | ||
T getValue(); | ||
} |
19 changes: 19 additions & 0 deletions
19
...p/common/src/main/java/io/opentelemetry/exporter/internal/otlp/AttributeKeyValueImpl.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,19 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.otlp; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import io.opentelemetry.api.common.AttributeKey; | ||
|
||
@AutoValue | ||
abstract class AttributeKeyValueImpl<T> implements AttributeKeyValue<T> { | ||
|
||
AttributeKeyValueImpl() {} | ||
|
||
static <T> AttributeKeyValueImpl<T> create(AttributeKey<T> attributeKey, T value) { | ||
return new AutoValue_AttributeKeyValueImpl<T>(attributeKey, value); | ||
} | ||
} |
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
49 changes: 49 additions & 0 deletions
49
...p/common/src/test/java/io/opentelemetry/exporter/internal/otlp/AttributeKeyValueTest.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,49 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.exporter.internal.otlp; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import io.opentelemetry.api.common.AttributeKey; | ||
import io.opentelemetry.api.common.AttributeType; | ||
import io.opentelemetry.api.common.Attributes; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import nl.jqno.equalsverifier.EqualsVerifier; | ||
import org.assertj.core.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class AttributeKeyValueTest { | ||
|
||
@Test | ||
void equalsVerifier() { | ||
EqualsVerifier.forClass(AttributeKeyValue.class).verify(); | ||
} | ||
|
||
@Test | ||
void ofEmpty() { | ||
assertThat(AttributeKeyValue.of(Attributes.empty())).isEmpty(); | ||
} | ||
|
||
@Test | ||
void ofOne() { | ||
AttributeKeyValue<String> input = AttributeKeyValue.of(AttributeKey.stringKey("foo"), "bar"); | ||
Attributes attributes = Attributes.of(input.getAttributeKey(), input.getValue()); | ||
List<AttributeKeyValue<?>> list = AttributeKeyValue.of(attributes); | ||
Assertions.assertThat(list).hasSize(1); | ||
assertThat(list.get(0)).isEqualTo(input); | ||
} | ||
|
||
@Test | ||
void ofList() { | ||
AttributeKeyValue<List<Long>> input = | ||
AttributeKeyValue.of(AttributeKey.longArrayKey("foo"), Collections.emptyList()); | ||
Attributes attributes = Attributes.of(input.getAttributeKey(), input.getValue()); | ||
List<AttributeKeyValue<?>> list = AttributeKeyValue.of(attributes); | ||
Assertions.assertThat(list).hasSize(1); | ||
assertThat(list.get(0).getAttributeKey().getType()).isEqualTo(AttributeType.LONG_ARRAY); | ||
} | ||
} |