Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ static AttributeKey<List<Long>> longArrayKey(String key) {
static AttributeKey<List<Double>> doubleArrayKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.DOUBLE_ARRAY);
}

static AttributeKey<byte[]> byteArrayKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.BYTE_ARRAY);
}

static AttributeKey<List<Value<?>>> valueArrayKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.VALUE_ARRAY);
}

static AttributeKey<Attributes> mapKey(String key) {
return InternalAttributeKeyImpl.create(key, AttributeType.MAP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ public enum AttributeType {
STRING_ARRAY,
BOOLEAN_ARRAY,
LONG_ARRAY,
DOUBLE_ARRAY
DOUBLE_ARRAY,
BYTE_ARRAY,
VALUE_ARRAY,
MAP
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
import static io.opentelemetry.api.common.ArrayBackedAttributesBuilder.toList;
import static io.opentelemetry.api.common.AttributeKey.booleanArrayKey;
import static io.opentelemetry.api.common.AttributeKey.booleanKey;
import static io.opentelemetry.api.common.AttributeKey.byteArrayKey;
import static io.opentelemetry.api.common.AttributeKey.doubleArrayKey;
import static io.opentelemetry.api.common.AttributeKey.doubleKey;
import static io.opentelemetry.api.common.AttributeKey.longArrayKey;
import static io.opentelemetry.api.common.AttributeKey.longKey;
import static io.opentelemetry.api.common.AttributeKey.mapKey;
import static io.opentelemetry.api.common.AttributeKey.stringArrayKey;
import static io.opentelemetry.api.common.AttributeKey.stringKey;
import static io.opentelemetry.api.common.AttributeKey.valueArrayKey;

import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -164,6 +167,30 @@ default AttributesBuilder put(String key, boolean... value) {
return put(booleanArrayKey(key), toList(value));
}

/** Puts a byte array attribute into this. */
default AttributesBuilder put(String key, byte[] value) {
if (value == null) {
return this;
}
return put(byteArrayKey(key), value);
}

/** Puts a {@link Value} array attribute into this. */
default AttributesBuilder put(String key, Value<?>... value) {
if (value == null) {
return this;
}
return put(valueArrayKey(key), Arrays.asList(value));
}

/** Puts an {@link Attributes} attribute into this. */
default AttributesBuilder put(String key, Attributes value) {
if (value == null) {
return this;
}
return put(mapKey(key), value);
}

/**
* Puts all the provided attributes into this Builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ public Attributes asAttributes() {
AttributeKey<Object> attributeKey =
(AttributeKey<Object>) extendedAttributeKey.asAttributeKey();
if (attributeKey != null) {
builder.put(attributeKey, value);
if (attributeKey.getType() == io.opentelemetry.api.common.AttributeType.MAP
&& value instanceof ExtendedAttributes) {
builder.put(attributeKey, ((ExtendedAttributes) value).asAttributes());
} else {
builder.put(attributeKey, value);
}
}
});
attributes = builder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.api.incubator.common;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Value;
import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl;
import java.util.List;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -93,8 +94,18 @@ static ExtendedAttributeKey<List<Double>> doubleArrayKey(String key) {
return fromAttributeKey(AttributeKey.doubleArrayKey(key));
}

/** Returns a new ExtendedAttributeKey for Map valued attributes. */
static ExtendedAttributeKey<ExtendedAttributes> extendedAttributesKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.EXTENDED_ATTRIBUTES);
/** Returns a new ExtendedAttributeKey for byte array valued attributes. */
static ExtendedAttributeKey<byte[]> byteArrayKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.BYTE_ARRAY);
}

/** Returns a new ExtendedAttributeKey for List&lt;Value&lt;?&gt;&gt; valued attributes. */
static ExtendedAttributeKey<List<Value<?>>> valueArrayKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.VALUE_ARRAY);
}

