From 2701d92e40e0cf3fa2af4ef82010d7fe67dfcc4e Mon Sep 17 00:00:00 2001 From: Jack Berg <34418638+jack-berg@users.noreply.github.com> Date: Mon, 11 May 2026 15:27:04 -0500 Subject: [PATCH 1/2] Remove deprecated ExtendedAttributes and related code --- .../common/ArrayBackedExtendedAttributes.java | 203 ----- .../ArrayBackedExtendedAttributesBuilder.java | 196 ----- .../common/ExtendedAttributeKey.java | 126 --- .../common/ExtendedAttributeType.java | 37 - .../incubator/common/ExtendedAttributes.java | 164 ---- .../common/ExtendedAttributesBuilder.java | 276 ------- .../InternalExtendedAttributeKeyImpl.java | 184 ----- .../incubator/logs/ExtendedDefaultLogger.java | 7 - .../logs/ExtendedLogRecordBuilder.java | 54 +- .../common/ExtendedAttributeKeyTest.java | 91 --- .../common/ExtendedAttributesTest.java | 751 ------------------ .../logs/ExtendedLogsBridgeApiUsageTest.java | 166 ---- .../logging/otlp/TestDataExporter.java | 6 +- ...edAttributeKeyValueStatelessMarshaler.java | 258 ------ .../internal/otlp/IncubatingUtil.java | 147 ---- .../internal/otlp/logs/LogMarshaler.java | 11 +- .../otlp/logs/LogStatelessMarshaler.java | 49 +- .../LogsRequestMarshalerIncubatingTest.java | 75 +- .../internal/ExtendedAttributesMap.java | 128 --- .../internal/ExtendedAttributesValueTest.java | 69 -- .../sdk/logs/ExtendedSdkLogRecordBuilder.java | 52 +- .../sdk/logs/ExtendedSdkLogRecordData.java | 68 -- .../logs/ExtendedSdkReadWriteLogRecord.java | 167 ---- .../sdk/logs/SdkLogRecordBuilder.java | 24 +- .../sdk/logs/SdkReadWriteLogRecord.java | 22 +- .../data/internal/ExtendedLogRecordData.java | 33 - .../internal/ExtendedReadWriteLogRecord.java | 64 -- .../internal/TestExtendedLogRecordData.java | 200 ----- .../testing/assertj/LogAssertionsTest.java | 4 +- 29 files changed, 68 insertions(+), 3564 deletions(-) delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributes.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributesBuilder.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKey.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeType.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributes.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributesBuilder.java delete mode 100644 api/incubator/src/main/java/io/opentelemetry/api/incubator/internal/InternalExtendedAttributeKeyImpl.java delete mode 100644 api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKeyTest.java delete mode 100644 api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java delete mode 100644 exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/ExtendedAttributeKeyValueStatelessMarshaler.java delete mode 100644 exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/IncubatingUtil.java delete mode 100644 sdk/common/src/main/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesMap.java delete mode 100644 sdk/common/src/test/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesValueTest.java delete mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordData.java delete mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkReadWriteLogRecord.java delete mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/internal/ExtendedLogRecordData.java delete mode 100644 sdk/logs/src/main/java/io/opentelemetry/sdk/logs/internal/ExtendedReadWriteLogRecord.java delete mode 100644 sdk/testing/src/main/java/io/opentelemetry/sdk/testing/logs/internal/TestExtendedLogRecordData.java diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributes.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributes.java deleted file mode 100644 index 6390138ce28..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributes.java +++ /dev/null @@ -1,203 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.AttributesBuilder; -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.common.ValueType; -import io.opentelemetry.api.internal.ImmutableKeyValuePairs; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -@SuppressWarnings("deprecation") -@Immutable -final class ArrayBackedExtendedAttributes - extends ImmutableKeyValuePairs, Object> implements ExtendedAttributes { - - // We only compare the key name, not type, when constructing, to allow deduping keys with the - // same name but different type. - private static final Comparator> KEY_COMPARATOR_FOR_CONSTRUCTION = - Comparator.comparing(ExtendedAttributeKey::getKey); - - static final ExtendedAttributes EMPTY = ExtendedAttributes.builder().build(); - - @Nullable private Attributes attributes; - - private ArrayBackedExtendedAttributes( - Object[] data, Comparator> keyComparator) { - super(data, keyComparator); - } - - /** - * Only use this constructor if you can guarantee that the data has been de-duped, sorted by key - * and contains no null values or null/empty keys. - * - * @param data the raw data - */ - ArrayBackedExtendedAttributes(Object[] data) { - super(data); - } - - @Override - public ExtendedAttributesBuilder toBuilder() { - return new ArrayBackedExtendedAttributesBuilder(new ArrayList<>(data())); - } - - @SuppressWarnings("unchecked") - @Override - @Nullable - public T get(ExtendedAttributeKey key) { - if (key == null) { - return null; - } - if (key.getType() == ExtendedAttributeType.VALUE) { - return (T) getAsValue(key.getKey()); - } - // Check if we're looking for an array type but have a VALUE with empty array - if (isArrayType(key.getType())) { - T value = (T) super.get(key); - if (value == null) { - // Check if there's a VALUE with the same key that contains an empty array - Value valueAttr = getValueAttribute(key.getKey()); - if (valueAttr != null && isEmptyArray(valueAttr)) { - return (T) Collections.emptyList(); - } - } - return value; - } - return (T) super.get(key); - } - - private static boolean isArrayType(ExtendedAttributeType type) { - return type == ExtendedAttributeType.STRING_ARRAY - || type == ExtendedAttributeType.LONG_ARRAY - || type == ExtendedAttributeType.DOUBLE_ARRAY - || type == ExtendedAttributeType.BOOLEAN_ARRAY; - } - - @Nullable - private Value getValueAttribute(String keyName) { - List data = data(); - for (int i = 0; i < data.size(); i += 2) { - ExtendedAttributeKey currentKey = (ExtendedAttributeKey) data.get(i); - if (currentKey.getKey().equals(keyName) - && currentKey.getType() == ExtendedAttributeType.VALUE) { - return (Value) data.get(i + 1); - } - } - return null; - } - - private static boolean isEmptyArray(Value value) { - if (value.getType() != ValueType.ARRAY) { - return false; - } - @SuppressWarnings("unchecked") - List> arrayValues = (List>) value.getValue(); - return arrayValues.isEmpty(); - } - - @Nullable - private Value getAsValue(String keyName) { - // Find any attribute with the same key name and convert it to Value - List data = data(); - for (int i = 0; i < data.size(); i += 2) { - ExtendedAttributeKey currentKey = (ExtendedAttributeKey) data.get(i); - if (currentKey.getKey().equals(keyName)) { - Object value = data.get(i + 1); - return asValue(currentKey.getType(), value); - } - } - return null; - } - - @SuppressWarnings({"unchecked", "deprecation"}) // deprecation: EXTENDED_ATTRIBUTES - @Nullable - private static Value asValue(ExtendedAttributeType type, Object value) { - switch (type) { - case STRING: - return Value.of((String) value); - case LONG: - return Value.of((Long) value); - case DOUBLE: - return Value.of((Double) value); - case BOOLEAN: - return Value.of((Boolean) value); - case STRING_ARRAY: - List stringList = (List) value; - Value[] stringValues = new Value[stringList.size()]; - for (int i = 0; i < stringList.size(); i++) { - stringValues[i] = Value.of(stringList.get(i)); - } - return Value.of(stringValues); - case LONG_ARRAY: - List longList = (List) value; - Value[] longValues = new Value[longList.size()]; - for (int i = 0; i < longList.size(); i++) { - longValues[i] = Value.of(longList.get(i)); - } - return Value.of(longValues); - case DOUBLE_ARRAY: - List doubleList = (List) value; - Value[] doubleValues = new Value[doubleList.size()]; - for (int i = 0; i < doubleList.size(); i++) { - doubleValues[i] = Value.of(doubleList.get(i)); - } - return Value.of(doubleValues); - case BOOLEAN_ARRAY: - List booleanList = (List) value; - Value[] booleanValues = new Value[booleanList.size()]; - for (int i = 0; i < booleanList.size(); i++) { - booleanValues[i] = Value.of(booleanList.get(i)); - } - return Value.of(booleanValues); - case VALUE: - // Already a Value - return (Value) value; - case EXTENDED_ATTRIBUTES: - // Cannot convert EXTENDED_ATTRIBUTES to Value - return null; - } - // Should not reach here - return null; - } - - @SuppressWarnings("unchecked") - @Override - public Attributes asAttributes() { - if (attributes == null) { - AttributesBuilder builder = Attributes.builder(); - forEach( - (extendedAttributeKey, value) -> { - AttributeKey attributeKey = - (AttributeKey) extendedAttributeKey.asAttributeKey(); - if (attributeKey != null) { - builder.put(attributeKey, value); - } - }); - attributes = builder.build(); - } - return attributes; - } - - static ExtendedAttributes sortAndFilterToAttributes(Object... data) { - // null out any empty keys or keys with null values - // so they will then be removed by the sortAndFilter method. - for (int i = 0; i < data.length; i += 2) { - ExtendedAttributeKey key = (ExtendedAttributeKey) data[i]; - if (key != null && key.getKey().isEmpty()) { - data[i] = null; - } - } - return new ArrayBackedExtendedAttributes(data, KEY_COMPARATOR_FOR_CONSTRUCTION); - } -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributesBuilder.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributesBuilder.java deleted file mode 100644 index 22c6ffc6870..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ArrayBackedExtendedAttributesBuilder.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.common.ValueType; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.function.Predicate; - -@SuppressWarnings("deprecation") -class ArrayBackedExtendedAttributesBuilder implements ExtendedAttributesBuilder { - private final List data; - - ArrayBackedExtendedAttributesBuilder() { - data = new ArrayList<>(); - } - - ArrayBackedExtendedAttributesBuilder(List data) { - this.data = data; - } - - @Override - public ExtendedAttributes build() { - // If only one key-value pair AND the entry hasn't been set to null (by #remove(AttributeKey) - // or #removeIf(Predicate>)), then we can bypass sorting and filtering - if (data.size() == 2 && data.get(0) != null) { - return new ArrayBackedExtendedAttributes(data.toArray()); - } - return ArrayBackedExtendedAttributes.sortAndFilterToAttributes(data.toArray()); - } - - @Override - public ExtendedAttributesBuilder put(ExtendedAttributeKey key, T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return this; - } - if (key.getType() == ExtendedAttributeType.VALUE && value instanceof Value) { - putValue(key, (Value) value); - return this; - } - data.add(key); - data.add(value); - return this; - } - - @SuppressWarnings({"unchecked", "deprecation"}) // deprecation: EXTENDED_ATTRIBUTES - private void putValue(ExtendedAttributeKey key, Value valueObj) { - // Convert VALUE type to narrower type when possible - String keyName = key.getKey(); - switch (valueObj.getType()) { - case STRING: - put(ExtendedAttributeKey.stringKey(keyName), ((Value) valueObj).getValue()); - return; - case LONG: - put(ExtendedAttributeKey.longKey(keyName), ((Value) valueObj).getValue()); - return; - case DOUBLE: - put(ExtendedAttributeKey.doubleKey(keyName), ((Value) valueObj).getValue()); - return; - case BOOLEAN: - put(ExtendedAttributeKey.booleanKey(keyName), ((Value) valueObj).getValue()); - return; - case ARRAY: - List> arrayValues = (List>) valueObj.getValue(); - ExtendedAttributeType attributeType = attributeType(arrayValues); - switch (attributeType) { - case STRING_ARRAY: - List strings = new ArrayList<>(arrayValues.size()); - for (Value v : arrayValues) { - strings.add((String) v.getValue()); - } - put(ExtendedAttributeKey.stringArrayKey(keyName), strings); - return; - case LONG_ARRAY: - List longs = new ArrayList<>(arrayValues.size()); - for (Value v : arrayValues) { - longs.add((Long) v.getValue()); - } - put(ExtendedAttributeKey.longArrayKey(keyName), longs); - return; - case DOUBLE_ARRAY: - List doubles = new ArrayList<>(arrayValues.size()); - for (Value v : arrayValues) { - doubles.add((Double) v.getValue()); - } - put(ExtendedAttributeKey.doubleArrayKey(keyName), doubles); - return; - case BOOLEAN_ARRAY: - List booleans = new ArrayList<>(arrayValues.size()); - for (Value v : arrayValues) { - booleans.add((Boolean) v.getValue()); - } - put(ExtendedAttributeKey.booleanArrayKey(keyName), booleans); - return; - case VALUE: - // Not coercible (empty, non-homogeneous, or unsupported element type) - data.add(key); - data.add(valueObj); - return; - case EXTENDED_ATTRIBUTES: - // Not coercible - data.add(key); - data.add(valueObj); - return; - default: - throw new IllegalArgumentException("Unexpected array attribute type: " + attributeType); - } - case KEY_VALUE_LIST: - case BYTES: - case EMPTY: - // Keep as VALUE type - data.add(key); - data.add(valueObj); - return; - } - } - - /** - * Returns the ExtendedAttributeType for a homogeneous array (STRING_ARRAY, LONG_ARRAY, - * DOUBLE_ARRAY, or BOOLEAN_ARRAY), or VALUE if the array is empty, non-homogeneous, or contains - * unsupported element types. - */ - private static ExtendedAttributeType attributeType(List> arrayValues) { - if (arrayValues.isEmpty()) { - return ExtendedAttributeType.VALUE; - } - ValueType elementType = arrayValues.get(0).getType(); - for (Value v : arrayValues) { - if (v.getType() != elementType) { - return ExtendedAttributeType.VALUE; - } - } - switch (elementType) { - case STRING: - return ExtendedAttributeType.STRING_ARRAY; - case LONG: - return ExtendedAttributeType.LONG_ARRAY; - case DOUBLE: - return ExtendedAttributeType.DOUBLE_ARRAY; - case BOOLEAN: - return ExtendedAttributeType.BOOLEAN_ARRAY; - case ARRAY: - case KEY_VALUE_LIST: - case BYTES: - case EMPTY: - return ExtendedAttributeType.VALUE; - } - throw new IllegalArgumentException("Unsupported element type: " + elementType); - } - - @Override - public ExtendedAttributesBuilder removeIf(Predicate> predicate) { - if (predicate == null) { - return this; - } - for (int i = 0; i < data.size() - 1; i += 2) { - Object entry = data.get(i); - if (entry instanceof ExtendedAttributeKey - && predicate.test((ExtendedAttributeKey) entry)) { - // null items are filtered out in ArrayBackedAttributes - data.set(i, null); - data.set(i + 1, null); - } - } - return this; - } - - static List toList(double... values) { - Double[] boxed = new Double[values.length]; - for (int i = 0; i < values.length; i++) { - boxed[i] = values[i]; - } - return Arrays.asList(boxed); - } - - static List toList(long... values) { - Long[] boxed = new Long[values.length]; - for (int i = 0; i < values.length; i++) { - boxed[i] = values[i]; - } - return Arrays.asList(boxed); - } - - static List toList(boolean... values) { - Boolean[] boxed = new Boolean[values.length]; - for (int i = 0; i < values.length; i++) { - boxed[i] = values[i]; - } - return Arrays.asList(boxed); - } -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKey.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKey.java deleted file mode 100644 index 89a90efe8c6..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKey.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -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; -import javax.annotation.concurrent.Immutable; - -/** - * This interface provides a handle for setting the values of {@link ExtendedAttributes}. The type - * of value that can be set with an implementation of this key is denoted by the type parameter. - * - *

Implementations MUST be immutable, as these are used as the keys to Maps. - * - *

The allowed {@link #getType()}s is a superset of those allowed in {@link AttributeKey}. - * - *

Convenience methods are provided for translating to / from {@link AttributeKey}: - * - *

    - *
  • {@link #asAttributeKey()} converts from {@link ExtendedAttributeKey} to {@link - * AttributeKey} - *
  • {@link #fromAttributeKey(AttributeKey)} converts from {@link AttributeKey} to {@link - * ExtendedAttributeKey} - *
- * - * @param The type of value that can be set with the key. - * @deprecated Use {@link AttributeKey} with {@link AttributeKey#valueKey(String)} instead. - */ -@Deprecated -@Immutable -public interface ExtendedAttributeKey { - /** Returns the underlying String representation of the key. */ - String getKey(); - - /** Returns the type of attribute for this key. Useful for building switch statements. */ - ExtendedAttributeType getType(); - - /** - * Return the equivalent {@link AttributeKey}, or {@code null} if the {@link #getType()} has no - * equivalent {@link io.opentelemetry.api.common.AttributeType}. - */ - @Nullable - default AttributeKey asAttributeKey() { - return InternalExtendedAttributeKeyImpl.toAttributeKey(this); - } - - /** Return an ExtendedAttributeKey equivalent to the {@code attributeKey}. */ - // TODO (jack-berg): remove once AttributeKey.asExtendedAttributeKey is available - static ExtendedAttributeKey fromAttributeKey(AttributeKey attributeKey) { - return InternalExtendedAttributeKeyImpl.toExtendedAttributeKey(attributeKey); - } - - /** Returns a new ExtendedAttributeKey for String valued attributes. */ - static ExtendedAttributeKey stringKey(String key) { - return fromAttributeKey(AttributeKey.stringKey(key)); - } - - /** Returns a new ExtendedAttributeKey for Boolean valued attributes. */ - static ExtendedAttributeKey booleanKey(String key) { - return fromAttributeKey(AttributeKey.booleanKey(key)); - } - - /** Returns a new ExtendedAttributeKey for Long valued attributes. */ - static ExtendedAttributeKey longKey(String key) { - return fromAttributeKey(AttributeKey.longKey(key)); - } - - /** Returns a new ExtendedAttributeKey for Double valued attributes. */ - static ExtendedAttributeKey doubleKey(String key) { - return fromAttributeKey(AttributeKey.doubleKey(key)); - } - - /** Returns a new ExtendedAttributeKey for List<String> valued attributes. */ - static ExtendedAttributeKey> stringArrayKey(String key) { - return fromAttributeKey(AttributeKey.stringArrayKey(key)); - } - - /** Returns a new ExtendedAttributeKey for List<Boolean> valued attributes. */ - static ExtendedAttributeKey> booleanArrayKey(String key) { - return fromAttributeKey(AttributeKey.booleanArrayKey(key)); - } - - /** Returns a new ExtendedAttributeKey for List<Long> valued attributes. */ - static ExtendedAttributeKey> longArrayKey(String key) { - return fromAttributeKey(AttributeKey.longArrayKey(key)); - } - - /** Returns a new ExtendedAttributeKey for List<Double> valued attributes. */ - static ExtendedAttributeKey> doubleArrayKey(String key) { - return fromAttributeKey(AttributeKey.doubleArrayKey(key)); - } - - /** - * Returns a new ExtendedAttributeKey for {@link ExtendedAttributes} valued attributes. - * - * @deprecated Use {@link #valueKey(String)} in combination with {@link Value#of(java.util.Map)} - * instead. - */ - @Deprecated - @SuppressWarnings("deprecation") - static ExtendedAttributeKey extendedAttributesKey(String key) { - return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.EXTENDED_ATTRIBUTES); - } - - /** - * Returns a new ExtendedAttributeKey for {@link Value} valued attributes. - * - *

Simple attributes ({@link ExtendedAttributeType#STRING}, {@link ExtendedAttributeType#LONG}, - * {@link ExtendedAttributeType#DOUBLE}, {@link ExtendedAttributeType#BOOLEAN}, {@link - * ExtendedAttributeType#STRING_ARRAY}, {@link ExtendedAttributeType#LONG_ARRAY}, {@link - * ExtendedAttributeType#DOUBLE_ARRAY}, {@link ExtendedAttributeType#BOOLEAN_ARRAY}) SHOULD be - * used whenever possible. Instrumentations SHOULD assume that backends do not index individual - * properties of complex attributes, that querying or aggregating on such properties is - * inefficient and complicated, and that reporting complex attributes carries higher performance - * overhead. - */ - static ExtendedAttributeKey> valueKey(String key) { - return InternalExtendedAttributeKeyImpl.create(key, ExtendedAttributeType.VALUE); - } -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeType.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeType.java deleted file mode 100644 index e032e659a31..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributeType.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -/** - * An enum that represents all the possible value types for an {@link ExtendedAttributeKey} and - * hence the types of values that are allowed for {@link ExtendedAttributes}. - * - *

This is a superset of {@link io.opentelemetry.api.common.AttributeType}, - * - * @deprecated Use {@link io.opentelemetry.api.common.AttributeType} instead. - */ -@Deprecated -public enum ExtendedAttributeType { - // Types copied AttributeType - STRING, - BOOLEAN, - LONG, - DOUBLE, - STRING_ARRAY, - BOOLEAN_ARRAY, - LONG_ARRAY, - DOUBLE_ARRAY, - // Extended types unique to ExtendedAttributes - /** - * Complex attribute type for {@link io.opentelemetry.api.common.Value}-based maps. - * - * @deprecated Use {@link #VALUE} with {@link io.opentelemetry.api.common.Value}-based maps - * instead. - */ - @Deprecated - EXTENDED_ATTRIBUTES, - VALUE; -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributes.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributes.java deleted file mode 100644 index 5fe75be4cb5..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributes.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; -import java.util.Map; -import java.util.function.BiConsumer; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * An immutable container for extended attributes. - * - *

"extended" refers an extended set of allowed value types compared to standard {@link - * Attributes}. Notably, {@link ExtendedAttributes} values can be of type {@link - * ExtendedAttributeType#VALUE}, allowing attributes backed by {@link - * io.opentelemetry.api.common.Value}. - * - *

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 - * (e.g. {@link ExtendedLogRecordBuilder#setAttribute(ExtendedAttributeKey, Object)}). - * - *

The keys are {@link ExtendedAttributeKey}s and the values are Object instances that match the - * type of the provided key. - * - *

Null keys will be silently dropped. - * - *

Note: The behavior of null-valued attributes is undefined, and hence strongly discouraged. - * - *

Implementations of this interface *must* be immutable and have well-defined value-based - * equals/hashCode implementations. If an implementation does not strictly conform to these - * requirements, behavior of the OpenTelemetry APIs and default SDK cannot be guaranteed. - * - *

For this reason, it is strongly suggested that you use the implementation that is provided - * here via the factory methods and the {@link ExtendedAttributesBuilder}. - * - *

Convenience methods are provided for translating to / from {@link Attributes}: - * - *

    - *
  • {@link #asAttributes()} converts from {@link ExtendedAttributes} to {@link Attributes} - *
  • {@link ExtendedAttributesBuilder#putAll(Attributes)} converts from {@link Attributes} to - * {@link ExtendedAttributes} - *
  • {@link #get(AttributeKey)} supports reading values using standard {@link AttributeKey} - *
- * - * @deprecated Use {@link Attributes} with {@link - * io.opentelemetry.api.common.AttributeKey#valueKey(String)} instead. - */ -@Deprecated -@Immutable -public interface ExtendedAttributes { - - /** Returns the value for the given {@link AttributeKey}, or {@code null} if not found. */ - @Nullable - default T get(AttributeKey key) { - if (key == null) { - return null; - } - return get(ExtendedAttributeKey.fromAttributeKey(key)); - } - - /** - * Returns the value for the given {@link ExtendedAttributeKey}, or {@code null} if not found. - * - *

Note: this method will automatically return the corresponding {@link - * io.opentelemetry.api.common.Value} instance when passed a key of type {@link - * ExtendedAttributeType#VALUE} and a simple attribute is found. This is the inverse of {@link - * ExtendedAttributesBuilder#put(ExtendedAttributeKey, Object)} when the key is {@link - * ExtendedAttributeType#VALUE}. - * - *

    - *
  • If {@code put(ExtendedAttributeKey.stringKey("key"), "a")} was called, then {@code - * get(ExtendedAttributeKey.valueKey("key"))} returns {@code Value.of("a")}. - *
  • If {@code put(ExtendedAttributeKey.longKey("key"), 1L)} was called, then {@code - * get(ExtendedAttributeKey.valueKey("key"))} returns {@code Value.of(1L)}. - *
  • If {@code put(ExtendedAttributeKey.doubleKey("key"), 1.0)} was called, then {@code - * get(ExtendedAttributeKey.valueKey("key"))} returns {@code Value.of(1.0)}. - *
  • If {@code put(ExtendedAttributeKey.booleanKey("key"), true)} was called, then {@code - * get(ExtendedAttributeKey.valueKey("key"))} returns {@code Value.of(true)}. - *
  • If {@code put(ExtendedAttributeKey.stringArrayKey("key"), Arrays.asList("a", "b"))} was - * called, then {@code get(ExtendedAttributeKey.valueKey("key"))} returns {@code - * Value.of(Value.of("a"), Value.of("b"))}. - *
  • If {@code put(ExtendedAttributeKey.longArrayKey("key"), Arrays.asList(1L, 2L))} was - * called, then {@code get(ExtendedAttributeKey.valueKey("key"))} returns {@code - * Value.of(Value.of(1L), Value.of(2L))}. - *
  • If {@code put(ExtendedAttributeKey.doubleArrayKey("key"), Arrays.asList(1.0, 2.0))} was - * called, then {@code get(ExtendedAttributeKey.valueKey("key"))} returns {@code - * Value.of(Value.of(1.0), Value.of(2.0))}. - *
  • If {@code put(ExtendedAttributeKey.booleanArrayKey("key"), Arrays.asList(true, false))} - * was called, then {@code get(ExtendedAttributeKey.valueKey("key"))} returns {@code - * Value.of(Value.of(true), Value.of(false))}. - *
- * - *

Further, if {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(emptyList()))} was - * called, then - * - *

    - *
  • {@code get(ExtendedAttributeKey.stringArrayKey("key"))} - *
  • {@code get(ExtendedAttributeKey.longArrayKey("key"))} - *
  • {@code get(ExtendedAttributeKey.booleanArrayKey("key"))} - *
  • {@code get(ExtendedAttributeKey.doubleArrayKey("key"))} - *
- * - *

all return an empty list (as opposed to {@code null}). - */ - @Nullable - T get(ExtendedAttributeKey key); - - /** - * Iterates over all the key-value pairs of attributes contained by this instance. - * - *

Note: {@link ExtendedAttributeType#VALUE} attributes will be represented as simple - * attributes if possible. See {@link ExtendedAttributesBuilder#put(ExtendedAttributeKey, Object)} - * for more details. - */ - void forEach(BiConsumer, ? super Object> consumer); - - /** The number of attributes contained in this. */ - int size(); - - /** Whether there are any attributes contained in this. */ - boolean isEmpty(); - - /** - * Returns a read-only view of this {@link ExtendedAttributes} as a {@link Map}. - * - *

Note: {@link ExtendedAttributeType#VALUE} attributes will be represented as simple - * attributes in this map if possible. See {@link - * ExtendedAttributesBuilder#put(ExtendedAttributeKey, Object)} for more details. - */ - Map, Object> asMap(); - - /** - * Return a view of this extended attributes with entries limited to those representable as - * standard attributes. - */ - Attributes asAttributes(); - - /** Returns a {@link ExtendedAttributes} instance with no attributes. */ - static ExtendedAttributes empty() { - return ArrayBackedExtendedAttributes.EMPTY; - } - - /** - * Returns a new {@link ExtendedAttributesBuilder} instance for creating arbitrary {@link - * ExtendedAttributes}. - * - * @return a new {@link ExtendedAttributesBuilder} instance - */ - static ExtendedAttributesBuilder builder() { - return new ArrayBackedExtendedAttributesBuilder(); - } - - /** - * Returns a new {@link ExtendedAttributesBuilder} instance populated with the data of this {@link - * ExtendedAttributes}. - */ - ExtendedAttributesBuilder toBuilder(); -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributesBuilder.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributesBuilder.java deleted file mode 100644 index a8f7fdf72a4..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/common/ExtendedAttributesBuilder.java +++ /dev/null @@ -1,276 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.Value; -import java.util.Arrays; -import java.util.List; -import java.util.function.Predicate; - -/** - * A builder of {@link ExtendedAttributes} supporting an arbitrary number of key-value pairs. - * - * @deprecated Use {@link io.opentelemetry.api.common.AttributesBuilder} with {@link - * io.opentelemetry.api.common.AttributeKey#valueKey(String)} instead. - */ -@Deprecated -public interface ExtendedAttributesBuilder { - /** Create the {@link ExtendedAttributes} from this. */ - ExtendedAttributes build(); - - /** Puts a {@link AttributeKey} with associated value into this. */ - default ExtendedAttributesBuilder put(AttributeKey key, T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return this; - } - return put(ExtendedAttributeKey.fromAttributeKey(key), value); - } - - /** - * Puts an {@link ExtendedAttributeKey} with an associated value into this if the value is - * non-null. Providing a null value does not remove or unset previously set values. - * - *

Simple attributes ({@link ExtendedAttributeType#STRING}, {@link ExtendedAttributeType#LONG}, - * {@link ExtendedAttributeType#DOUBLE}, {@link ExtendedAttributeType#BOOLEAN}, {@link - * ExtendedAttributeType#STRING_ARRAY}, {@link ExtendedAttributeType#LONG_ARRAY}, {@link - * ExtendedAttributeType#DOUBLE_ARRAY}, {@link ExtendedAttributeType#BOOLEAN_ARRAY}) SHOULD be - * used whenever possible. Instrumentations SHOULD assume that backends do not index individual - * properties of complex attributes, that querying or aggregating on such properties is - * inefficient and complicated, and that reporting complex attributes carries higher performance - * overhead. - * - *

Note: This method will automatically convert complex attributes ({@link - * ExtendedAttributeType#VALUE}) to simple attributes when possible. - * - *

    - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of("a"))} is equivalent to - * calling {@code put(ExtendedAttributeKey.stringKey("key"), "a")}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(1L))} is equivalent to - * calling {@code put(ExtendedAttributeKey.longKey("key"), 1L)}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(1.0))} is equivalent to - * calling {@code put(ExtendedAttributeKey.doubleKey("key"), 1.0)}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(true))} is equivalent - * to calling {@code put(ExtendedAttributeKey.booleanKey("key"), true)}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(Value.of("a"), - * Value.of("b")))} is equivalent to calling {@code - * put(ExtendedAttributeKey.stringArrayKey("key"), Arrays.asList("a", "b"))}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(Value.of(1L), - * Value.of(2L)))} is equivalent to calling {@code - * put(ExtendedAttributeKey.longArrayKey("key"), Arrays.asList(1L, 2L))}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(Value.of(1.0), - * Value.of(2.0)))} is equivalent to calling {@code - * put(ExtendedAttributeKey.doubleArrayKey("key"), Arrays.asList(1.0, 2.0))}. - *
  • Calling {@code put(ExtendedAttributeKey.valueKey("key"), Value.of(Value.of(true), - * Value.of(false)))} is equivalent to calling {@code - * put(ExtendedAttributeKey.booleanArrayKey("key"), Arrays.asList(true, false))}. - *
- */ - ExtendedAttributesBuilder put(ExtendedAttributeKey key, T value); - - /** - * Puts a String attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, String value) { - return put(ExtendedAttributeKey.stringKey(key), value); - } - - /** - * Puts a long attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, long value) { - return put(ExtendedAttributeKey.longKey(key), value); - } - - /** - * Puts a double attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, double value) { - return put(ExtendedAttributeKey.doubleKey(key), value); - } - - /** - * Puts a boolean attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, boolean value) { - return put(ExtendedAttributeKey.booleanKey(key), value); - } - - /** - * Puts a {@link ExtendedAttributes} attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - * @deprecated Use {@link #put(ExtendedAttributeKey, Object)} with {@link Value#of(java.util.Map)} - * instead. - */ - @Deprecated - default ExtendedAttributesBuilder put(String key, ExtendedAttributes value) { - return put(ExtendedAttributeKey.extendedAttributesKey(key), value); - } - - /** - * Puts a String array attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, String... value) { - if (value == null) { - return this; - } - return put(ExtendedAttributeKey.stringArrayKey(key), Arrays.asList(value)); - } - - /** - * Puts a List attribute into this. - * - * @return this Builder - */ - @SuppressWarnings("unchecked") - default ExtendedAttributesBuilder put(AttributeKey> key, T... value) { - if (value == null) { - return this; - } - return put(key, Arrays.asList(value)); - } - - /** - * Puts a Long array attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, long... value) { - if (value == null) { - return this; - } - return put( - ExtendedAttributeKey.longArrayKey(key), ArrayBackedExtendedAttributesBuilder.toList(value)); - } - - /** - * Puts a Double array attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, double... value) { - if (value == null) { - return this; - } - return put( - ExtendedAttributeKey.doubleArrayKey(key), - ArrayBackedExtendedAttributesBuilder.toList(value)); - } - - /** - * Puts a Boolean array attribute into this. - * - *

Note: It is strongly recommended to use {@link #put(ExtendedAttributeKey, Object)}, and - * pre-allocate your keys, if possible. - * - * @return this Builder - */ - default ExtendedAttributesBuilder put(String key, boolean... value) { - if (value == null) { - return this; - } - return put( - ExtendedAttributeKey.booleanArrayKey(key), - ArrayBackedExtendedAttributesBuilder.toList(value)); - } - - /** - * Puts all the provided attributes into this Builder. - * - * @return this Builder - */ - @SuppressWarnings({"unchecked"}) - default ExtendedAttributesBuilder putAll(Attributes attributes) { - if (attributes == null) { - return this; - } - attributes.forEach((key, value) -> put((AttributeKey) key, value)); - return this; - } - - /** - * Puts all the provided attributes into this Builder. - * - * @return this Builder - */ - @SuppressWarnings({"unchecked"}) - default ExtendedAttributesBuilder putAll(ExtendedAttributes attributes) { - if (attributes == null) { - return this; - } - attributes.forEach((key, value) -> put((ExtendedAttributeKey) key, value)); - return this; - } - - /** - * Remove all attributes where {@link AttributeKey#getKey()} and {@link AttributeKey#getType()} - * match the {@code key}. - * - * @return this Builder - */ - default ExtendedAttributesBuilder remove(AttributeKey key) { - return remove(ExtendedAttributeKey.fromAttributeKey(key)); - } - - /** - * Remove all attributes where {@link ExtendedAttributeKey#getKey()} and {@link - * ExtendedAttributeKey#getType()} match the {@code key}. - * - * @return this Builder - */ - default ExtendedAttributesBuilder remove(ExtendedAttributeKey key) { - if (key == null || key.getKey().isEmpty()) { - return this; - } - return removeIf( - entryKey -> - key.getKey().equals(entryKey.getKey()) && key.getType().equals(entryKey.getType())); - } - - /** - * Remove all attributes that satisfy the given predicate. Errors or runtime exceptions thrown by - * the predicate are relayed to the caller. - * - * @return this Builder - */ - ExtendedAttributesBuilder removeIf(Predicate> filter); -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/internal/InternalExtendedAttributeKeyImpl.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/internal/InternalExtendedAttributeKeyImpl.java deleted file mode 100644 index a4ea4acd059..00000000000 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/internal/InternalExtendedAttributeKeyImpl.java +++ /dev/null @@ -1,184 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.internal; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.AttributeType; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributeType; -import io.opentelemetry.api.internal.InternalAttributeKeyImpl; -import java.nio.charset.StandardCharsets; -import javax.annotation.Nullable; - -/** - * This class is internal and is hence not for public use. Its APIs are unstable and can change at - * any time. - */ -@SuppressWarnings("deprecation") -public final class InternalExtendedAttributeKeyImpl implements ExtendedAttributeKey { - - private final ExtendedAttributeType type; - private final String key; - private final int hashCode; - - @Nullable private byte[] keyUtf8; - @Nullable private AttributeKey attributeKey; - - private InternalExtendedAttributeKeyImpl(ExtendedAttributeType type, String key) { - if (type == null) { - throw new NullPointerException("Null type"); - } - this.type = type; - if (key == null) { - throw new NullPointerException("Null key"); - } - this.key = key; - this.hashCode = buildHashCode(type, key); - } - - public static ExtendedAttributeKey create( - @Nullable String key, ExtendedAttributeType type) { - return new InternalExtendedAttributeKeyImpl<>(type, key != null ? key : ""); - } - - @Override - public ExtendedAttributeType getType() { - return type; - } - - @Override - public String getKey() { - return key; - } - - @Nullable - @Override - public AttributeKey asAttributeKey() { - if (attributeKey == null) { - attributeKey = toAttributeKey(this); - } - return attributeKey; - } - - /** Returns the key, encoded as UTF-8 bytes. */ - public byte[] getKeyUtf8() { - byte[] keyUtf8 = this.keyUtf8; - if (keyUtf8 == null) { - keyUtf8 = key.getBytes(StandardCharsets.UTF_8); - this.keyUtf8 = keyUtf8; - } - return keyUtf8; - } - - @Override - public boolean equals(@Nullable Object o) { - if (o == this) { - return true; - } - if (o instanceof InternalExtendedAttributeKeyImpl) { - InternalExtendedAttributeKeyImpl that = (InternalExtendedAttributeKeyImpl) o; - return this.type.equals(that.getType()) && this.key.equals(that.getKey()); - } - return false; - } - - @Override - public int hashCode() { - return hashCode; - } - - @Override - public String toString() { - return key; - } - - // this method exists to make EqualsVerifier happy - @SuppressWarnings("unused") - private int buildHashCode() { - return buildHashCode(type, key); - } - - private static int buildHashCode(ExtendedAttributeType type, String key) { - int result = 1; - result *= 1000003; - result ^= type.hashCode(); - result *= 1000003; - result ^= key.hashCode(); - return result; - } - - /** - * Return the equivalent {@link AttributeKey} for the {@link ExtendedAttributeKey}, or {@code - * null} if the {@link #getType()} has no equivalent {@link - * io.opentelemetry.api.common.AttributeType}. - */ - @Nullable - public static AttributeKey toAttributeKey(ExtendedAttributeKey extendedAttributeKey) { - switch (extendedAttributeKey.getType()) { - case STRING: - return InternalAttributeKeyImpl.create(extendedAttributeKey.getKey(), AttributeType.STRING); - case BOOLEAN: - return InternalAttributeKeyImpl.create( - extendedAttributeKey.getKey(), AttributeType.BOOLEAN); - case LONG: - return InternalAttributeKeyImpl.create(extendedAttributeKey.getKey(), AttributeType.LONG); - case DOUBLE: - return InternalAttributeKeyImpl.create(extendedAttributeKey.getKey(), AttributeType.DOUBLE); - case STRING_ARRAY: - return InternalAttributeKeyImpl.create( - extendedAttributeKey.getKey(), AttributeType.STRING_ARRAY); - case BOOLEAN_ARRAY: - return InternalAttributeKeyImpl.create( - extendedAttributeKey.getKey(), AttributeType.BOOLEAN_ARRAY); - case LONG_ARRAY: - return InternalAttributeKeyImpl.create( - extendedAttributeKey.getKey(), AttributeType.LONG_ARRAY); - case DOUBLE_ARRAY: - return InternalAttributeKeyImpl.create( - extendedAttributeKey.getKey(), AttributeType.DOUBLE_ARRAY); - case VALUE: - return InternalAttributeKeyImpl.create(extendedAttributeKey.getKey(), AttributeType.VALUE); - case EXTENDED_ATTRIBUTES: - return null; - } - throw new IllegalArgumentException( - "Unrecognized extendedAttributeKey type: " + extendedAttributeKey.getType()); - } - - /** Return the equivalent {@link ExtendedAttributeKey} for the {@link AttributeKey}. */ - public static ExtendedAttributeKey toExtendedAttributeKey(AttributeKey attributeKey) { - switch (attributeKey.getType()) { - case STRING: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.STRING); - case BOOLEAN: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.BOOLEAN); - case LONG: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.LONG); - case DOUBLE: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.DOUBLE); - case STRING_ARRAY: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.STRING_ARRAY); - case BOOLEAN_ARRAY: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.BOOLEAN_ARRAY); - case LONG_ARRAY: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.LONG_ARRAY); - case DOUBLE_ARRAY: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.DOUBLE_ARRAY); - case VALUE: - return InternalExtendedAttributeKeyImpl.create( - attributeKey.getKey(), ExtendedAttributeType.VALUE); - } - throw new IllegalArgumentException("Unrecognized attributeKey type: " + attributeKey.getType()); - } -} diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java index 77b7970ecf0..c9a163d0aec 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedDefaultLogger.java @@ -7,7 +7,6 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.Severity; import io.opentelemetry.context.Context; @@ -15,7 +14,6 @@ import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; -@SuppressWarnings("deprecation") class ExtendedDefaultLogger implements ExtendedLogger { private static final Logger INSTANCE = new ExtendedDefaultLogger(); @@ -52,11 +50,6 @@ public ExtendedLogRecordBuilder setException(Throwable throwable) { return this; } - @Override - public ExtendedLogRecordBuilder setAttribute(ExtendedAttributeKey key, T value) { - return this; - } - @Override public ExtendedLogRecordBuilder setAttribute(AttributeKey key, @Nullable T value) { return this; diff --git a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java index 5e9eb383f62..740ca827622 100644 --- a/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java +++ b/api/incubator/src/main/java/io/opentelemetry/api/incubator/logs/ExtendedLogRecordBuilder.java @@ -8,8 +8,6 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; import io.opentelemetry.api.logs.LogRecordBuilder; import io.opentelemetry.api.logs.Severity; import io.opentelemetry.context.Context; @@ -18,7 +16,6 @@ import javax.annotation.Nullable; /** Extended {@link LogRecordBuilder} with experimental APIs. */ -@SuppressWarnings("deprecation") public interface ExtendedLogRecordBuilder extends LogRecordBuilder { // keep this class even if it is empty, since experimental methods may be added in the future. @@ -71,15 +68,7 @@ default ExtendedLogRecordBuilder setBody(Value body) { @Override ExtendedLogRecordBuilder setEventName(String eventName); - /** - * {@inheritDoc} - * - *

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 - * log record, there is no difference between adding attributes using the standard or extended - * attribute APIs. - */ + /** {@inheritDoc} */ @SuppressWarnings("unchecked") @Override default ExtendedLogRecordBuilder setAllAttributes(Attributes attributes) { @@ -91,49 +80,10 @@ default ExtendedLogRecordBuilder setAllAttributes(Attributes attributes) { return this; } - /** - * Sets attributes. If the {@link LogRecordBuilder} previously contained a mapping for any of the - * keys, the old values are replaced by the specified values. - * - *

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 - * log record, there is no difference between adding attributes using the standard or extended - * attribute APIs. - */ - @SuppressWarnings("unchecked") - default ExtendedLogRecordBuilder setAllAttributes(ExtendedAttributes attributes) { - if (attributes == null || attributes.isEmpty()) { - return this; - } - attributes.forEach( - (attributeKey, value) -> setAttribute((ExtendedAttributeKey) attributeKey, value)); - return this; - } - - /** - * {@inheritDoc} - * - *

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 - * log record, there is no difference between adding attributes using the standard or extended - * attribute APIs. - */ + /** {@inheritDoc} */ @Override ExtendedLogRecordBuilder setAttribute(AttributeKey key, @Nullable T value); - /** - * Set an attribute. - * - *

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 - * log record, there is no difference between adding attributes using the standard or extended - * attribute APIs. - */ - ExtendedLogRecordBuilder setAttribute(ExtendedAttributeKey key, T value); - /** {@inheritDoc} */ @Override ExtendedLogRecordBuilder setException(Throwable throwable); diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKeyTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKeyTest.java deleted file mode 100644 index 4ff77c1144a..00000000000 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributeKeyTest.java +++ /dev/null @@ -1,91 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.api.common.AttributeKey; -import java.util.stream.Stream; -import javax.annotation.Nullable; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -@SuppressWarnings("deprecation") // Testing deprecated EXTENDED_ATTRIBUTES until removed -public class ExtendedAttributeKeyTest { - - @ParameterizedTest - @MethodSource("attributeKeyArgs") - void test( - ExtendedAttributeKey key, - String expectedKey, - ExtendedAttributeType expectedType, - @Nullable AttributeKey expectedAttributeKey) { - assertThat(key.getKey()).isEqualTo(expectedKey); - assertThat(key.getType()).isEqualTo(expectedType); - assertThat(key.asAttributeKey()).isEqualTo(expectedAttributeKey); - - if (expectedAttributeKey != null) { - ExtendedAttributeKey extendedAttributeKey = - ExtendedAttributeKey.fromAttributeKey(expectedAttributeKey); - assertThat(extendedAttributeKey).isEqualTo(key); - } - } - - private static Stream attributeKeyArgs() { - return Stream.of( - Arguments.of( - ExtendedAttributeKey.stringKey("key"), - "key", - ExtendedAttributeType.STRING, - AttributeKey.stringKey("key")), - Arguments.of( - ExtendedAttributeKey.booleanKey("key"), - "key", - ExtendedAttributeType.BOOLEAN, - AttributeKey.booleanKey("key")), - Arguments.of( - ExtendedAttributeKey.longKey("key"), - "key", - ExtendedAttributeType.LONG, - AttributeKey.longKey("key")), - Arguments.of( - ExtendedAttributeKey.doubleKey("key"), - "key", - ExtendedAttributeType.DOUBLE, - AttributeKey.doubleKey("key")), - Arguments.of( - ExtendedAttributeKey.stringArrayKey("key"), - "key", - ExtendedAttributeType.STRING_ARRAY, - AttributeKey.stringArrayKey("key")), - Arguments.of( - ExtendedAttributeKey.booleanArrayKey("key"), - "key", - ExtendedAttributeType.BOOLEAN_ARRAY, - AttributeKey.booleanArrayKey("key")), - Arguments.of( - ExtendedAttributeKey.longArrayKey("key"), - "key", - ExtendedAttributeType.LONG_ARRAY, - AttributeKey.longArrayKey("key")), - Arguments.of( - ExtendedAttributeKey.doubleArrayKey("key"), - "key", - ExtendedAttributeType.DOUBLE_ARRAY, - AttributeKey.doubleArrayKey("key")), - Arguments.of( - ExtendedAttributeKey.extendedAttributesKey("key"), - "key", - ExtendedAttributeType.EXTENDED_ATTRIBUTES, - null), - Arguments.of( - ExtendedAttributeKey.valueKey("key"), - "key", - ExtendedAttributeType.VALUE, - AttributeKey.valueKey("key"))); - } -} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java deleted file mode 100644 index ee649872527..00000000000 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/common/ExtendedAttributesTest.java +++ /dev/null @@ -1,751 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.api.incubator.common; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.entry; - -import com.google.common.collect.ImmutableMap; -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.Value; -import java.util.Arrays; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.stream.Stream; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.Arguments; -import org.junit.jupiter.params.provider.MethodSource; - -@SuppressWarnings("deprecation") // Testing deprecated EXTENDED_ATTRIBUTES until removed -class ExtendedAttributesTest { - - @ParameterizedTest - @MethodSource("attributesArgs") - void get_ExtendedAttributeKey( - ExtendedAttributes extendedAttributes, Map expectedMap) { - expectedMap.forEach( - (key, value) -> { - ExtendedAttributeKey extendedAttributeKey = getKey(key, value); - Object actualValue = extendedAttributes.get(extendedAttributeKey); - if (actualValue instanceof ExtendedAttributes) { - Map mapValue = toMap((ExtendedAttributes) actualValue); - actualValue = mapValue; - } - - assertThat(actualValue) - .describedAs(key + "(" + extendedAttributeKey.getType() + ")") - .isEqualTo(value); - }); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void get_AttributeKey(ExtendedAttributes extendedAttributes, Map expectedMap) { - expectedMap.forEach( - (key, value) -> { - ExtendedAttributeKey extendedAttributeKey = getKey(key, value); - AttributeKey attributeKey = extendedAttributeKey.asAttributeKey(); - - // Skip attribute keys which cannot be represented as AttributeKey - if (attributeKey == null) { - return; - } - - Object actualValue = extendedAttributes.get(attributeKey); - - assertThat(actualValue) - .describedAs(key + "(" + attributeKey.getType() + ")") - .isEqualTo(value); - }); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void forEach(ExtendedAttributes extendedAttributes, Map expectedMap) { - // toMap uses .forEach to convert - Map seenEntries = toMap(extendedAttributes); - - assertThat(seenEntries).isEqualTo(expectedMap); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void size(ExtendedAttributes extendedAttributes, Map expectedMap) { - assertThat(extendedAttributes.size()).isEqualTo(expectedMap.size()); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void isEmpty(ExtendedAttributes extendedAttributes, Map expectedMap) { - assertThat(extendedAttributes.isEmpty()).isEqualTo(expectedMap.isEmpty()); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void asMap(ExtendedAttributes extendedAttributes, Map expectedMap) { - assertEquals(extendedAttributes.asMap(), expectedMap); - } - - @SuppressWarnings("unchecked") - private static void assertEquals( - Map, Object> actual, Map expected) { - assertThat(actual.size()).isEqualTo(expected.size()); - actual.forEach( - (key, value) -> { - if (key.getType() == ExtendedAttributeType.EXTENDED_ATTRIBUTES) { - assertEquals( - ((ExtendedAttributes) value).asMap(), - (Map) expected.get(key.getKey())); - return; - } - assertThat(expected.get(key.getKey())).isEqualTo(value); - }); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void asAttributes(ExtendedAttributes extendedAttributes, Map expectedMap) { - Attributes attributes = extendedAttributes.asAttributes(); - - attributes.forEach( - (key, value) -> { - assertThat(value).isEqualTo(expectedMap.get(key.getKey())); - }); - - long expectedSize = - expectedMap.values().stream() - .filter(value -> !(value instanceof Map)) - .filter(value -> !(value instanceof Value)) - .count(); - assertThat(attributes.size()).isEqualTo(expectedSize); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void toBuilder(ExtendedAttributes extendedAttributes, Map expectedMap) { - ExtendedAttributesBuilder builder = extendedAttributes.toBuilder(); - - builder.put("extraKey", "value"); - - ExtendedAttributes extendedAttributes1 = builder.build(); - assertThat(extendedAttributes1.size()).isEqualTo(expectedMap.size() + 1); - - ExtendedAttributes extendedAttributes2 = - extendedAttributes1.toBuilder().remove(ExtendedAttributeKey.stringKey("extraKey")).build(); - - assertThat(extendedAttributes2).isEqualTo(extendedAttributes); - assertThat(extendedAttributes2.size()).isEqualTo(expectedMap.size()); - } - - @ParameterizedTest - @MethodSource("attributesArgs") - void equalsAndHashcode(ExtendedAttributes extendedAttributes, Map expectedMap) { - ExtendedAttributes withExtraEntry = - extendedAttributes.toBuilder().put("extraKey", "value").build(); - assertThat(extendedAttributes).isNotEqualTo(withExtraEntry); - assertThat(extendedAttributes.hashCode()).isNotEqualTo(withExtraEntry.hashCode()); - - ExtendedAttributes copy1 = - extendedAttributes.toBuilder().remove(ExtendedAttributeKey.stringKey("extraKey")).build(); - assertThat(extendedAttributes).isEqualTo(copy1); - assertThat(extendedAttributes.hashCode()).isEqualTo(copy1.hashCode()); - - ExtendedAttributes copy2 = fromMap(expectedMap); - assertThat(extendedAttributes).isEqualTo(copy2); - assertThat(extendedAttributes.hashCode()).isEqualTo(copy2.hashCode()); - } - - @SuppressWarnings("unchecked") - private static ExtendedAttributes fromMap(Map map) { - ExtendedAttributesBuilder builder = ExtendedAttributes.builder(); - map.forEach( - (key, value) -> { - ExtendedAttributeKey extendedAttributeKey = getKey(key, value); - if (extendedAttributeKey.getType() == ExtendedAttributeType.EXTENDED_ATTRIBUTES) { - builder.put( - (ExtendedAttributeKey) extendedAttributeKey, - fromMap((Map) value)); - return; - } - putInBuilder((ExtendedAttributeKey) extendedAttributeKey, value, builder); - }); - return builder.build(); - } - - private static void putInBuilder( - ExtendedAttributeKey key, Object value, ExtendedAttributesBuilder builder) { - builder.put(key, value); - } - - private static Stream attributesArgs() { - return Stream.of( - // Single entry attributes - Arguments.of(ExtendedAttributes.builder().build(), Collections.emptyMap()), - Arguments.of( - ExtendedAttributes.builder().put("key", "value").build(), - ImmutableMap.builder().put("key", "value").build()), - Arguments.of( - ExtendedAttributes.builder().put("key", true).build(), - ImmutableMap.builder().put("key", true).build()), - Arguments.of( - ExtendedAttributes.builder().put("key", 1L).build(), - ImmutableMap.builder().put("key", 1L).build()), - Arguments.of( - ExtendedAttributes.builder().put("key", 1.1).build(), - ImmutableMap.builder().put("key", 1.1).build()), - Arguments.of( - ExtendedAttributes.builder().put("key", "value1", "value2").build(), - ImmutableMap.builder().put("key", Arrays.asList("value1", "value2")).build()), - Arguments.of( - ExtendedAttributes.builder().put("key", true, false).build(), - ImmutableMap.builder().put("key", Arrays.asList(true, false)).build()), - Arguments.of( - ExtendedAttributes.builder().put("key", 1L, 2L).build(), - ImmutableMap.builder().put("key", Arrays.asList(1L, 2L)).build()), - Arguments.of( - ExtendedAttributes.builder().put("key", 1.1, 2.2).build(), - ImmutableMap.builder().put("key", Arrays.asList(1.1, 2.2)).build()), - Arguments.of( - ExtendedAttributes.builder() - .put("key", ExtendedAttributes.builder().put("child", "value").build()) - .build(), - ImmutableMap.builder() - .put("key", ImmutableMap.builder().put("child", "value").build()) - .build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of("value")) - .build(), - ImmutableMap.builder().put("key", "value").build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.stringKey("key"), "value") - .build(), - ImmutableMap.builder().put("key", "value").build()), - Arguments.of( - ExtendedAttributes.builder().put(ExtendedAttributeKey.booleanKey("key"), true).build(), - ImmutableMap.builder().put("key", true).build()), - Arguments.of( - ExtendedAttributes.builder().put(ExtendedAttributeKey.longKey("key"), 1L).build(), - ImmutableMap.builder().put("key", 1L).build()), - Arguments.of( - ExtendedAttributes.builder().put(ExtendedAttributeKey.doubleKey("key"), 1.1).build(), - ImmutableMap.builder().put("key", 1.1).build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.stringArrayKey("key"), Arrays.asList("value1", "value2")) - .build(), - ImmutableMap.builder().put("key", Arrays.asList("value1", "value2")).build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.booleanArrayKey("key"), Arrays.asList(true, false)) - .build(), - ImmutableMap.builder().put("key", Arrays.asList(true, false)).build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.longArrayKey("key"), Arrays.asList(1L, 2L)) - .build(), - ImmutableMap.builder().put("key", Arrays.asList(1L, 2L)).build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.doubleArrayKey("key"), Arrays.asList(1.1, 2.2)) - .build(), - ImmutableMap.builder().put("key", Arrays.asList(1.1, 2.2)).build()), - Arguments.of( - ExtendedAttributes.builder() - .put( - ExtendedAttributeKey.extendedAttributesKey("key"), - ExtendedAttributes.builder().put("child", "value").build()) - .build(), - ImmutableMap.builder() - .put("key", ImmutableMap.builder().put("child", "value").build()) - .build()), - Arguments.of( - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of("value")) - .build(), - ImmutableMap.builder().put("key", "value").build()), - // Multiple entries - Arguments.of( - ExtendedAttributes.builder() - .put("key1", "value1") - .put("key2", "value2") - .put("key3", true) - .put("key4", 1L) - .put("key5", 1.1) - .put("key6", "value1", "value2") - .put("key7", true, false) - .put("key8", 1L, 2L) - .put("key9", 1.1, 2.2) - .put("key10", ExtendedAttributes.builder().put("child", "value").build()) - .put(ExtendedAttributeKey.valueKey("key11"), Value.of("value")) - .build(), - ImmutableMap.builder() - .put("key1", "value1") - .put("key2", "value2") - .put("key3", true) - .put("key4", 1L) - .put("key5", 1.1) - .put("key6", Arrays.asList("value1", "value2")) - .put("key7", Arrays.asList(true, false)) - .put("key8", Arrays.asList(1L, 2L)) - .put("key9", Arrays.asList(1.1, 2.2)) - .put("key10", ImmutableMap.builder().put("child", "value").build()) - .put("key11", "value") - .build())); - } - - private static Map toMap(ExtendedAttributes extendedAttributes) { - Map map = new HashMap<>(); - extendedAttributes.forEach( - (key, value) -> { - if (key.getType() == ExtendedAttributeType.EXTENDED_ATTRIBUTES) { - map.put(key.getKey(), toMap((ExtendedAttributes) value)); - return; - } - map.put(key.getKey(), value); - }); - return map; - } - - private static ExtendedAttributeKey getKey(String key, Object value) { - switch (getType(value)) { - case STRING: - return ExtendedAttributeKey.stringKey(key); - case BOOLEAN: - return ExtendedAttributeKey.booleanKey(key); - case LONG: - return ExtendedAttributeKey.longKey(key); - case DOUBLE: - return ExtendedAttributeKey.doubleKey(key); - case STRING_ARRAY: - return ExtendedAttributeKey.stringArrayKey(key); - case BOOLEAN_ARRAY: - return ExtendedAttributeKey.booleanArrayKey(key); - case LONG_ARRAY: - return ExtendedAttributeKey.longArrayKey(key); - case DOUBLE_ARRAY: - return ExtendedAttributeKey.doubleArrayKey(key); - case EXTENDED_ATTRIBUTES: - return ExtendedAttributeKey.extendedAttributesKey(key); - case VALUE: - return ExtendedAttributeKey.valueKey(key); - } - throw new IllegalArgumentException(); - } - - @SuppressWarnings("unchecked") - private static ExtendedAttributeType getType(Object value) { - if (value instanceof String) { - return ExtendedAttributeType.STRING; - } - if (value instanceof Boolean) { - return ExtendedAttributeType.BOOLEAN; - } - if ((value instanceof Long) || (value instanceof Integer)) { - return ExtendedAttributeType.LONG; - } - if ((value instanceof Double) || (value instanceof Float)) { - return ExtendedAttributeType.DOUBLE; - } - if (value instanceof List) { - List list = (List) value; - if (list.isEmpty()) { - throw new IllegalArgumentException("Empty list"); - } - if (list.get(0) instanceof String) { - return ExtendedAttributeType.STRING_ARRAY; - } - if (list.get(0) instanceof Boolean) { - return ExtendedAttributeType.BOOLEAN_ARRAY; - } - if ((list.get(0) instanceof Long) || (list.get(0) instanceof Integer)) { - return ExtendedAttributeType.LONG_ARRAY; - } - if ((list.get(0) instanceof Double) || (list.get(0) instanceof Float)) { - return ExtendedAttributeType.DOUBLE_ARRAY; - } - } - if ((value instanceof Map)) { - return ExtendedAttributeType.EXTENDED_ATTRIBUTES; - } - if (value instanceof Value) { - return ExtendedAttributeType.VALUE; - } - throw new IllegalArgumentException("Unrecognized value type: " + value); - } - - @Test - void complexValueStoredAsString() { - // When putting a VALUE attribute with a string Value, it should be stored as STRING type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of("test")) - .build(); - - // Should be stored as STRING type internally - assertThat(attributes.get(ExtendedAttributeKey.stringKey("key"))).isEqualTo("test"); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(Value.of("test")); - - // forEach should show STRING type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen).containsExactly(entry(ExtendedAttributeKey.stringKey("key"), "test")); - - // asMap should show STRING type - assertThat(attributes.asMap()) - .containsExactly(entry(ExtendedAttributeKey.stringKey("key"), "test")); - } - - @Test - void complexValueStoredAsLong() { - // When putting a VALUE attribute with a long Value, it should be stored as LONG type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of(123L)) - .build(); - - // Should be stored as LONG type internally - assertThat(attributes.get(ExtendedAttributeKey.longKey("key"))).isEqualTo(123L); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(Value.of(123L)); - - // forEach should show LONG type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen).containsExactly(entry(ExtendedAttributeKey.longKey("key"), 123L)); - - // asMap should show LONG type - assertThat(attributes.asMap()) - .containsExactly(entry(ExtendedAttributeKey.longKey("key"), 123L)); - } - - @Test - void complexValueStoredAsDouble() { - // When putting a VALUE attribute with a double Value, it should be stored as DOUBLE type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of(1.23)) - .build(); - - // Should be stored as DOUBLE type internally - assertThat(attributes.get(ExtendedAttributeKey.doubleKey("key"))).isEqualTo(1.23); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(Value.of(1.23)); - - // forEach should show DOUBLE type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen).containsExactly(entry(ExtendedAttributeKey.doubleKey("key"), 1.23)); - - // asMap should show DOUBLE type - assertThat(attributes.asMap()) - .containsExactly(entry(ExtendedAttributeKey.doubleKey("key"), 1.23)); - } - - @Test - void complexValueStoredAsBoolean() { - // When putting a VALUE attribute with a boolean Value, it should be stored as BOOLEAN type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of(true)) - .build(); - - // Should be stored as BOOLEAN type internally - assertThat(attributes.get(ExtendedAttributeKey.booleanKey("key"))).isEqualTo(true); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(Value.of(true)); - - // forEach should show BOOLEAN type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen).containsExactly(entry(ExtendedAttributeKey.booleanKey("key"), true)); - - // asMap should show BOOLEAN type - assertThat(attributes.asMap()) - .containsExactly(entry(ExtendedAttributeKey.booleanKey("key"), true)); - } - - @Test - void complexValueStoredAsStringArray() { - // When putting a VALUE attribute with a homogeneous string array, it should be stored as - // STRING_ARRAY type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put( - ExtendedAttributeKey.valueKey("key"), - Value.of(Arrays.asList(Value.of("a"), Value.of("b")))) - .build(); - - // Should be stored as STRING_ARRAY type internally - assertThat(attributes.get(ExtendedAttributeKey.stringArrayKey("key"))) - .containsExactly("a", "b"); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))) - .isEqualTo(Value.of(Arrays.asList(Value.of("a"), Value.of("b")))); - - // forEach should show STRING_ARRAY type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly( - entry(ExtendedAttributeKey.stringArrayKey("key"), Arrays.asList("a", "b"))); - - // asMap should show STRING_ARRAY type - assertThat(attributes.asMap()) - .containsExactly( - entry(ExtendedAttributeKey.stringArrayKey("key"), Arrays.asList("a", "b"))); - } - - @Test - void complexValueStoredAsLongArray() { - // When putting a VALUE attribute with a homogeneous long array, it should be stored as - // LONG_ARRAY type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put( - ExtendedAttributeKey.valueKey("key"), - Value.of(Arrays.asList(Value.of(1L), Value.of(2L)))) - .build(); - - // Should be stored as LONG_ARRAY type internally - assertThat(attributes.get(ExtendedAttributeKey.longArrayKey("key"))).containsExactly(1L, 2L); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))) - .isEqualTo(Value.of(Arrays.asList(Value.of(1L), Value.of(2L)))); - - // forEach should show LONG_ARRAY type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly(entry(ExtendedAttributeKey.longArrayKey("key"), Arrays.asList(1L, 2L))); - - // asMap should show LONG_ARRAY type - assertThat(attributes.asMap()) - .containsExactly(entry(ExtendedAttributeKey.longArrayKey("key"), Arrays.asList(1L, 2L))); - } - - @Test - void complexValueStoredAsDoubleArray() { - // When putting a VALUE attribute with a homogeneous double array, it should be stored as - // DOUBLE_ARRAY type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put( - ExtendedAttributeKey.valueKey("key"), - Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2)))) - .build(); - - // Should be stored as DOUBLE_ARRAY type internally - assertThat(attributes.get(ExtendedAttributeKey.doubleArrayKey("key"))) - .containsExactly(1.1, 2.2); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))) - .isEqualTo(Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2)))); - - // forEach should show DOUBLE_ARRAY type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly( - entry(ExtendedAttributeKey.doubleArrayKey("key"), Arrays.asList(1.1, 2.2))); - - // asMap should show DOUBLE_ARRAY type - assertThat(attributes.asMap()) - .containsExactly( - entry(ExtendedAttributeKey.doubleArrayKey("key"), Arrays.asList(1.1, 2.2))); - } - - @Test - void complexValueStoredAsBooleanArray() { - // When putting a VALUE attribute with a homogeneous boolean array, it should be stored as - // BOOLEAN_ARRAY type - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put( - ExtendedAttributeKey.valueKey("key"), - Value.of(Arrays.asList(Value.of(true), Value.of(false)))) - .build(); - - // Should be stored as BOOLEAN_ARRAY type internally - assertThat(attributes.get(ExtendedAttributeKey.booleanArrayKey("key"))) - .containsExactly(true, false); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))) - .isEqualTo(Value.of(Arrays.asList(Value.of(true), Value.of(false)))); - - // forEach should show BOOLEAN_ARRAY type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly( - entry(ExtendedAttributeKey.booleanArrayKey("key"), Arrays.asList(true, false))); - - // asMap should show BOOLEAN_ARRAY type - assertThat(attributes.asMap()) - .containsExactly( - entry(ExtendedAttributeKey.booleanArrayKey("key"), Arrays.asList(true, false))); - } - - @Test - void simpleAttributeRetrievedAsComplexValue() { - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put("string", "test") - .put("long", 123L) - .put("double", 1.23) - .put("boolean", true) - .put("stringArray", "a", "b") - .put("longArray", 1L, 2L) - .put("doubleArray", 1.1, 2.2) - .put("booleanArray", true, false) - .build(); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("string"))).isEqualTo(Value.of("test")); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("long"))).isEqualTo(Value.of(123L)); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("double"))).isEqualTo(Value.of(1.23)); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("boolean"))).isEqualTo(Value.of(true)); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("stringArray"))) - .isEqualTo(Value.of(Arrays.asList(Value.of("a"), Value.of("b")))); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("longArray"))) - .isEqualTo(Value.of(Arrays.asList(Value.of(1L), Value.of(2L)))); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("doubleArray"))) - .isEqualTo(Value.of(Arrays.asList(Value.of(1.1), Value.of(2.2)))); - assertThat(attributes.get(ExtendedAttributeKey.valueKey("booleanArray"))) - .isEqualTo(Value.of(Arrays.asList(Value.of(true), Value.of(false)))); - } - - @Test - void emptyValueArrayRetrievedAsAnyArrayType() { - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.valueKey("key"), Value.of(Collections.emptyList())) - .build(); - assertThat(attributes.get(ExtendedAttributeKey.stringArrayKey("key"))).isEmpty(); - assertThat(attributes.get(ExtendedAttributeKey.longArrayKey("key"))).isEmpty(); - assertThat(attributes.get(ExtendedAttributeKey.doubleArrayKey("key"))).isEmpty(); - assertThat(attributes.get(ExtendedAttributeKey.booleanArrayKey("key"))).isEmpty(); - } - - @Test - void getNullKey() { - ExtendedAttributes attributes = ExtendedAttributes.builder().put("key", "value").build(); - assertThat(attributes.get((ExtendedAttributeKey) null)).isNull(); - } - - @Test - void putNullKey() { - ExtendedAttributes attributes = - ExtendedAttributes.builder().put((ExtendedAttributeKey) null, "value").build(); - assertThat(attributes.isEmpty()).isTrue(); - } - - @Test - void putNullValue() { - ExtendedAttributes attributes = - ExtendedAttributes.builder().put(ExtendedAttributeKey.stringKey("key"), null).build(); - assertThat(attributes.isEmpty()).isTrue(); - } - - @Test - void putEmptyKey() { - ExtendedAttributes attributes = - ExtendedAttributes.builder().put(ExtendedAttributeKey.stringKey(""), "value").build(); - assertThat(attributes.isEmpty()).isTrue(); - } - - @Test - void extendedAttributesNotConvertibleToValue() { - ExtendedAttributes nested = ExtendedAttributes.builder().put("child", "value").build(); - ExtendedAttributes attributes = - ExtendedAttributes.builder() - .put(ExtendedAttributeKey.extendedAttributesKey("key"), nested) - .build(); - - // Getting as VALUE should return null since EXTENDED_ATTRIBUTES cannot be converted to Value - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isNull(); - } - - @Test - void complexValueWithKeyValueList() { - // KEY_VALUE_LIST should be kept as VALUE type - Value kvListValue = Value.of(Collections.emptyMap()); - ExtendedAttributes attributes = - ExtendedAttributes.builder().put(ExtendedAttributeKey.valueKey("key"), kvListValue).build(); - - // Should be stored as VALUE type - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(kvListValue); - - // forEach should show VALUE type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly(entry(ExtendedAttributeKey.valueKey("key"), kvListValue)); - } - - @Test - void complexValueWithBytes() { - // BYTES should be kept as VALUE type - Value bytesValue = Value.of(new byte[] {1, 2, 3}); - ExtendedAttributes attributes = - ExtendedAttributes.builder().put(ExtendedAttributeKey.valueKey("key"), bytesValue).build(); - - // Should be stored as VALUE type - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(bytesValue); - - // forEach should show VALUE type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly(entry(ExtendedAttributeKey.valueKey("key"), bytesValue)); - } - - @Test - void complexValueWithNonHomogeneousArray() { - // Non-homogeneous array should be kept as VALUE type - Value mixedArray = Value.of(Arrays.asList(Value.of("string"), Value.of(123L))); - ExtendedAttributes attributes = - ExtendedAttributes.builder().put(ExtendedAttributeKey.valueKey("key"), mixedArray).build(); - - // Should be stored as VALUE type - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(mixedArray); - - // forEach should show VALUE type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly(entry(ExtendedAttributeKey.valueKey("key"), mixedArray)); - } - - @Test - void complexValueWithNestedArray() { - // Array containing arrays should be kept as VALUE type - Value nestedArray = - Value.of( - Arrays.asList( - Value.of(Arrays.asList(Value.of("a"), Value.of("b"))), - Value.of(Arrays.asList(Value.of("c"), Value.of("d"))))); - ExtendedAttributes attributes = - ExtendedAttributes.builder().put(ExtendedAttributeKey.valueKey("key"), nestedArray).build(); - - // Should be stored as VALUE type - assertThat(attributes.get(ExtendedAttributeKey.valueKey("key"))).isEqualTo(nestedArray); - - // forEach should show VALUE type - Map, Object> entriesSeen = new HashMap<>(); - attributes.forEach(entriesSeen::put); - assertThat(entriesSeen) - .containsExactly(entry(ExtendedAttributeKey.valueKey("key"), nestedArray)); - } - - @Test - void getNonExistentArrayType() { - // Test the code path where we look for an array type that doesn't exist - ExtendedAttributes attributes = ExtendedAttributes.builder().put("key", "value").build(); - - // Looking for an array type when only a string exists should return null - assertThat(attributes.get(ExtendedAttributeKey.stringArrayKey("key"))).isNull(); - assertThat(attributes.get(ExtendedAttributeKey.longArrayKey("key"))).isNull(); - assertThat(attributes.get(ExtendedAttributeKey.doubleArrayKey("key"))).isNull(); - assertThat(attributes.get(ExtendedAttributeKey.booleanArrayKey("key"))).isNull(); - } -} diff --git a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java index 5837b5cda85..f02c7a7aea7 100644 --- a/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java +++ b/api/incubator/src/test/java/io/opentelemetry/api/incubator/logs/ExtendedLogsBridgeApiUsageTest.java @@ -9,34 +9,20 @@ import static io.opentelemetry.sdk.logs.internal.LoggerConfig.disabled; import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat; -import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.KeyValue; -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.logs.Logger; import io.opentelemetry.api.logs.Severity; -import io.opentelemetry.internal.testing.slf4j.SuppressLogger; import io.opentelemetry.sdk.logs.SdkLoggerProvider; import io.opentelemetry.sdk.logs.SdkLoggerProviderBuilder; -import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData; import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; import io.opentelemetry.sdk.logs.internal.SdkLoggerProviderUtil; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; -import java.util.Arrays; -import java.util.List; import java.util.Random; import org.junit.jupiter.api.Test; /** Demonstrating usage of extended Logs Bridge API. */ -@SuppressWarnings("deprecation") class ExtendedLogsBridgeApiUsageTest { - private static final java.util.logging.Logger logger = - java.util.logging.Logger.getLogger(ExtendedLogsBridgeApiUsageTest.class.getName()); - @Test void loggerEnabled() { // Setup SdkLoggerProvider @@ -93,156 +79,4 @@ void loggerEnabled() { private static String flipCoin() { return random.nextBoolean() ? "heads" : "tails"; } - - // Primitive keys - AttributeKey strKey = AttributeKey.stringKey("acme.string"); - AttributeKey longKey = AttributeKey.longKey("acme.long"); - AttributeKey booleanKey = AttributeKey.booleanKey("acme.boolean"); - AttributeKey doubleKey = AttributeKey.doubleKey("acme.double"); - - // Primitive array keys - AttributeKey> strArrKey = AttributeKey.stringArrayKey("acme.string_array"); - AttributeKey> longArrKey = AttributeKey.longArrayKey("acme.long_array"); - AttributeKey> booleanArrKey = AttributeKey.booleanArrayKey("acme.boolean_array"); - AttributeKey> doubleArrKey = AttributeKey.doubleArrayKey("acme.double_array"); - - // VALUE key - ExtendedAttributeKey> valueKey = ExtendedAttributeKey.valueKey("acme.value"); - - @Test - @SuppressLogger(ExtendedLogsBridgeApiUsageTest.class) - void extendedAttributesUsage() { - // Initialize from builder. Varargs style initialization (ExtendedAttributes.of(...) not - // supported. - ExtendedAttributes extendedAttributes = - ExtendedAttributes.builder() - .put(strKey, "value") - .put(longKey, 1L) - .put(booleanKey, true) - .put(doubleKey, 1.1) - .put(strArrKey, Arrays.asList("value1", "value2")) - .put(longArrKey, Arrays.asList(1L, 2L)) - .put(booleanArrKey, Arrays.asList(true, false)) - .put(doubleArrKey, Arrays.asList(1.1, 2.2)) - .put( - valueKey, - Value.of( - KeyValue.of("childStr", Value.of("value")), - KeyValue.of("childLong", Value.of(1L)))) - .build(); - - // Retrieval - assertThat(extendedAttributes.get(strKey)).isEqualTo("value"); - assertThat(extendedAttributes.get(longKey)).isEqualTo(1); - assertThat(extendedAttributes.get(booleanKey)).isEqualTo(true); - assertThat(extendedAttributes.get(doubleKey)).isEqualTo(1.1); - assertThat(extendedAttributes.get(strArrKey)).isEqualTo(Arrays.asList("value1", "value2")); - assertThat(extendedAttributes.get(longArrKey)).isEqualTo(Arrays.asList(1L, 2L)); - assertThat(extendedAttributes.get(booleanArrKey)).isEqualTo(Arrays.asList(true, false)); - assertThat(extendedAttributes.get(doubleArrKey)).isEqualTo(Arrays.asList(1.1, 2.2)); - assertThat(extendedAttributes.get(valueKey)) - .isEqualTo( - Value.of( - KeyValue.of("childStr", Value.of("value")), - KeyValue.of("childLong", Value.of(1L)))); - - // Iteration - // Output: - // acme.boolean(BOOLEAN): true - // acme.boolean_array(BOOLEAN_ARRAY): [true, false] - // acme.double(DOUBLE): 1.1 - // acme.double_array(DOUBLE_ARRAY): [1.1, 2.2] - // acme.long(LONG): 1 - // acme.long_array(LONG_ARRAY): [1, 2] - // acme.string(STRING): value - // acme.string_array(STRING_ARRAY): [value1, value2] - // acme.value(VALUE): [KeyValue{key=childStr, value=StringValue{value=value}}, - // KeyValue{key=childLong, value=LongValue{value=1}}] - extendedAttributes.forEach( - (extendedAttributeKey, object) -> - logger.info( - String.format( - "%s(%s): %s", - extendedAttributeKey.getKey(), extendedAttributeKey.getType(), object))); - } - - @Test - void logRecordBuilder_ExtendedAttributes() { - InMemoryLogRecordExporter exporter = InMemoryLogRecordExporter.create(); - SdkLoggerProvider loggerProvider = - SdkLoggerProvider.builder() - .addLogRecordProcessor(SimpleLogRecordProcessor.create(exporter)) - .build(); - - Logger logger = loggerProvider.get("logger"); - - // Can set either standard or extended attributes on - ((ExtendedLogRecordBuilder) logger.logRecordBuilder()) - .setBody("message") - .setAttribute(strKey, "value") - .setAttribute(longKey, 1L) - .setAttribute(booleanKey, true) - .setAttribute(doubleKey, 1.1) - .setAttribute(strArrKey, Arrays.asList("value1", "value2")) - .setAttribute(longArrKey, Arrays.asList(1L, 2L)) - .setAttribute(booleanArrKey, Arrays.asList(true, false)) - .setAttribute(doubleArrKey, Arrays.asList(1.1, 2.2)) - .setAttribute( - valueKey, - Value.of( - KeyValue.of("childStr", Value.of("value")), KeyValue.of("childLong", Value.of(1L)))) - .setAllAttributes(Attributes.builder().put("key1", "value").build()) - .setAllAttributes(ExtendedAttributes.builder().put("key2", "value").build()) - .emit(); - - assertThat(exporter.getFinishedLogRecordItems()) - .satisfiesExactly( - logRecordData -> { - assertThat(logRecordData).isInstanceOf(ExtendedLogRecordData.class); - ExtendedLogRecordData extendedLogRecordData = (ExtendedLogRecordData) logRecordData; - - // Optionally access standard attributes, which filters out any extended attribute - // types - assertThat(logRecordData.getAttributes()) - .isEqualTo( - Attributes.builder() - .put(strKey, "value") - .put(longKey, 1L) - .put(booleanKey, true) - .put(doubleKey, 1.1) - .put(strArrKey, Arrays.asList("value1", "value2")) - .put(longArrKey, Arrays.asList(1L, 2L)) - .put(booleanArrKey, Arrays.asList(true, false)) - .put(doubleArrKey, Arrays.asList(1.1, 2.2)) - .put( - AttributeKey.valueKey("acme.value"), - Value.of( - KeyValue.of("childStr", Value.of("value")), - KeyValue.of("childLong", Value.of(1L)))) - .put("key1", "value") - .put("key2", "value") - .build()); - - // But preferably access and serialize full extended attributes - assertThat(extendedLogRecordData.getExtendedAttributes()) - .isEqualTo( - ExtendedAttributes.builder() - .put(strKey, "value") - .put(longKey, 1L) - .put(booleanKey, true) - .put(doubleKey, 1.1) - .put(strArrKey, Arrays.asList("value1", "value2")) - .put(longArrKey, Arrays.asList(1L, 2L)) - .put(booleanArrKey, Arrays.asList(true, false)) - .put(doubleArrKey, Arrays.asList(1.1, 2.2)) - .put( - valueKey, - Value.of( - KeyValue.of("childStr", Value.of("value")), - KeyValue.of("childLong", Value.of(1L)))) - .put("key1", "value") - .put("key2", "value") - .build()); - }); - } } diff --git a/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/TestDataExporter.java b/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/TestDataExporter.java index ab3d59b78b7..cbaf58ae2b4 100644 --- a/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/TestDataExporter.java +++ b/exporters/logging-otlp/src/test/java/io/opentelemetry/exporter/logging/otlp/TestDataExporter.java @@ -30,7 +30,7 @@ import io.opentelemetry.sdk.metrics.internal.data.ImmutableMetricData; import io.opentelemetry.sdk.metrics.internal.data.ImmutableSumData; import io.opentelemetry.sdk.resources.Resource; -import io.opentelemetry.sdk.testing.logs.internal.TestExtendedLogRecordData; +import io.opentelemetry.sdk.testing.logs.TestLogRecordData; import io.opentelemetry.sdk.testing.trace.TestSpanData; import io.opentelemetry.sdk.trace.data.EventData; import io.opentelemetry.sdk.trace.data.SpanData; @@ -50,7 +50,7 @@ abstract class TestDataExporter { Resource.create(Attributes.builder().put("key", "value").build()); private static final LogRecordData LOG1 = - TestExtendedLogRecordData.builder() + TestLogRecordData.builder() .setResource(RESOURCE) .setInstrumentationScopeInfo( InstrumentationScopeInfo.builder("instrumentation") @@ -81,7 +81,7 @@ abstract class TestDataExporter { .build(); private static final LogRecordData LOG2 = - TestExtendedLogRecordData.builder() + TestLogRecordData.builder() .setResource(RESOURCE) .setInstrumentationScopeInfo( InstrumentationScopeInfo.builder("instrumentation2").setVersion("2").build()) diff --git a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/ExtendedAttributeKeyValueStatelessMarshaler.java b/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/ExtendedAttributeKeyValueStatelessMarshaler.java deleted file mode 100644 index 704198c2474..00000000000 --- a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/ExtendedAttributeKeyValueStatelessMarshaler.java +++ /dev/null @@ -1,258 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.exporter.internal.otlp; - -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributeType; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl; -import io.opentelemetry.exporter.internal.marshal.CodedOutputStream; -import io.opentelemetry.exporter.internal.marshal.MarshalerContext; -import io.opentelemetry.exporter.internal.marshal.MarshalerUtil; -import io.opentelemetry.exporter.internal.marshal.ProtoFieldInfo; -import io.opentelemetry.exporter.internal.marshal.Serializer; -import io.opentelemetry.exporter.internal.marshal.StatelessMarshaler; -import io.opentelemetry.exporter.internal.marshal.StatelessMarshaler2; -import io.opentelemetry.exporter.internal.marshal.StatelessMarshalerUtil; -import io.opentelemetry.proto.common.v1.internal.AnyValue; -import io.opentelemetry.proto.common.v1.internal.KeyValue; -import io.opentelemetry.proto.common.v1.internal.KeyValueList; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.util.List; -import java.util.Objects; - -/** - * A Marshaler of {@link ExtendedAttributes} key value pairs. See {@link KeyValueMarshaler}. - * - *

This class is internal and is hence not for public use. Its APIs are unstable and can change - * at any time. - */ -@SuppressWarnings("deprecation") -public final class ExtendedAttributeKeyValueStatelessMarshaler - implements StatelessMarshaler2, Object> { - private static final ExtendedAttributeKeyValueStatelessMarshaler INSTANCE = - new ExtendedAttributeKeyValueStatelessMarshaler(); - private static final byte[] EMPTY_BYTES = new byte[0]; - - private ExtendedAttributeKeyValueStatelessMarshaler() {} - - /** - * Serializes the {@code attributes}. This method reads elements from context, use together with - * {@link ExtendedAttributeKeyValueStatelessMarshaler#sizeExtendedAttributes(ProtoFieldInfo, - * ExtendedAttributes, MarshalerContext)}. - */ - public static void serializeExtendedAttributes( - Serializer output, - ProtoFieldInfo field, - ExtendedAttributes attributes, - MarshalerContext context) - throws IOException { - output.writeStartRepeated(field); - - if (!attributes.isEmpty()) { - try { - attributes.forEach( - (extendedAttributeKey, value) -> { - try { - output.writeStartRepeatedElement(field, context.getSize()); - INSTANCE.writeTo(output, extendedAttributeKey, value, context); - output.writeEndRepeatedElement(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - }); - } catch (UncheckedIOException e) { - throw e.getCause(); - } - } - - output.writeEndRepeated(); - } - - /** - * Sizes the {@code attributes}. This method adds elements to context, use together with {@link - * ExtendedAttributeKeyValueStatelessMarshaler#serializeExtendedAttributes(Serializer, - * ProtoFieldInfo, ExtendedAttributes, MarshalerContext)}. - */ - public static int sizeExtendedAttributes( - ProtoFieldInfo field, ExtendedAttributes attributes, MarshalerContext context) { - if (attributes.isEmpty()) { - return 0; - } - - int[] size = new int[] {0}; - - attributes.forEach( - (extendedAttributeKey, value) -> { - int sizeIndex = context.addSize(); - int fieldSize = INSTANCE.getBinarySerializedSize(extendedAttributeKey, value, context); - context.setSize(sizeIndex, fieldSize); - size[0] += - field.getTagSize() + CodedOutputStream.computeUInt32SizeNoTag(fieldSize) + fieldSize; - }); - - return size[0]; - } - - @Override - public void writeTo( - Serializer output, - ExtendedAttributeKey attributeKey, - Object value, - MarshalerContext context) - throws IOException { - if (attributeKey.getKey().isEmpty()) { - output.serializeString(KeyValue.KEY, EMPTY_BYTES); - } else if (attributeKey instanceof InternalExtendedAttributeKeyImpl) { - byte[] keyUtf8 = ((InternalExtendedAttributeKeyImpl) attributeKey).getKeyUtf8(); - output.serializeString(KeyValue.KEY, keyUtf8); - } else { - output.serializeStringWithContext(KeyValue.KEY, attributeKey.getKey(), context); - } - output.serializeMessageWithContext( - KeyValue.VALUE, attributeKey, value, ValueStatelessMarshaler.INSTANCE, context); - } - - @Override - public int getBinarySerializedSize( - ExtendedAttributeKey attributeKey, Object value, MarshalerContext context) { - int size = 0; - if (!attributeKey.getKey().isEmpty()) { - if (attributeKey instanceof InternalExtendedAttributeKeyImpl) { - byte[] keyUtf8 = ((InternalExtendedAttributeKeyImpl) attributeKey).getKeyUtf8(); - size += MarshalerUtil.sizeBytes(KeyValue.KEY, keyUtf8); - } else { - return StatelessMarshalerUtil.sizeStringWithContext( - KeyValue.KEY, attributeKey.getKey(), context); - } - } - size += - StatelessMarshalerUtil.sizeMessageWithContext( - KeyValue.VALUE, attributeKey, value, ValueStatelessMarshaler.INSTANCE, context); - - return size; - } - - private static class ValueStatelessMarshaler - implements StatelessMarshaler2, Object> { - static final ValueStatelessMarshaler INSTANCE = new ValueStatelessMarshaler(); - - // Supporting deprecated EXTENDED_ATTRIBUTES type until removed - @SuppressWarnings("unchecked") - @Override - public int getBinarySerializedSize( - ExtendedAttributeKey attributeKey, Object value, MarshalerContext context) { - ExtendedAttributeType attributeType = attributeKey.getType(); - switch (attributeType) { - case STRING: - return StringAnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize( - (String) value, context); - case LONG: - return IntAnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize( - (Long) value, context); - case BOOLEAN: - return BoolAnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize( - (Boolean) value, context); - case DOUBLE: - return DoubleAnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize( - (Double) value, context); - case STRING_ARRAY: - case LONG_ARRAY: - case BOOLEAN_ARRAY: - case DOUBLE_ARRAY: - return StatelessMarshalerUtil.sizeMessageWithContext( - AnyValue.ARRAY_VALUE, - Objects.requireNonNull(attributeKey.asAttributeKey()).getType(), - (List) value, - AttributeArrayAnyValueStatelessMarshaler.INSTANCE, - context); - case EXTENDED_ATTRIBUTES: - return StatelessMarshalerUtil.sizeMessageWithContext( - AnyValue.KVLIST_VALUE, - (ExtendedAttributes) value, - ExtendedAttributesKeyValueListStatelessMarshaler.INSTANCE, - context); - case VALUE: - return AnyValueStatelessMarshaler.INSTANCE.getBinarySerializedSize( - (Value) value, context); - } - // Error prone ensures the switch statement is complete, otherwise only can happen with - // unaligned versions which are not supported. - throw new IllegalArgumentException("Unsupported attribute type."); - } - - // Supporting deprecated EXTENDED_ATTRIBUTES type until removed - @SuppressWarnings("unchecked") - @Override - public void writeTo( - Serializer output, - ExtendedAttributeKey attributeKey, - Object value, - MarshalerContext context) - throws IOException { - ExtendedAttributeType attributeType = attributeKey.getType(); - switch (attributeType) { - case STRING: - StringAnyValueStatelessMarshaler.INSTANCE.writeTo(output, (String) value, context); - return; - case LONG: - IntAnyValueStatelessMarshaler.INSTANCE.writeTo(output, (Long) value, context); - return; - case BOOLEAN: - BoolAnyValueStatelessMarshaler.INSTANCE.writeTo(output, (Boolean) value, context); - return; - case DOUBLE: - DoubleAnyValueStatelessMarshaler.INSTANCE.writeTo(output, (Double) value, context); - return; - case STRING_ARRAY: - case LONG_ARRAY: - case BOOLEAN_ARRAY: - case DOUBLE_ARRAY: - output.serializeMessageWithContext( - AnyValue.ARRAY_VALUE, - Objects.requireNonNull(attributeKey.asAttributeKey()).getType(), - (List) value, - AttributeArrayAnyValueStatelessMarshaler.INSTANCE, - context); - return; - case EXTENDED_ATTRIBUTES: - output.serializeMessageWithContext( - AnyValue.KVLIST_VALUE, - (ExtendedAttributes) value, - ExtendedAttributesKeyValueListStatelessMarshaler.INSTANCE, - context); - return; - case VALUE: - AnyValueStatelessMarshaler.INSTANCE.writeTo(output, (Value) value, context); - return; - } - // Error prone ensures the switch statement is complete, otherwise only can happen with - // unaligned versions which are not supported. - throw new IllegalArgumentException("Unsupported attribute type."); - } - } - - private static class ExtendedAttributesKeyValueListStatelessMarshaler - implements StatelessMarshaler { - private static final ExtendedAttributesKeyValueListStatelessMarshaler INSTANCE = - new ExtendedAttributesKeyValueListStatelessMarshaler(); - - private ExtendedAttributesKeyValueListStatelessMarshaler() {} - - @Override - public void writeTo(Serializer output, ExtendedAttributes value, MarshalerContext context) - throws IOException { - serializeExtendedAttributes(output, KeyValueList.VALUES, value, context); - } - - @Override - public int getBinarySerializedSize(ExtendedAttributes value, MarshalerContext context) { - return sizeExtendedAttributes(KeyValueList.VALUES, value, context); - } - } -} diff --git a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/IncubatingUtil.java b/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/IncubatingUtil.java deleted file mode 100644 index 4ee235aa1e1..00000000000 --- a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/IncubatingUtil.java +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.exporter.internal.otlp; - -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.incubator.internal.InternalExtendedAttributeKeyImpl; -import io.opentelemetry.exporter.internal.marshal.MarshalerContext; -import io.opentelemetry.exporter.internal.marshal.Serializer; -import io.opentelemetry.proto.logs.v1.internal.LogRecord; -import io.opentelemetry.sdk.logs.data.LogRecordData; -import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.function.BiConsumer; - -/** - * Utilities for interacting with {@code io.opentelemetry:opentelemetry-api-incubator}, which is not - * guaranteed to be present on the classpath. For all methods, callers MUST first separately - * reflectively confirm that the incubator is available on the classpath. - * - *

This class is internal and is hence not for public use. Its APIs are unstable and can change - * at any time. - */ -@SuppressWarnings("deprecation") -public class IncubatingUtil { - - private static final boolean INCUBATOR_AVAILABLE; - - static { - boolean incubatorAvailable = false; - try { - Class.forName("io.opentelemetry.api.incubator.common.ExtendedAttributes"); - incubatorAvailable = true; - } catch (ClassNotFoundException e) { - // Not available - } - INCUBATOR_AVAILABLE = incubatorAvailable; - } - - private static final byte[] EMPTY_BYTES = new byte[0]; - private static final KeyValueMarshaler[] EMPTY_REPEATED = new KeyValueMarshaler[0]; - - private IncubatingUtil() {} - - public static boolean isExtendedLogRecordData(LogRecordData logRecordData) { - return INCUBATOR_AVAILABLE && logRecordData instanceof ExtendedLogRecordData; - } - - @SuppressWarnings("AvoidObjectArrays") - public static KeyValueMarshaler[] createdExtendedAttributesMarhsalers( - LogRecordData logRecordData) { - return createForExtendedAttributes(getExtendedAttributes(logRecordData)); - } - - public static int extendedAttributesSize(LogRecordData logRecordData) { - return getExtendedAttributes(logRecordData).size(); - } - - // TODO(jack-berg): move to KeyValueMarshaler when ExtendedAttributes is stable - private static KeyValueMarshaler[] createForExtendedAttributes(ExtendedAttributes attributes) { - if (attributes.isEmpty()) { - return EMPTY_REPEATED; - } - - KeyValueMarshaler[] marshalers = new KeyValueMarshaler[attributes.size()]; - attributes.forEach( - new BiConsumer, Object>() { - int index = 0; - - @Override - public void accept(ExtendedAttributeKey attributeKey, Object o) { - marshalers[index++] = create(attributeKey, o); - } - }); - return marshalers; - } - - // TODO(jack-berg): move to KeyValueMarshaler when ExtendedAttributes is stable - // Supporting deprecated EXTENDED_ATTRIBUTES type until removed - @SuppressWarnings("unchecked") - private static KeyValueMarshaler create(ExtendedAttributeKey attributeKey, Object value) { - byte[] keyUtf8; - if (attributeKey.getKey().isEmpty()) { - keyUtf8 = EMPTY_BYTES; - } else if (attributeKey instanceof InternalExtendedAttributeKeyImpl) { - keyUtf8 = ((InternalExtendedAttributeKeyImpl) attributeKey).getKeyUtf8(); - } else { - keyUtf8 = attributeKey.getKey().getBytes(StandardCharsets.UTF_8); - } - switch (attributeKey.getType()) { - case STRING: - return new KeyValueMarshaler(keyUtf8, StringAnyValueMarshaler.create((String) value)); - case LONG: - return new KeyValueMarshaler(keyUtf8, IntAnyValueMarshaler.create((long) value)); - case BOOLEAN: - return new KeyValueMarshaler(keyUtf8, BoolAnyValueMarshaler.create((boolean) value)); - case DOUBLE: - return new KeyValueMarshaler(keyUtf8, DoubleAnyValueMarshaler.create((double) value)); - case STRING_ARRAY: - return new KeyValueMarshaler( - keyUtf8, ArrayAnyValueMarshaler.createString((List) value)); - case LONG_ARRAY: - return new KeyValueMarshaler(keyUtf8, ArrayAnyValueMarshaler.createInt((List) value)); - case BOOLEAN_ARRAY: - return new KeyValueMarshaler( - keyUtf8, ArrayAnyValueMarshaler.createBool((List) value)); - case DOUBLE_ARRAY: - return new KeyValueMarshaler( - keyUtf8, ArrayAnyValueMarshaler.createDouble((List) value)); - case EXTENDED_ATTRIBUTES: - return new KeyValueMarshaler( - keyUtf8, - new KeyValueListAnyValueMarshaler( - new KeyValueListAnyValueMarshaler.KeyValueListMarshaler( - createForExtendedAttributes((ExtendedAttributes) value)))); - case VALUE: - return new KeyValueMarshaler(keyUtf8, AnyValueMarshaler.create((Value) value)); - } - // Error prone ensures the switch statement is complete, otherwise only can happen with - // unaligned versions which are not supported. - throw new IllegalArgumentException("Unsupported attribute type."); - } - - public static int sizeExtendedAttributes(LogRecordData log, MarshalerContext context) { - return ExtendedAttributeKeyValueStatelessMarshaler.sizeExtendedAttributes( - LogRecord.ATTRIBUTES, getExtendedAttributes(log), context); - } - - public static void serializeExtendedAttributes( - Serializer output, LogRecordData log, MarshalerContext context) throws IOException { - ExtendedAttributeKeyValueStatelessMarshaler.serializeExtendedAttributes( - output, LogRecord.ATTRIBUTES, getExtendedAttributes(log), context); - } - - private static ExtendedAttributes getExtendedAttributes(LogRecordData logRecordData) { - if (!(logRecordData instanceof ExtendedLogRecordData)) { - throw new IllegalArgumentException("logRecordData must be ExtendedLogRecordData"); - } - return ((ExtendedLogRecordData) logRecordData).getExtendedAttributes(); - } -} diff --git a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java b/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java index a53a523c642..48a38808cb1 100644 --- a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java +++ b/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogMarshaler.java @@ -15,7 +15,6 @@ import io.opentelemetry.exporter.internal.marshal.ProtoEnumInfo; import io.opentelemetry.exporter.internal.marshal.Serializer; import io.opentelemetry.exporter.internal.otlp.AnyValueMarshaler; -import io.opentelemetry.exporter.internal.otlp.IncubatingUtil; import io.opentelemetry.exporter.internal.otlp.KeyValueMarshaler; import io.opentelemetry.proto.logs.v1.internal.LogRecord; import io.opentelemetry.proto.logs.v1.internal.SeverityNumber; @@ -41,14 +40,8 @@ final class LogMarshaler extends MarshalerWithSize { static LogMarshaler create(LogRecordData logRecordData) { KeyValueMarshaler[] attributeMarshalers = - IncubatingUtil.isExtendedLogRecordData(logRecordData) - ? IncubatingUtil.createdExtendedAttributesMarhsalers(logRecordData) - : KeyValueMarshaler.createForAttributes(logRecordData.getAttributes()); - - int attributeSize = - IncubatingUtil.isExtendedLogRecordData(logRecordData) - ? IncubatingUtil.extendedAttributesSize(logRecordData) - : logRecordData.getAttributes().size(); + KeyValueMarshaler.createForAttributes(logRecordData.getAttributes()); + int attributeSize = logRecordData.getAttributes().size(); MarshalerWithSize bodyMarshaler = logRecordData.getBodyValue() == null diff --git a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogStatelessMarshaler.java b/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogStatelessMarshaler.java index 505c42cbc47..fd8e69a63c8 100644 --- a/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogStatelessMarshaler.java +++ b/exporters/otlp/common/src/main/java/io/opentelemetry/exporter/internal/otlp/logs/LogStatelessMarshaler.java @@ -17,7 +17,6 @@ import io.opentelemetry.exporter.internal.marshal.StatelessMarshalerUtil; import io.opentelemetry.exporter.internal.otlp.AnyValueStatelessMarshaler; import io.opentelemetry.exporter.internal.otlp.AttributeKeyValueStatelessMarshaler; -import io.opentelemetry.exporter.internal.otlp.IncubatingUtil; import io.opentelemetry.proto.logs.v1.internal.LogRecord; import io.opentelemetry.sdk.logs.data.LogRecordData; import java.io.IOException; @@ -43,20 +42,14 @@ public void writeTo(Serializer output, LogRecordData log, MarshalerContext conte LogRecord.BODY, log.getBodyValue(), AnyValueStatelessMarshaler.INSTANCE, context); } - int droppedAttributesCount; - if (IncubatingUtil.isExtendedLogRecordData(log)) { - IncubatingUtil.serializeExtendedAttributes(output, log, context); - droppedAttributesCount = - log.getTotalAttributeCount() - IncubatingUtil.extendedAttributesSize(log); - } else { - output.serializeRepeatedMessageWithContext( - LogRecord.ATTRIBUTES, - log.getAttributes(), - AttributeKeyValueStatelessMarshaler.INSTANCE, - context); - droppedAttributesCount = log.getTotalAttributeCount() - log.getAttributes().size(); - } - output.serializeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount); + output.serializeRepeatedMessageWithContext( + LogRecord.ATTRIBUTES, + log.getAttributes(), + AttributeKeyValueStatelessMarshaler.INSTANCE, + context); + output.serializeUInt32( + LogRecord.DROPPED_ATTRIBUTES_COUNT, + log.getTotalAttributeCount() - log.getAttributes().size()); SpanContext spanContext = log.getSpanContext(); output.serializeFixed32(LogRecord.FLAGS, spanContext.getTraceFlags().asByte()); @@ -87,23 +80,15 @@ public int getBinarySerializedSize(LogRecordData log, MarshalerContext context) StatelessMarshalerUtil.sizeMessageWithContext( LogRecord.BODY, log.getBodyValue(), AnyValueStatelessMarshaler.INSTANCE, context); } - if (IncubatingUtil.isExtendedLogRecordData(log)) { - size += IncubatingUtil.sizeExtendedAttributes(log, context); - - int droppedAttributesCount = - log.getTotalAttributeCount() - IncubatingUtil.extendedAttributesSize(log); - size += MarshalerUtil.sizeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount); - } else { - size += - StatelessMarshalerUtil.sizeRepeatedMessageWithContext( - LogRecord.ATTRIBUTES, - log.getAttributes(), - AttributeKeyValueStatelessMarshaler.INSTANCE, - context); - - int droppedAttributesCount = log.getTotalAttributeCount() - log.getAttributes().size(); - size += MarshalerUtil.sizeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount); - } + size += + StatelessMarshalerUtil.sizeRepeatedMessageWithContext( + LogRecord.ATTRIBUTES, + log.getAttributes(), + AttributeKeyValueStatelessMarshaler.INSTANCE, + context); + + int droppedAttributesCount = log.getTotalAttributeCount() - log.getAttributes().size(); + size += MarshalerUtil.sizeUInt32(LogRecord.DROPPED_ATTRIBUTES_COUNT, droppedAttributesCount); SpanContext spanContext = log.getSpanContext(); size += MarshalerUtil.sizeFixed32(LogRecord.FLAGS, spanContext.getTraceFlags().asByte()); diff --git a/exporters/otlp/common/src/testIncubating/java/io/opentelemetry/exporter/internal/otlp/logs/LogsRequestMarshalerIncubatingTest.java b/exporters/otlp/common/src/testIncubating/java/io/opentelemetry/exporter/internal/otlp/logs/LogsRequestMarshalerIncubatingTest.java index 84639db8312..3a86dfba05f 100644 --- a/exporters/otlp/common/src/testIncubating/java/io/opentelemetry/exporter/internal/otlp/logs/LogsRequestMarshalerIncubatingTest.java +++ b/exporters/otlp/common/src/testIncubating/java/io/opentelemetry/exporter/internal/otlp/logs/LogsRequestMarshalerIncubatingTest.java @@ -13,9 +13,8 @@ import com.google.protobuf.Message; import com.google.protobuf.util.JsonFormat; import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.api.common.KeyValue; import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; import io.opentelemetry.api.internal.OtelEncodingUtils; import io.opentelemetry.api.logs.Severity; import io.opentelemetry.api.trace.SpanContext; @@ -29,7 +28,6 @@ import io.opentelemetry.exporter.internal.marshal.StatelessMarshaler; import io.opentelemetry.proto.common.v1.AnyValue; import io.opentelemetry.proto.common.v1.ArrayValue; -import io.opentelemetry.proto.common.v1.KeyValue; import io.opentelemetry.proto.common.v1.KeyValueList; import io.opentelemetry.proto.logs.v1.LogRecord; import io.opentelemetry.proto.logs.v1.ResourceLogs; @@ -38,7 +36,6 @@ import io.opentelemetry.sdk.logs.data.LogRecordData; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.logs.TestLogRecordData; -import io.opentelemetry.sdk.testing.logs.internal.TestExtendedLogRecordData; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; @@ -51,7 +48,6 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.EnumSource; -@SuppressWarnings("deprecation") // Testing deprecated EXTENDED_ATTRIBUTES until removed class LogsRequestMarshalerIncubatingTest { private static final byte[] TRACE_ID_BYTES = new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4}; @@ -82,7 +78,7 @@ void toProtoLogRecord(MarshalerSource marshalerSource) { parse( LogRecord.getDefaultInstance(), marshalerSource.create( - TestExtendedLogRecordData.builder() + TestLogRecordData.builder() .setResource( Resource.create(Attributes.builder().put("testKey", "testValue").build())) .setInstrumentationScopeInfo( @@ -96,10 +92,9 @@ void toProtoLogRecord(MarshalerSource marshalerSource) { .setTotalAttributeCount(11) .setTimestamp(12345, TimeUnit.NANOSECONDS) .setObservedTimestamp(6789, TimeUnit.NANOSECONDS) - // Extended fields .setEventName(EVENT_NAME) - .setExtendedAttributes( - ExtendedAttributes.builder() + .setAttributes( + Attributes.builder() .put("str_key", "str_value") .put("str_arr_key", "str_value1", "str_value2") .put("bool_key", true) @@ -109,33 +104,15 @@ void toProtoLogRecord(MarshalerSource marshalerSource) { .put("int_key", 1) .put("int_arr_key", 1, 2) .put( - "kv_list_key", - ExtendedAttributes.builder() - .put("bool_key", true) - .put("double_key", 1.1) - .put("int_key", 1) - .put( - "kv_list_key", - ExtendedAttributes.builder() - .put("str_key", "str_value") - .build()) - .put("str_key", "str_value") - .build()) - .put( - ExtendedAttributeKey.valueKey("value_key"), + "value_key", Value.of( - io.opentelemetry.api.common.KeyValue.of( - "bool_key", Value.of(true)), - io.opentelemetry.api.common.KeyValue.of( - "double_key", Value.of(1.1)), - io.opentelemetry.api.common.KeyValue.of("int_key", Value.of(1)), - io.opentelemetry.api.common.KeyValue.of( + KeyValue.of("bool_key", Value.of(true)), + KeyValue.of("double_key", Value.of(1.1)), + KeyValue.of("int_key", Value.of(1)), + KeyValue.of( "value_key", - Value.of( - io.opentelemetry.api.common.KeyValue.of( - "str_key", Value.of("str_value")))), - io.opentelemetry.api.common.KeyValue.of( - "str_key", Value.of("str_value")))) + Value.of(KeyValue.of("str_key", Value.of("str_value")))), + KeyValue.of("str_key", Value.of("str_value")))) .build()) .build())); @@ -144,7 +121,7 @@ void toProtoLogRecord(MarshalerSource marshalerSource) { assertThat(logRecord.getSeverityText()).isEqualTo("INFO"); assertThat(logRecord.getBody()).isEqualTo(AnyValue.newBuilder().setStringValue(BODY).build()); - assertThat(logRecord.getDroppedAttributesCount()).isEqualTo(1); + assertThat(logRecord.getDroppedAttributesCount()).isEqualTo(2); assertThat(logRecord.getTimeUnixNano()).isEqualTo(12345); assertThat(logRecord.getObservedTimeUnixNano()).isEqualTo(6789); assertThat(logRecord.getEventName()).isEqualTo(EVENT_NAME); @@ -160,27 +137,6 @@ void toProtoLogRecord(MarshalerSource marshalerSource) { keyValue("double_arr_key", anyValue(Arrays.asList(anyValue(1.1), anyValue(2.2)))), keyValue("int_key", anyValue(1)), keyValue("int_arr_key", anyValue(Arrays.asList(anyValue(1), anyValue(2)))), - keyValue( - "kv_list_key", - AnyValue.newBuilder() - .setKvlistValue( - KeyValueList.newBuilder() - .addValues(keyValue("bool_key", anyValue(true))) - .addValues(keyValue("double_key", anyValue(1.1))) - .addValues(keyValue("int_key", anyValue(1))) - .addValues( - keyValue( - "kv_list_key", - AnyValue.newBuilder() - .setKvlistValue( - KeyValueList.newBuilder() - .addValues( - keyValue("str_key", anyValue("str_value"))) - .build()) - .build())) - .addValues(keyValue("str_key", anyValue("str_value"))) - .build()) - .build()), keyValue( "value_key", AnyValue.newBuilder() @@ -226,8 +182,11 @@ private static AnyValue anyValue(List value) { .build(); } - private static KeyValue keyValue(String key, AnyValue anyValue) { - return KeyValue.newBuilder().setKey(key).setValue(anyValue).build(); + private static io.opentelemetry.proto.common.v1.KeyValue keyValue(String key, AnyValue anyValue) { + return io.opentelemetry.proto.common.v1.KeyValue.newBuilder() + .setKey(key) + .setValue(anyValue) + .build(); } @SuppressWarnings("unchecked") diff --git a/sdk/common/src/main/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesMap.java b/sdk/common/src/main/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesMap.java deleted file mode 100644 index 233fa7eace9..00000000000 --- a/sdk/common/src/main/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesMap.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.common.internal; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.incubator.common.ExtendedAttributesBuilder; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; -import java.util.function.BiConsumer; -import javax.annotation.Nullable; - -/** - * A map with a fixed capacity that drops attributes when the map gets full, and which truncates - * string and array string attribute values to the {@link #lengthLimit}. - * - *

{@link ExtendedAttributes} analog of {@link AttributesMap}. - * - *

This class is internal and is hence not for public use. Its APIs are unstable and can change - * at any time. - */ -@SuppressWarnings("deprecation") -public final class ExtendedAttributesMap extends HashMap, Object> - implements ExtendedAttributes { - - private static final long serialVersionUID = -2674974862318200501L; - - private final long capacity; - private final int lengthLimit; - private int totalAddedValues = 0; - - private ExtendedAttributesMap(long capacity, int lengthLimit) { - this.capacity = capacity; - this.lengthLimit = lengthLimit; - } - - /** - * Create an instance. - * - * @param capacity the max number of extended attribute entries - * @param lengthLimit the maximum length of string attributes - */ - public static ExtendedAttributesMap create(long capacity, int lengthLimit) { - return new ExtendedAttributesMap(capacity, lengthLimit); - } - - /** Add the attribute key value pair, applying capacity and length limits. */ - @Override - @Nullable - public Object put(ExtendedAttributeKey key, @Nullable Object value) { - if (value == null) { - return null; - } - totalAddedValues++; - if (size() >= capacity && !containsKey(key)) { - return null; - } - return super.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit)); - } - - public void putIfCapacity(ExtendedAttributeKey key, @Nullable T value) { - put(key, value); - } - - /** - * Get the total number of extended attributes added, including those dropped for capacity limits. - */ - public int getTotalAddedValues() { - return totalAddedValues; - } - - @SuppressWarnings("unchecked") - @Nullable - @Override - public T get(ExtendedAttributeKey key) { - return (T) super.get(key); - } - - @Override - public Map, Object> asMap() { - // Because ExtendedAttributes is marked Immutable, IDEs may recognize this as redundant usage. - // However, this class is private and is actually mutable, so we need to wrap with - // unmodifiableMap anyways. We implement the immutable ExtendedAttributes for this class to - // support the ExtendedAttributes.builder().putAll usage - it is tricky but an implementation - // detail of this private class. - return Collections.unmodifiableMap(this); - } - - @Override - public ExtendedAttributesBuilder toBuilder() { - return ExtendedAttributes.builder().putAll(this); - } - - @Override - public void forEach(BiConsumer, ? super Object> action) { - // https://github.com/open-telemetry/opentelemetry-java/issues/4161 - // Help out android desugaring by having an explicit call to HashMap.forEach, when forEach is - // just called through ExtendedAttributes.forEach desugaring is unable to correctly handle it. - super.forEach(action); - } - - @Override - public Attributes asAttributes() { - return immutableCopy().asAttributes(); - } - - @Override - public String toString() { - return "ExtendedAttributesMap{" - + "data=" - + super.toString() - + ", capacity=" - + capacity - + ", totalAddedValues=" - + totalAddedValues - + '}'; - } - - /** Create an immutable copy of the extended attributes in this map. */ - public ExtendedAttributes immutableCopy() { - return ExtendedAttributes.builder().putAll(this).build(); - } -} diff --git a/sdk/common/src/test/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesValueTest.java b/sdk/common/src/test/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesValueTest.java deleted file mode 100644 index e3350b82efd..00000000000 --- a/sdk/common/src/test/java/io/opentelemetry/sdk/common/internal/ExtendedAttributesValueTest.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.common.internal; - -import static org.assertj.core.api.Assertions.assertThat; - -import io.opentelemetry.api.common.KeyValue; -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import java.nio.ByteBuffer; -import java.util.List; -import org.junit.jupiter.api.Test; - -@SuppressWarnings("deprecation") -class ExtendedAttributesValueTest { - - @Test - void put_ByteArrayTruncation() { - ExtendedAttributesMap map = ExtendedAttributesMap.create(128, 5); - byte[] bytes = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; - Value value = Value.of(bytes); - - map.put(ExtendedAttributeKey.valueKey("key"), value); - - Value result = map.get(ExtendedAttributeKey.valueKey("key")); - ByteBuffer buffer = (ByteBuffer) result.getValue(); - byte[] resultBytes = new byte[buffer.remaining()]; - buffer.get(resultBytes); - assertThat(resultBytes).containsExactly(1, 2, 3, 4, 5); - } - - @Test - void put_ValueArrayTruncation() { - ExtendedAttributesMap map = ExtendedAttributesMap.create(128, 5); - - Value arrayValue = Value.of(Value.of("short"), Value.of("this is too long")); - - map.put(ExtendedAttributeKey.valueKey("key"), arrayValue); - - Value result = map.get(ExtendedAttributeKey.valueKey("key")); - @SuppressWarnings("unchecked") - List> resultList = (List>) result.getValue(); - assertThat(resultList).hasSize(2); - assertThat(resultList.get(0).getValue()).isEqualTo("short"); - assertThat(resultList.get(1).getValue()).isEqualTo("this "); - } - - @Test - void put_ValueKeyValueListTruncation() { - ExtendedAttributesMap map = ExtendedAttributesMap.create(128, 5); - - Value kvListValue = - Value.of( - KeyValue.of("key1", Value.of("short")), - KeyValue.of("key2", Value.of("this is too long"))); - - map.put(ExtendedAttributeKey.valueKey("key"), kvListValue); - - Value result = map.get(ExtendedAttributeKey.valueKey("key")); - @SuppressWarnings("unchecked") - List resultList = (List) result.getValue(); - assertThat(resultList).hasSize(2); - assertThat(resultList.get(0).getValue().getValue()).isEqualTo("short"); - assertThat(resultList.get(1).getValue().getValue()).isEqualTo("this "); - } -} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java index 2300d9ae7ae..8e627207fca 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java @@ -7,24 +7,18 @@ import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; import io.opentelemetry.api.incubator.logs.ExtendedLogRecordBuilder; import io.opentelemetry.api.logs.Severity; -import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -import io.opentelemetry.sdk.common.internal.ExtendedAttributesMap; import java.time.Instant; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; /** SDK implementation of {@link ExtendedLogRecordBuilder}. */ -@SuppressWarnings("deprecation") final class ExtendedSdkLogRecordBuilder extends SdkLogRecordBuilder implements ExtendedLogRecordBuilder { - @Nullable private ExtendedAttributesMap extendedAttributes; - ExtendedSdkLogRecordBuilder( LoggerSharedState loggerSharedState, InstrumentationScopeInfo instrumentationScopeInfo, @@ -98,51 +92,9 @@ public ExtendedSdkLogRecordBuilder setBody(Value value) { return this; } - @Override - public ExtendedSdkLogRecordBuilder setAttribute(ExtendedAttributeKey key, T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return this; - } - if (this.extendedAttributes == null) { - this.extendedAttributes = - ExtendedAttributesMap.create( - logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength()); - } - this.extendedAttributes.put(key, value); - return this; - } - @Override public ExtendedSdkLogRecordBuilder setAttribute(AttributeKey key, @Nullable T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return this; - } - return setAttribute(ExtendedAttributeKey.fromAttributeKey(key), value); - } - - @Override - protected ReadWriteLogRecord createLogRecord(Context context, long observedTimestampEpochNanos) { - return ExtendedSdkReadWriteLogRecord.create( - loggerSharedState.getLogLimits(), - loggerSharedState.getResource(), - instrumentationScopeInfo, - eventName, - timestampEpochNanos, - observedTimestampEpochNanos, - Span.fromContext(context).getSpanContext(), - severity, - severityText, - body, - extendedAttributes); - } - - @Override - protected void setExceptionAttribute(AttributeKey key, @Nullable T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return; - } - if (extendedAttributes == null || extendedAttributes.get(key) == null) { - setAttribute(key, value); - } + super.setAttribute(key, value); + return this; } } diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordData.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordData.java deleted file mode 100644 index 842a7ec2ea6..00000000000 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordData.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.logs; - -import com.google.auto.value.AutoValue; -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.logs.Severity; -import io.opentelemetry.api.trace.SpanContext; -import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -import io.opentelemetry.sdk.logs.data.Body; -import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData; -import io.opentelemetry.sdk.resources.Resource; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -@AutoValue -@AutoValue.CopyAnnotations -@Immutable -@SuppressWarnings("deprecation") -abstract class ExtendedSdkLogRecordData implements ExtendedLogRecordData { - - ExtendedSdkLogRecordData() {} - - static ExtendedSdkLogRecordData create( - Resource resource, - InstrumentationScopeInfo instrumentationScopeInfo, - @Nullable String eventName, - long epochNanos, - long observedEpochNanos, - SpanContext spanContext, - Severity severity, - @Nullable String severityText, - @Nullable Value body, - ExtendedAttributes attributes, - int totalAttributeCount) { - return new AutoValue_ExtendedSdkLogRecordData( - resource, - instrumentationScopeInfo, - epochNanos, - observedEpochNanos, - spanContext, - severity, - severityText, - totalAttributeCount, - attributes, - body, - eventName); - } - - @Override - @Nullable - public abstract Value getBodyValue(); - - @Override - @Nullable - public abstract String getEventName(); - - @Override - @SuppressWarnings("deprecation") // Implementation of deprecated method - public Body getBody() { - Value valueBody = getBodyValue(); - return valueBody == null ? Body.empty() : Body.string(valueBody.asString()); - } -} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkReadWriteLogRecord.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkReadWriteLogRecord.java deleted file mode 100644 index be8417b0bb5..00000000000 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkReadWriteLogRecord.java +++ /dev/null @@ -1,167 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.logs; - -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.internal.GuardedBy; -import io.opentelemetry.api.logs.Severity; -import io.opentelemetry.api.trace.SpanContext; -import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -import io.opentelemetry.sdk.common.internal.ExtendedAttributesMap; -import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData; -import io.opentelemetry.sdk.logs.internal.ExtendedReadWriteLogRecord; -import io.opentelemetry.sdk.resources.Resource; -import javax.annotation.Nullable; -import javax.annotation.concurrent.ThreadSafe; - -@ThreadSafe -@SuppressWarnings("deprecation") -class ExtendedSdkReadWriteLogRecord extends SdkReadWriteLogRecord - implements ExtendedReadWriteLogRecord { - - private final Object lock = new Object(); - - @GuardedBy("lock") - @Nullable - private ExtendedAttributesMap extendedAttributes; - - @SuppressWarnings("unused") - private ExtendedSdkReadWriteLogRecord( - LogLimits logLimits, - Resource resource, - InstrumentationScopeInfo instrumentationScopeInfo, - @Nullable String eventName, - long timestampEpochNanos, - long observedTimestampEpochNanos, - SpanContext spanContext, - Severity severity, - @Nullable String severityText, - @Nullable Value body, - @Nullable ExtendedAttributesMap extendedAttributes) { - super( - logLimits, - resource, - instrumentationScopeInfo, - timestampEpochNanos, - observedTimestampEpochNanos, - spanContext, - severity, - severityText, - body, - null, - eventName); - this.extendedAttributes = extendedAttributes; - } - - /** Create the extended log record with the given configuration. */ - static ExtendedSdkReadWriteLogRecord create( - LogLimits logLimits, - Resource resource, - InstrumentationScopeInfo instrumentationScopeInfo, - @Nullable String eventName, - long timestampEpochNanos, - long observedTimestampEpochNanos, - SpanContext spanContext, - Severity severity, - @Nullable String severityText, - @Nullable Value body, - @Nullable ExtendedAttributesMap extendedAttributes) { - return new ExtendedSdkReadWriteLogRecord( - logLimits, - resource, - instrumentationScopeInfo, - eventName, - timestampEpochNanos, - observedTimestampEpochNanos, - spanContext, - severity, - severityText, - body, - extendedAttributes); - } - - @Override - public ExtendedSdkReadWriteLogRecord setAttribute(AttributeKey key, T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return this; - } - return setAttribute(ExtendedAttributeKey.fromAttributeKey(key), value); - } - - @Override - public ExtendedSdkReadWriteLogRecord setAttribute(ExtendedAttributeKey key, T value) { - if (key == null || key.getKey().isEmpty() || value == null) { - return this; - } - synchronized (lock) { - if (extendedAttributes == null) { - extendedAttributes = - ExtendedAttributesMap.create( - logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength()); - } - extendedAttributes.put(key, value); - } - return this; - } - - private ExtendedAttributes getImmutableExtendedAttributes() { - synchronized (lock) { - if (extendedAttributes == null) { - return ExtendedAttributes.empty(); - } - return extendedAttributes.immutableCopy(); - } - } - - @Override - public ExtendedLogRecordData toLogRecordData() { - synchronized (lock) { - return ExtendedSdkLogRecordData.create( - resource, - instrumentationScopeInfo, - eventName, - timestampEpochNanos, - observedTimestampEpochNanos, - spanContext, - severity, - severityText, - body, - getImmutableExtendedAttributes(), - extendedAttributes == null ? 0 : extendedAttributes.getTotalAddedValues()); - } - } - - @Override - public Attributes getAttributes() { - return getExtendedAttributes().asAttributes(); - } - - @Nullable - @Override - public T getAttribute(AttributeKey key) { - return getAttribute(ExtendedAttributeKey.fromAttributeKey(key)); - } - - @Nullable - @Override - public T getAttribute(ExtendedAttributeKey key) { - synchronized (lock) { - if (extendedAttributes == null || extendedAttributes.isEmpty()) { - return null; - } - return extendedAttributes.get(key); - } - } - - @Override - public ExtendedAttributes getExtendedAttributes() { - return getImmutableExtendedAttributes(); - } -} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java index a99165d33e5..7f51ad9f4b2 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java @@ -20,18 +20,18 @@ /** SDK implementation of {@link LogRecordBuilder}. */ class SdkLogRecordBuilder implements LogRecordBuilder { - protected final LoggerSharedState loggerSharedState; - protected final LogLimits logLimits; - protected final SdkLogger logger; - - protected final InstrumentationScopeInfo instrumentationScopeInfo; - protected long timestampEpochNanos; - protected long observedTimestampEpochNanos; - @Nullable protected Context context; - protected Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER; - @Nullable protected String severityText; - @Nullable protected Value body; - @Nullable protected String eventName; + private final LoggerSharedState loggerSharedState; + private final LogLimits logLimits; + private final SdkLogger logger; + + private final InstrumentationScopeInfo instrumentationScopeInfo; + private long timestampEpochNanos; + private long observedTimestampEpochNanos; + @Nullable private Context context; + private Severity severity = Severity.UNDEFINED_SEVERITY_NUMBER; + @Nullable private String severityText; + @Nullable private Value body; + @Nullable private String eventName; @Nullable private AttributesMap attributes; SdkLogRecordBuilder( diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java index 8ea462bc82c..4bb837668fd 100644 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java +++ b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java @@ -21,16 +21,16 @@ @ThreadSafe class SdkReadWriteLogRecord implements ReadWriteLogRecord { - protected final LogLimits logLimits; - protected final Resource resource; - protected final InstrumentationScopeInfo instrumentationScopeInfo; - protected final long timestampEpochNanos; - protected final long observedTimestampEpochNanos; - protected final SpanContext spanContext; - protected final Severity severity; - @Nullable protected final String severityText; - @Nullable protected final Value body; - @Nullable protected String eventName; + private final LogLimits logLimits; + private final Resource resource; + private final InstrumentationScopeInfo instrumentationScopeInfo; + private final long timestampEpochNanos; + private final long observedTimestampEpochNanos; + private final SpanContext spanContext; + private final Severity severity; + @Nullable private final String severityText; + @Nullable private final Value body; + @Nullable private final String eventName; private final Object lock = new Object(); @GuardedBy("lock") @@ -105,7 +105,7 @@ public ReadWriteLogRecord setAttribute(AttributeKey key, T value) { return this; } - private Attributes getImmutableAttributes() { + protected Attributes getImmutableAttributes() { synchronized (lock) { if (attributes == null || attributes.isEmpty()) { return Attributes.empty(); diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/internal/ExtendedLogRecordData.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/internal/ExtendedLogRecordData.java deleted file mode 100644 index 18e2c8f57a5..00000000000 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/data/internal/ExtendedLogRecordData.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.logs.data.internal; - -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.sdk.logs.data.LogRecordData; - -/** - * This class is internal and experimental. Its APIs are unstable and can change at any time. Its - * APIs (or a version of them) may be promoted to the public stable API in the future, but no - * guarantees are made. - */ -@SuppressWarnings("deprecation") -public interface ExtendedLogRecordData extends LogRecordData { - - /** Returns the attributes for this log, or {@link ExtendedAttributes#empty()} if unset. */ - ExtendedAttributes getExtendedAttributes(); - - /** - * Returns the attributes for this log, or {@link Attributes#empty()} if unset. - * - * @deprecated Use {@link #getExtendedAttributes()}. - */ - @Override - @Deprecated - default Attributes getAttributes() { - return getExtendedAttributes().asAttributes(); - } -} diff --git a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/internal/ExtendedReadWriteLogRecord.java b/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/internal/ExtendedReadWriteLogRecord.java deleted file mode 100644 index 130bbc1471d..00000000000 --- a/sdk/logs/src/main/java/io/opentelemetry/sdk/logs/internal/ExtendedReadWriteLogRecord.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.logs.internal; - -import io.opentelemetry.api.incubator.common.ExtendedAttributeKey; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.sdk.logs.ReadWriteLogRecord; -import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData; -import javax.annotation.Nullable; - -/** - * A collection of configuration options which define the behavior of a {@link - * io.opentelemetry.api.logs.Logger}. - * - *

This class is internal and experimental. Its APIs are unstable and can change at any time. Its - * APIs (or a version of them) may be promoted to the public stable API in the future, but no - * guarantees are made. - */ -@SuppressWarnings("deprecation") -public interface ExtendedReadWriteLogRecord extends ReadWriteLogRecord { - - /** - * Sets an attribute on the log record. If the log record previously contained a mapping for the - * key, the old value is replaced by the specified value. - * - *

Note: the behavior of null values is undefined, and hence strongly discouraged. - */ - ExtendedReadWriteLogRecord setAttribute(ExtendedAttributeKey key, T value); - - /** - * Sets attributes to the {@link ReadWriteLogRecord}. If the {@link ReadWriteLogRecord} previously - * contained a mapping for any of the keys, the old values are replaced by the specified values. - * - * @param extendedAttributes the attributes - * @return this. - */ - @SuppressWarnings("unchecked") - default ExtendedReadWriteLogRecord setAllAttributes(ExtendedAttributes extendedAttributes) { - if (extendedAttributes == null || extendedAttributes.isEmpty()) { - return this; - } - extendedAttributes.forEach( - (attributeKey, value) -> - this.setAttribute((ExtendedAttributeKey) attributeKey, value)); - return this; - } - - /** Return an immutable {@link ExtendedLogRecordData} instance representing this log record. */ - @Override - ExtendedLogRecordData toLogRecordData(); - - /** - * Returns the value of a given attribute if it exists. This is the equivalent of calling - * getAttributes().get(key) - */ - @Nullable - T getAttribute(ExtendedAttributeKey key); - - /** Returns the attributes for this log, or {@link ExtendedAttributes#empty()} if unset. */ - ExtendedAttributes getExtendedAttributes(); -} diff --git a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/logs/internal/TestExtendedLogRecordData.java b/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/logs/internal/TestExtendedLogRecordData.java deleted file mode 100644 index 44fe6bf6bfa..00000000000 --- a/sdk/testing/src/main/java/io/opentelemetry/sdk/testing/logs/internal/TestExtendedLogRecordData.java +++ /dev/null @@ -1,200 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.sdk.testing.logs.internal; - -import com.google.auto.value.AutoValue; -import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.common.Value; -import io.opentelemetry.api.incubator.common.ExtendedAttributes; -import io.opentelemetry.api.logs.Severity; -import io.opentelemetry.api.trace.SpanContext; -import io.opentelemetry.sdk.common.InstrumentationScopeInfo; -import io.opentelemetry.sdk.logs.data.Body; -import io.opentelemetry.sdk.logs.data.LogRecordData; -import io.opentelemetry.sdk.logs.data.internal.ExtendedLogRecordData; -import io.opentelemetry.sdk.resources.Resource; -import java.time.Instant; -import java.util.concurrent.TimeUnit; -import javax.annotation.Nullable; -import javax.annotation.concurrent.Immutable; - -/** - * Immutable representation of {@link LogRecordData}. - * - *

This class is internal and experimental. Its APIs are unstable and can change at any time. Its - * APIs (or a version of them) may be promoted to the public stable API in the future, but no - * guarantees are made. - * - * @since 1.27.0 - */ -@Immutable -@AutoValue -@AutoValue.CopyAnnotations -// Carry suppression for Body to AutoValue implementation via @AutoValue.CopyAnnotations -@SuppressWarnings("deprecation") -public abstract class TestExtendedLogRecordData implements ExtendedLogRecordData { - - /** Creates a new Builder for creating an {@link LogRecordData} instance. */ - public static Builder builder() { - return new AutoValue_TestExtendedLogRecordData.Builder() - .setResource(Resource.empty()) - .setInstrumentationScopeInfo(InstrumentationScopeInfo.empty()) - .setTimestamp(0, TimeUnit.NANOSECONDS) - .setObservedTimestamp(0, TimeUnit.NANOSECONDS) - .setSpanContext(SpanContext.getInvalid()) - .setSeverity(Severity.UNDEFINED_SEVERITY_NUMBER) - .setAttributes(Attributes.empty()) - .setTotalAttributeCount(0); - } - - @Deprecated - public Body getBody() { - Value valueBody = getBodyValue(); - return valueBody == null ? Body.empty() : Body.string(valueBody.asString()); - } - - /** - * {@inheritDoc} - * - * @since 1.42.0 - */ - @Override - @Nullable - public abstract Value getBodyValue(); - - @Override - @Nullable - public abstract String getEventName(); - - TestExtendedLogRecordData() {} - - /** - * A {@code Builder} class for {@link TestExtendedLogRecordData}. - * - *

This class is internal and experimental. Its APIs are unstable and can change at any time. - * Its APIs (or a version of them) may be promoted to the public stable API in the future, but no - * guarantees are made. - */ - @AutoValue.Builder - public abstract static class Builder { - - abstract TestExtendedLogRecordData autoBuild(); - - /** Create a new {@link LogRecordData} instance from the data in this. */ - public TestExtendedLogRecordData build() { - return autoBuild(); - } - - /** Set the {@link Resource}. */ - public abstract Builder setResource(Resource resource); - - /** Sets the {@link InstrumentationScopeInfo}. */ - public abstract Builder setInstrumentationScopeInfo( - InstrumentationScopeInfo instrumentationScopeInfo); - - /** Sets the event name. */ - public abstract Builder setEventName(String eventName); - - /** - * Set the epoch {@code timestamp}, using the instant. - * - *

The {@code timestamp} is the time at which the log record occurred. - */ - public Builder setTimestamp(Instant instant) { - return setTimestampEpochNanos( - TimeUnit.SECONDS.toNanos(instant.getEpochSecond()) + instant.getNano()); - } - - /** - * Set the epoch {@code timestamp}, using the timestamp and unit. - * - *

The {@code timestamp} is the time at which the log record occurred. - */ - public Builder setTimestamp(long timestamp, TimeUnit unit) { - return setTimestampEpochNanos(unit.toNanos(timestamp)); - } - - /** - * Set the epoch {@code timestamp}. - * - *

The {@code timestamp} is the time at which the log record occurred. - */ - abstract Builder setTimestampEpochNanos(long epochNanos); - - /** - * Set the {@code observedTimestamp}, using the instant. - * - *

The {@code observedTimestamp} is the time at which the log record was observed. - */ - public Builder setObservedTimestamp(Instant instant) { - return setObservedTimestampEpochNanos( - TimeUnit.SECONDS.toNanos(instant.getEpochSecond()) + instant.getNano()); - } - - /** - * Set the epoch {@code observedTimestamp}, using the timestamp and unit. - * - *

The {@code observedTimestamp} is the time at which the log record was observed. - */ - public Builder setObservedTimestamp(long timestamp, TimeUnit unit) { - return setObservedTimestampEpochNanos(unit.toNanos(timestamp)); - } - - /** - * Set the epoch {@code observedTimestamp}. - * - *

The {@code observedTimestamp} is the time at which the log record was observed. - */ - abstract Builder setObservedTimestampEpochNanos(long epochNanos); - - /** Set the span context. */ - public abstract Builder setSpanContext(SpanContext spanContext); - - /** Set the severity. */ - public abstract Builder setSeverity(Severity severity); - - /** Set the severity text. */ - public abstract Builder setSeverityText(String severityText); - - /** Set the body string. */ - public Builder setBody(String body) { - return setBodyValue(Value.of(body)); - } - - /** - * Set the body. - * - * @deprecated Use {@link #setBodyValue(Value)}. - */ - @Deprecated - public Builder setBody(Body body) { - if (body.getType() == Body.Type.STRING) { - setBodyValue(Value.of(body.asString())); - } else if (body.getType() == Body.Type.EMPTY) { - setBodyValue(null); - } - return this; - } - - /** - * Set the body. - * - * @since 1.42.0 - */ - public abstract Builder setBodyValue(@Nullable Value body); - - /** Set the attributes. */ - public Builder setAttributes(Attributes attributes) { - return setExtendedAttributes(ExtendedAttributes.builder().putAll(attributes).build()); - } - - /** Set the total attribute count. */ - public abstract Builder setTotalAttributeCount(int totalAttributeCount); - - /** Set extended attributes. * */ - public abstract Builder setExtendedAttributes(ExtendedAttributes extendedAttributes); - } -} diff --git a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java index 0a144a24eef..81799e7769b 100644 --- a/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java +++ b/sdk/testing/src/test/java/io/opentelemetry/sdk/testing/assertj/LogAssertionsTest.java @@ -28,7 +28,7 @@ import io.opentelemetry.sdk.logs.export.SimpleLogRecordProcessor; import io.opentelemetry.sdk.resources.Resource; import io.opentelemetry.sdk.testing.exporter.InMemoryLogRecordExporter; -import io.opentelemetry.sdk.testing.logs.internal.TestExtendedLogRecordData; +import io.opentelemetry.sdk.testing.logs.TestLogRecordData; import java.util.Arrays; import java.util.List; import java.util.concurrent.TimeUnit; @@ -57,7 +57,7 @@ public class LogAssertionsTest { .build(); private static final LogRecordData LOG_DATA = - TestExtendedLogRecordData.builder() + TestLogRecordData.builder() .setResource(RESOURCE) .setInstrumentationScopeInfo(INSTRUMENTATION_SCOPE_INFO) .setEventName("event name") From ef6dca02e7ba61a56ad2d598682dd92e55c7f793 Mon Sep 17 00:00:00 2001 From: Jack Berg <34418638+jack-berg@users.noreply.github.com> Date: Mon, 11 May 2026 16:17:41 -0500 Subject: [PATCH 2/2] More remnants --- .../java/io/opentelemetry/api/common/AttributeKey.java | 7 +------ sdk/common/build.gradle.kts | 1 - 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java b/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java index 3fd822dd07d..3cc471f29e4 100644 --- a/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java +++ b/api/all/src/main/java/io/opentelemetry/api/common/AttributeKey.java @@ -26,11 +26,6 @@ public interface AttributeKey { /** Returns the type of attribute for this key. Useful for building switch statements. */ AttributeType getType(); - // TODO (jack-berg): uncomment when extended attributes are promoted from incubator to API - // default ExtendedAttributeKey asExtendedAttributeKey() { - // return InternalAttributeKeyImpl.toExtendedAttributeKey(this); - // } - /** Returns a new AttributeKey for String valued attributes. */ static AttributeKey stringKey(String key) { return InternalAttributeKeyImpl.create(key, AttributeType.STRING); @@ -72,7 +67,7 @@ static AttributeKey> doubleArrayKey(String key) { } /** - * Returns a new ExtendedAttributeKey for {@link Value} valued attributes. + * Returns a new AttributeKey for {@link Value} valued attributes. * *

Simple attributes ({@link AttributeType#STRING}, {@link AttributeType#LONG}, {@link * AttributeType#DOUBLE}, {@link AttributeType#BOOLEAN}, {@link AttributeType#STRING_ARRAY}, diff --git a/sdk/common/build.gradle.kts b/sdk/common/build.gradle.kts index e695eaf1d83..f046ea24554 100644 --- a/sdk/common/build.gradle.kts +++ b/sdk/common/build.gradle.kts @@ -20,7 +20,6 @@ dependencies { testAnnotationProcessor("com.google.auto.value:auto-value") - testImplementation(project(":api:incubator")) // for ExtendedAttributesValueTest testImplementation(project(":sdk:testing")) testImplementation("com.google.guava:guava-testlib") testImplementation("io.opentelemetry.semconv:opentelemetry-semconv-incubating")