Skip to content

Commit ce448be

Browse files
committed
Fix bug preventing accurate reporting of dropped attribute count
1 parent b5daeb0 commit ce448be

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
* <p>This class is internal and is hence not for public use. Its APIs are unstable and can change
2222
* at any time.
2323
*/
24-
public final class AttributesMap extends HashMap<AttributeKey<?>, Object> implements Attributes {
25-
26-
private static final long serialVersionUID = -5072696312123632376L;
24+
public final class AttributesMap implements Attributes {
2725

26+
private final HashMap<AttributeKey<?>, Object> delegate = new HashMap<>();
2827
private final long capacity;
2928
private final int lengthLimit;
3029
private int totalAddedValues = 0;
@@ -47,10 +46,10 @@ public static AttributesMap create(long capacity, int lengthLimit) {
4746
/** Add the attribute key value pair, applying capacity and length limits. */
4847
public <T> void put(AttributeKey<T> key, T value) {
4948
totalAddedValues++;
50-
if (size() >= capacity && !containsKey(key)) {
49+
if (size() >= capacity && !delegate.containsKey(key)) {
5150
return;
5251
}
53-
super.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
52+
delegate.put(key, AttributeUtil.applyAttributeLengthLimit(value, lengthLimit));
5453
}
5554

5655
/** Get the total number of attributes added, including those dropped for capcity limits. */
@@ -62,7 +61,7 @@ public int getTotalAddedValues() {
6261
@Override
6362
@Nullable
6463
public <T> T get(AttributeKey<T> key) {
65-
return (T) super.get(key);
64+
return (T) delegate.get(key);
6665
}
6766

6867
@Override
@@ -72,7 +71,7 @@ public Map<AttributeKey<?>, Object> asMap() {
7271
// anyways. We implement the immutable Attributes for this class to support the
7372
// Attributes.builder().putAll usage - it is tricky but an implementation detail of this private
7473
// class.
75-
return Collections.unmodifiableMap(this);
74+
return Collections.unmodifiableMap(delegate);
7675
}
7776

7877
@Override
@@ -85,14 +84,24 @@ public void forEach(BiConsumer<? super AttributeKey<?>, ? super Object> action)
8584
// https://github.com/open-telemetry/opentelemetry-java/issues/4161
8685
// Help out android desugaring by having an explicit call to HashMap.forEach, when forEach is
8786
// just called through Attributes.forEach desugaring is unable to correctly handle it.
88-
super.forEach(action);
87+
delegate.forEach(action);
88+
}
89+
90+
@Override
91+
public int size() {
92+
return delegate.size();
93+
}
94+
95+
@Override
96+
public boolean isEmpty() {
97+
return delegate.isEmpty();
8998
}
9099

91100
@Override
92101
public String toString() {
93102
return "AttributesMap{"
94103
+ "data="
95-
+ super.toString()
104+
+ delegate
96105
+ ", capacity="
97106
+ capacity
98107
+ ", totalAddedValues="

sdk/trace/src/main/java/io/opentelemetry/sdk/trace/SdkSpan.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ public ReadWriteSpan recordException(Throwable exception) {
466466
}
467467

468468
@Override
469+
@SuppressWarnings("unchecked")
469470
public ReadWriteSpan recordException(Throwable exception, Attributes additionalAttributes) {
470471
if (exception == null) {
471472
return this;
@@ -495,7 +496,10 @@ public ReadWriteSpan recordException(Throwable exception, Attributes additionalA
495496
attributes.put(EXCEPTION_STACKTRACE, stackTrace);
496497
}
497498

498-
additionalAttributes.forEach(attributes::put);
499+
additionalAttributes.forEach(
500+
(attributeKey, object) -> {
501+
attributes.put((AttributeKey<Object>) attributeKey, object);
502+
});
499503

500504
addTimedEvent(
501505
ExceptionEventData.create(

sdk/trace/src/test/java/io/opentelemetry/sdk/trace/SdkSpanTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,21 @@ void recordException_additionalAttributes() {
12771277
});
12781278
}
12791279

1280+
@Test
1281+
void recordException_SpanLimits() {
1282+
SdkSpan span = createTestSpan(SpanLimits.builder().setMaxNumberOfAttributes(2).build());
1283+
span.recordException(
1284+
new IllegalStateException("error"),
1285+
Attributes.builder().put("key1", "value").put("key2", "value").build());
1286+
1287+
List<EventData> events = span.toSpanData().getEvents();
1288+
assertThat(events.size()).isEqualTo(1);
1289+
EventData event = events.get(0);
1290+
assertThat(event.getAttributes().size()).isEqualTo(2);
1291+
assertThat(event.getTotalAttributeCount()).isEqualTo(5);
1292+
assertThat(event.getTotalAttributeCount() - event.getAttributes().size()).isPositive();
1293+
}
1294+
12801295
@Test
12811296
void badArgsIgnored() {
12821297
SdkSpan span = createTestRootSpan();
@@ -1519,7 +1534,9 @@ void testAsSpanData() {
15191534
Resource resource = this.resource;
15201535
Attributes attributes = TestUtils.generateRandomAttributes();
15211536
AttributesMap attributesWithCapacity = AttributesMap.create(32, Integer.MAX_VALUE);
1522-
attributes.forEach(attributesWithCapacity::put);
1537+
attributes.forEach(
1538+
(attributeKey, object) ->
1539+
attributesWithCapacity.put((AttributeKey<Object>) attributeKey, object));
15231540
Attributes event1Attributes = TestUtils.generateRandomAttributes();
15241541
Attributes event2Attributes = TestUtils.generateRandomAttributes();
15251542
SpanContext context =

0 commit comments

Comments
 (0)