|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.api.incubator.common; |
| 7 | + |
| 8 | +import io.opentelemetry.api.common.AttributeKey; |
| 9 | +import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl; |
| 10 | +import java.util.List; |
| 11 | +import javax.annotation.Nullable; |
| 12 | +import javax.annotation.concurrent.Immutable; |
| 13 | + |
| 14 | +/** |
| 15 | + * This interface provides a handle for setting the values of {@link ExtendedAttributes}. The type |
| 16 | + * of value that can be set with an implementation of this key is denoted by the type parameter. |
| 17 | + * |
| 18 | + * <p>Implementations MUST be immutable, as these are used as the keys to Maps. |
| 19 | + * |
| 20 | + * <p>The allowed {@link #getType()}s is a superset of those allowed in {@link AttributeKey}. |
| 21 | + * |
| 22 | + * <p>Convenience methods are provided for translating to / from {@link AttributeKey}: |
| 23 | + * |
| 24 | + * <ul> |
| 25 | + * <li>{@link #asAttributeKey()} converts from {@link ExtendedAttributeKey} to {@link |
| 26 | + * AttributeKey} |
| 27 | + * <li>{@link #fromAttributeKey(AttributeKey)} converts from {@link AttributeKey} to {@link |
| 28 | + * ExtendedAttributeKey} |
| 29 | + * </ul> |
| 30 | + * |
| 31 | + * @param <T> The type of value that can be set with the key. |
| 32 | + */ |
| 33 | +@Immutable |
| 34 | +public interface ExtendedAttributeKey<T> { |
| 35 | + /** Returns the underlying String representation of the key. */ |
| 36 | + String getKey(); |
| 37 | + |
| 38 | + /** Returns the type of attribute for this key. Useful for building switch statements. */ |
| 39 | + ExtendedAttributeType getType(); |
| 40 | + |
| 41 | + /** |
| 42 | + * Return the equivalent {@link AttributeKey}, or {@code null} if the {@link #getType()} has no |
| 43 | + * equivalent {@link io.opentelemetry.api.common.AttributeType}. |
| 44 | + */ |
| 45 | + @Nullable |
| 46 | + default AttributeKey<T> asAttributeKey() { |
| 47 | + return InternalExtendedAttributeKeyImpl.toAttributeKey(this); |
| 48 | + } |
| 49 | + |
| 50 | + /** Return an ExtendedAttributeKey equivalent to the {@code attributeKey}. */ |
| 51 | + // TODO (jack-berg): remove once AttributeKey.asExtendedAttributeKey is available |
| 52 | + static <T> ExtendedAttributeKey<T> fromAttributeKey(AttributeKey<T> attributeKey) { |
| 53 | + return InternalExtendedAttributeKeyImpl.toExtendedAttributeKey(attributeKey); |
| 54 | + } |
| 55 | + |
| 56 | + /** Returns a new ExtendedAttributeKey for String valued attributes. */ |
| 57 | + static ExtendedAttributeKey<String> stringKey(String key) { |
| 58 | + return fromAttributeKey(AttributeKey.stringKey(key)); |
| 59 | + } |
| 60 | + |
| 61 | + /** Returns a new ExtendedAttributeKey for Boolean valued attributes. */ |
| 62 | + static ExtendedAttributeKey<Boolean> booleanKey(String key) { |
| 63 | + return fromAttributeKey(AttributeKey.booleanKey(key)); |
| 64 | + } |
| 65 | + |
| 66 | + /** Returns a new ExtendedAttributeKey for Long valued attributes. */ |
| 67 | + static ExtendedAttributeKey<Long> longKey(String key) { |
| 68 | + return fromAttributeKey(AttributeKey.longKey(key)); |
| 69 | + } |
| 70 | + |
| 71 | + /** Returns a new ExtendedAttributeKey for Double valued attributes. */ |
| 72 | + static ExtendedAttributeKey<Double> doubleKey(String key) { |
| 73 | + return fromAttributeKey(AttributeKey.doubleKey(key)); |
| 74 | + } |
| 75 | + |
| 76 | + /** Returns a new ExtendedAttributeKey for List<String> valued attributes. */ |
| 77 | + static ExtendedAttributeKey<List<String>> stringArrayKey(String key) { |
| 78 | + return fromAttributeKey(AttributeKey.stringArrayKey(key)); |
| 79 | + } |
| 80 | + |
| 81 | + /** Returns a new ExtendedAttributeKey for List<Boolean> valued attributes. */ |
| 82 | + static ExtendedAttributeKey<List<Boolean>> booleanArrayKey(String key) { |
| 83 | + return fromAttributeKey(AttributeKey.booleanArrayKey(key)); |
| 84 | + } |
| 85 | + |
| 86 | + /** Returns a new ExtendedAttributeKey for List<Long> valued attributes. */ |
| 87 | + static ExtendedAttributeKey<List<Long>> longArrayKey(String key) { |
| 88 | + return fromAttributeKey(AttributeKey.longArrayKey(key)); |
| 89 | + } |
| 90 | + |
| 91 | + /** Returns a new ExtendedAttributeKey for List<Double> valued attributes. */ |
| 92 | + static ExtendedAttributeKey<List<Double>> doubleArrayKey(String key) { |
| 93 | + return fromAttributeKey(AttributeKey.doubleArrayKey(key)); |
| 94 | + } |
| 95 | + |
| 96 | + /** Returns a new ExtendedAttributeKey for Map valued attributes. */ |
| 97 | + static ExtendedAttributeKey<ExtendedAttributes> extendedAttributesKey(String key) { |
| 98 | + return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.EXTENDED_ATTRIBUTES); |
| 99 | + } |
| 100 | +} |
0 commit comments