/** Returns a new ExtendedAttributeKey for {@link ExtendedAttributes} valued attributes. */
static ExtendedAttributeKey<ExtendedAttributes> mapKey(String key) {
return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.MAP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,7 @@ public enum ExtendedAttributeType {
LONG_ARRAY,
DOUBLE_ARRAY,
// Extended types unique to ExtendedAttributes
EXTENDED_ATTRIBUTES;
BYTE_ARRAY,
VALUE_ARRAY,
MAP;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.opentelemetry.api.incubator.common;

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.AttributeType;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder;
import java.util.Map;
Expand All @@ -17,9 +18,8 @@
* An immutable container for extended attributes.
*
* <p>"extended" refers an extended set of allowed value types compared to standard {@link
* Attributes}. Notably, {@link ExtendedAttributes} values can be of type {@link
* ExtendedAttributeType#EXTENDED_ATTRIBUTES}, allowing nested {@link ExtendedAttributes} of
* arbitrary depth.
* Attributes}. Notably, {@link ExtendedAttributes} values can be nested {@link ExtendedAttributes}
* via the {@link ExtendedAttributeType#MAP} type, allowing arbitrary depth.
*
* <p>Where standard {@link Attributes} are accepted everyone that OpenTelemetry represents key /
* value pairs, {@link ExtendedAttributes} are only accepted in select places, such as log records
Expand Down Expand Up @@ -53,11 +53,20 @@ public interface ExtendedAttributes {

/** Returns the value for the given {@link AttributeKey}, or {@code null} if not found. */
@Nullable
@SuppressWarnings("unchecked")
default <T> T get(AttributeKey<T> key) {
if (key == null) {
return null;
}
return get(ExtendedAttributeKey.fromAttributeKey(key));
ExtendedAttributeKey<T> extendedAttributeKey = ExtendedAttributeKey.fromAttributeKey(key);
Object value = get(extendedAttributeKey);
if (value == null) {
return null;
}
if (key.getType() == AttributeType.MAP && value instanceof ExtendedAttributes) {
return (T) ((ExtendedAttributes) value).asAttributes();
}
return (T) value;
}

/** Returns the value for the given {@link ExtendedAttributeKey}, or {@code null} if not found. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
import static io.opentelemetry.api.incubator.common.ArrayBackedExtendedAttributesBuilder.toList;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.booleanArrayKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.booleanKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.byteArrayKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.doubleArrayKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.doubleKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.longArrayKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.longKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.mapKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringArrayKey;
import static io.opentelemetry.api.incubator.common.ExtendedAttributeKey.stringKey;

Expand Down Expand Up @@ -93,8 +95,27 @@ default ExtendedAttributesBuilder put(String key, boolean value) {
*
* @return this Builder
*/
default <T> ExtendedAttributesBuilder put(String key, ExtendedAttributes value) {
return put(ExtendedAttributeKey.extendedAttributesKey(key), value);
default ExtendedAttributesBuilder put(String key, ExtendedAttributes value) {
if (value == null) {
return this;
}
return put(mapKey(key), value);
}

/** Puts a byte array attribute into this. */
default ExtendedAttributesBuilder put(String key, byte[] value) {
if (value == null) {
return this;
}
return put(byteArrayKey(key), value);
}

/** Puts an {@link Attributes} attribute into this. */
default ExtendedAttributesBuilder put(String key, Attributes value) {
if (value == null) {
return this;
}
return put(ExtendedAttributeKey.fromAttributeKey(AttributeKey.mapKey(key)), value);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ public static <T> AttributeKey<T> toAttributeKey(ExtendedAttributeKey<T> extende
case DOUBLE_ARRAY:
return InternalAttributeKeyImpl.create(
extendedAttributeKey.getKey(), AttributeType.DOUBLE_ARRAY);
case EXTENDED_ATTRIBUTES:
return null;
case BYTE_ARRAY:
return InternalAttributeKeyImpl.create(
extendedAttributeKey.getKey(), AttributeType.BYTE_ARRAY);
case VALUE_ARRAY:
return InternalAttributeKeyImpl.create(
extendedAttributeKey.getKey(), AttributeType.VALUE_ARRAY);
case MAP:
return InternalAttributeKeyImpl.create(extendedAttributeKey.getKey(), AttributeType.MAP);
}
throw new IllegalArgumentException(
"Unrecognized extendedAttributeKey type: " + extendedAttributeKey.getType());
Expand Down Expand Up @@ -172,6 +178,15 @@ public static <T> ExtendedAttributeKey<T> toExtendedAttributeKey(AttributeKey<T>
case DOUBLE_ARRAY:
return InternalExtendedAttributeKeyImpl.create(
attributeKey.getKey(), ExtendedAttributeType.DOUBLE_ARRAY);
case BYTE_ARRAY:
return InternalExtendedAttributeKeyImpl.create(
attributeKey.getKey(), ExtendedAttributeType.BYTE_ARRAY);
case VALUE_ARRAY:
return InternalExtendedAttributeKeyImpl.create(
attributeKey.getKey(), ExtendedAttributeType.VALUE_ARRAY);
case MAP:
return InternalExtendedAttributeKeyImpl.create(
attributeKey.getKey(), ExtendedAttributeType.MAP);
}
throw new IllegalArgumentException("Unrecognized attributeKey type: " + attributeKey.getType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
import io.opentelemetry.api.incubator.propagation.ExtendedContextPropagators;
import io.opentelemetry.api.internal.ApiUsageLogger;
import io.opentelemetry.api.trace.Span;
Expand Down Expand Up @@ -118,6 +119,11 @@ public <T> NoopSpanBuilder setAttribute(AttributeKey<T> key, T value) {
return this;
}

@Override
public <T> NoopSpanBuilder setAttribute(ExtendedAttributeKey<T> key, T value) {
return this;
}

@Override
public NoopSpanBuilder setAllAttributes(Attributes attributes) {
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.common.ExtendedAttributeKey;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanBuilder;
import io.opentelemetry.api.trace.SpanContext;
Expand Down Expand Up @@ -127,6 +128,17 @@ <E extends Throwable> void startAndRun(
@Override
<T> ExtendedSpanBuilder setAttribute(AttributeKey<T> key, T value);

/**
* Set an attribute.
*
* <p>NOTE: all standard {@link AttributeKey}-value pairs can also be represented as {@link
* ExtendedAttributeKey}-value pairs, but not all {@link ExtendedAttributeKey}-value pairs can be
* represented as standard {@link AttributeKey}-value pairs. From the standpoint of the emitted
* span, there is no difference between adding attributes using the standard or extended attribute
* APIs.
*/
<T> ExtendedSpanBuilder setAttribute(ExtendedAttributeKey<T> key, T value);

/** {@inheritDoc} */
@Override
default ExtendedSpanBuilder setAllAttributes(Attributes attributes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,24 @@ private static Stream<Arguments> attributeKeyArgs() {
ExtendedAttributeType.DOUBLE_ARRAY,
AttributeKey.doubleArrayKey("key")),
Arguments.of(
ExtendedAttributeKey.extendedAttributesKey("key"),
ExtendedAttributeKey.byteArrayKey("key"),
"key",
ExtendedAttributeType.EXTENDED_ATTRIBUTES,
null));
ExtendedAttributeType.BYTE_ARRAY,
AttributeKey.byteArrayKey("key")),
Arguments.of(
ExtendedAttributeKey.valueArrayKey("key"),
"key",
ExtendedAttributeType.VALUE_ARRAY,
AttributeKey.valueArrayKey("key")),
Arguments.of(
ExtendedAttributeKey.fromAttributeKey(AttributeKey.mapKey("key")),
"key",
ExtendedAttributeType.MAP,
AttributeKey.mapKey("key")),
Arguments.of(
ExtendedAttributeKey.mapKey("key"),
"key",
ExtendedAttributeType.MAP,
AttributeKey.mapKey("key")));
}
}
Loading
Loading