Skip to content

Commit 8c5e2f6

Browse files
committed
Consolidate to single ExceptionAttributeResolver method
1 parent c31ba99 commit 8c5e2f6

File tree

16 files changed

+79
-134
lines changed

16 files changed

+79
-134
lines changed

sdk/common/src/main/java/io/opentelemetry/sdk/internal/AttributeUtil.java

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,12 @@
55

66
package io.opentelemetry.sdk.internal;
77

8-
import static io.opentelemetry.sdk.internal.ExceptionAttributeResolver.EXCEPTION_MESSAGE;
9-
import static io.opentelemetry.sdk.internal.ExceptionAttributeResolver.EXCEPTION_STACKTRACE;
10-
import static io.opentelemetry.sdk.internal.ExceptionAttributeResolver.EXCEPTION_TYPE;
11-
128
import io.opentelemetry.api.common.AttributeKey;
139
import io.opentelemetry.api.common.Attributes;
1410
import io.opentelemetry.api.common.AttributesBuilder;
1511
import java.util.ArrayList;
1612
import java.util.List;
1713
import java.util.Map;
18-
import java.util.function.BiConsumer;
1914
import java.util.function.Predicate;
2015

2116
/**
@@ -100,28 +95,4 @@ public static Object applyAttributeLengthLimit(Object value, int lengthLimit) {
10095
}
10196
return value;
10297
}
103-
104-
public static void addExceptionAttributes(
105-
ExceptionAttributeResolver exceptionAttributeResolver,
106-
int maxAttributeLength,
107-
Throwable exception,
108-
BiConsumer<AttributeKey<String>, String> attributeConsumer) {
109-
String exceptionType =
110-
exceptionAttributeResolver.getExceptionType(exception, maxAttributeLength);
111-
if (exceptionType != null) {
112-
attributeConsumer.accept(EXCEPTION_TYPE, exceptionType);
113-
}
114-
115-
String exceptionMessage =
116-
exceptionAttributeResolver.getExceptionMessage(exception, maxAttributeLength);
117-
if (exceptionMessage != null) {
118-
attributeConsumer.accept(EXCEPTION_MESSAGE, exceptionMessage);
119-
}
120-
121-
String stackTrace =
122-
exceptionAttributeResolver.getExceptionStacktrace(exception, maxAttributeLength);
123-
if (stackTrace != null) {
124-
attributeConsumer.accept(EXCEPTION_STACKTRACE, stackTrace);
125-
}
126-
}
12798
}

sdk/common/src/main/java/io/opentelemetry/sdk/internal/AttributesMap.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
* <p>WARNING: In order to reduce memory allocation, this class extends {@link HashMap} when it
2222
* would be more appropriate to delegate. The problem with extending is that we don't enforce that
2323
* all {@link HashMap} methods for reading / writing data conform to the configured attribute
24-
* limits. Therefore, it's easy to accidentally call something like {@link Map#putAll(Map)} or
25-
* {@link Map#put(Object, Object)} and bypass the restrictions (see <a
24+
* limits. Therefore, it's easy to accidentally call something like {@link Map#putAll(Map)} and
25+
* bypass the restrictions (see <a
2626
* href="https://github.com/open-telemetry/opentelemetry-java/issues/7135">#7135</a>). Callers MUST
2727
* take care to only call methods from {@link AttributesMap}, and not {@link HashMap}.
2828
*
@@ -58,14 +58,22 @@ public static AttributesMap create(long capacity, int lengthLimit) {
5858
*/
5959
@Override
6060
@Nullable
61-
public Object put(AttributeKey<?> key, Object value) {
61+
public Object put(AttributeKey<?> key, @Nullable Object value) {
62+
if (value == null) {
63+
return null;
64+
}
6265
totalAddedValues++;
6366
if (size() >= capacity && !containsKey(key)) {
6467
return null;
6568
}
6669
return super.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
6770
}
6871

72+
/** Generic overload of {@link #put(AttributeKey, Object)}. */
73+
public <T> void putIfCapacity(AttributeKey<T> key, @Nullable T value) {
74+
put(key, value);
75+
}
76+
6977
/** Get the total number of attributes added, including those dropped for capacity limits. */
7078
public int getTotalAddedValues() {
7179
return totalAddedValues;

sdk/common/src/main/java/io/opentelemetry/sdk/internal/DefaultExceptionAttributeResolver.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.io.PrintWriter;
99
import java.io.StringWriter;
10-
import javax.annotation.Nullable;
1110

1211
/**
1312
* This class is internal and experimental. Its APIs are unstable and can change at any time. Its
@@ -26,24 +25,19 @@ public static ExceptionAttributeResolver getInstance() {
2625
}
2726

2827
@Override
29-
@Nullable
30-
public String getExceptionType(Throwable throwable, int maxAttributeLength) {
31-
return throwable.getClass().getCanonicalName();
32-
}
33-
34-
@Override
35-
@Nullable
36-
public String getExceptionMessage(Throwable throwable, int maxAttributeLength) {
37-
return throwable.getMessage();
38-
}
39-
40-
@Override
41-
@Nullable
42-
public String getExceptionStacktrace(Throwable throwable, int maxAttributeLength) {
28+
public void setExceptionAttributes(
29+
AttributeSetter attributeSetter, Throwable throwable, int maxAttributeLength) {
30+
attributeSetter.setAttribute(
31+
ExceptionAttributeResolver.EXCEPTION_TYPE, throwable.getClass().getCanonicalName());
32+
String message = throwable.getMessage();
33+
if (message != null) {
34+
attributeSetter.setAttribute(ExceptionAttributeResolver.EXCEPTION_MESSAGE, message);
35+
}
4336
StringWriter stringWriter = new StringWriter();
4437
try (PrintWriter printWriter = new PrintWriter(stringWriter)) {
4538
throwable.printStackTrace(printWriter);
4639
}
47-
return stringWriter.toString();
40+
attributeSetter.setAttribute(
41+
ExceptionAttributeResolver.EXCEPTION_STACKTRACE, stringWriter.toString());
4842
}
4943
}

sdk/common/src/main/java/io/opentelemetry/sdk/internal/ExceptionAttributeResolver.java

Lines changed: 10 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,17 @@ public interface ExceptionAttributeResolver {
2121
AttributeKey<String> EXCEPTION_MESSAGE = AttributeKey.stringKey("exception.message");
2222
AttributeKey<String> EXCEPTION_STACKTRACE = AttributeKey.stringKey("exception.stacktrace");
2323

24-
/**
25-
* Resolve the {@link #EXCEPTION_TYPE} attribute from the {@code throwable}, or {@code null} if no
26-
* value should be set.
27-
*
28-
* @param throwable the throwable
29-
* @param maxAttributeLength the max attribute length that will be retained by the SDK. Responses
30-
* are not required to conform to this limit, but implementations may incorporate this limit
31-
* to avoid unnecessary compute.
32-
*/
33-
@Nullable
34-
String getExceptionType(Throwable throwable, int maxAttributeLength);
35-
36-
/**
37-
* Resolve the {@link #EXCEPTION_MESSAGE} attribute from the {@code throwable}, or {@code null} if
38-
* no value should be set.
39-
*
40-
* @param throwable the throwable
41-
* @param maxAttributeLength the max attribute length that will be retained by the SDK. Responses
42-
* are not required to conform to this limit, but implementations may incorporate this limit
43-
* to avoid unnecessary compute.
44-
*/
45-
@Nullable
46-
String getExceptionMessage(Throwable throwable, int maxAttributeLength);
24+
void setExceptionAttributes(
25+
AttributeSetter attributeSetter, Throwable throwable, int maxAttributeLength);
4726

4827
/**
49-
* Resolve the {@link #EXCEPTION_STACKTRACE} attribute from the {@code throwable}, or {@code null}
50-
* if no value should be set.
51-
*
52-
* @param throwable the throwable
53-
* @param maxAttributeLength the max attribute length that will be retained by the SDK. Responses
54-
* are not required to conform to this limit, but implementations may incorporate this limit
55-
* to avoid unnecessary compute.
28+
* This class is internal and experimental. Its APIs are unstable and can change at any time. Its
29+
* APIs (or a version of them) may be promoted to the public stable API in the future, but no
30+
* guarantees are made.
5631
*/
57-
@Nullable
58-
String getExceptionStacktrace(Throwable throwable, int maxAttributeLength);
32+
// TODO(jack-berg): Consider promoting to opentelemetry and extending with Span, LogRecordBuilder,
33+
// AttributeBuilder, AttributesMap etc.
34+
interface AttributeSetter {
35+
<T> void setAttribute(AttributeKey<T> key, @Nullable T value);
36+
}
5937
}

sdk/common/src/main/java/io/opentelemetry/sdk/internal/ExtendedAttributesMap.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,23 @@ public static ExtendedAttributesMap create(long capacity, int lengthLimit) {
4949
}
5050

5151
/** Add the attribute key value pair, applying capacity and length limits. */
52-
public <T> void put(ExtendedAttributeKey<T> key, T value) {
52+
@Override
53+
@Nullable
54+
public Object put(ExtendedAttributeKey<?> key, @Nullable Object value) {
55+
if (value == null) {
56+
return null;
57+
}
5358
totalAddedValues++;
54-
// TODO(jack-berg): apply capcity to nested entries
59+
// TODO(jack-berg): apply capacity to nested entries
5560
if (size() >= capacity && !containsKey(key)) {
56-
return;
61+
return null;
5762
}
5863
// TODO(jack-berg): apply limits to nested entries
59-
super.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
64+
return super.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
65+
}
66+
67+
public <T> void putIfCapacity(ExtendedAttributeKey<T> key, @Nullable T value) {
68+
put(key, value);
6069
}
6170

6271
/**

sdk/common/src/test/java/io/opentelemetry/sdk/internal/AttributesMapTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ class AttributesMapTest {
1616
@Test
1717
void asMap() {
1818
AttributesMap attributesMap = AttributesMap.create(2, Integer.MAX_VALUE);
19-
attributesMap.put(longKey("one"), 1L);
20-
attributesMap.put(longKey("two"), 2L);
19+
attributesMap.putIfCapacity(longKey("one"), 1L);
20+
attributesMap.putIfCapacity(longKey("two"), 2L);
2121

2222
assertThat(attributesMap.asMap())
2323
.containsOnly(entry(longKey("one"), 1L), entry(longKey("two"), 2L));

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkLogRecordBuilder.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.opentelemetry.api.trace.Span;
1414
import io.opentelemetry.context.Context;
1515
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
16-
import io.opentelemetry.sdk.internal.AttributeUtil;
1716
import io.opentelemetry.sdk.internal.ExtendedAttributesMap;
1817
import java.time.Instant;
1918
import java.util.concurrent.TimeUnit;
@@ -42,11 +41,12 @@ public ExtendedSdkLogRecordBuilder setException(Throwable throwable) {
4241
return this;
4342
}
4443

45-
AttributeUtil.addExceptionAttributes(
46-
loggerSharedState.getExceptionAttributeResolver(),
47-
loggerSharedState.getLogLimits().getMaxAttributeValueLength(),
48-
throwable,
49-
this::setAttribute);
44+
loggerSharedState
45+
.getExceptionAttributeResolver()
46+
.setExceptionAttributes(
47+
this::setAttribute,
48+
throwable,
49+
loggerSharedState.getLogLimits().getMaxAttributeValueLength());
5050

5151
return this;
5252
}
@@ -115,7 +115,7 @@ public <T> ExtendedSdkLogRecordBuilder setAttribute(ExtendedAttributeKey<T> key,
115115
ExtendedAttributesMap.create(
116116
logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength());
117117
}
118-
this.extendedAttributes.put(key, value);
118+
this.extendedAttributes.putIfCapacity(key, value);
119119
return this;
120120
}
121121

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/ExtendedSdkReadWriteLogRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public <T> ExtendedSdkReadWriteLogRecord setAttribute(ExtendedAttributeKey<T> ke
105105
ExtendedAttributesMap.create(
106106
logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength());
107107
}
108-
extendedAttributes.put(key, value);
108+
extendedAttributes.putIfCapacity(key, value);
109109
}
110110
return this;
111111
}

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkLogRecordBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public <T> SdkLogRecordBuilder setAttribute(AttributeKey<T> key, @Nullable T val
111111
AttributesMap.create(
112112
logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength());
113113
}
114-
this.attributes.put(key, value);
114+
this.attributes.putIfCapacity(key, value);
115115
return this;
116116
}
117117

sdk/logs/src/main/java/io/opentelemetry/sdk/logs/SdkReadWriteLogRecord.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public <T> ReadWriteLogRecord setAttribute(AttributeKey<T> key, T value) {
100100
AttributesMap.create(
101101
logLimits.getMaxNumberOfAttributes(), logLimits.getMaxAttributeValueLength());
102102
}
103-
attributes.put(key, value);
103+
attributes.putIfCapacity(key, value);
104104
}
105105
return this;
106106
}

0 commit comments

Comments
 (0)