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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### API

* Fix `TraceStateBuilder.remove` corrupting the builder when the same key is removed twice
([#8613](https://github.com/open-telemetry/opentelemetry-java/pull/8613))

## Version 1.64.0 (2026-07-10)

### API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,10 @@ public TraceStateBuilder remove(String key) {
}
for (int i = 0; i < reversedEntries.size(); i += 2) {
if (reversedEntries.get(i).equals(key)) {
reversedEntries.set(i + 1, null);
numEntries--;
if (reversedEntries.get(i + 1) != null) {
reversedEntries.set(i + 1, null);
numEntries--;
}
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
class TraceStateTest {
private static final String FIRST_KEY = "key_1";
private static final String SECOND_KEY = "key_2";
private static final String THIRD_KEY = "key_3";
private static final String FIRST_VALUE = "value.1";
private static final String SECOND_VALUE = "value.2";
private static final String THIRD_VALUE = "value.3";

private final TraceState firstTraceState =
TraceState.builder().put(FIRST_KEY, FIRST_VALUE).build();
Expand Down Expand Up @@ -303,6 +305,32 @@ void removeNotPresent() {
.isEqualTo(multiValueTraceState);
}

@Test
void removeTwice() {
assertThat(firstTraceState.toBuilder().remove(FIRST_KEY).remove(FIRST_KEY).build())
.isEqualTo(TraceState.getDefault());
}

@Test
void removeTwice_KeepsRemainingEntry() {
assertThat(multiValueTraceState.toBuilder().remove(FIRST_KEY).remove(FIRST_KEY).build().asMap())
.containsExactly(entry(SECOND_KEY, SECOND_VALUE));
}

@Test
void removeTwice_KeepsRemainingEntries() {
assertThat(
TraceState.builder()
.put(FIRST_KEY, FIRST_VALUE)
.put(SECOND_KEY, SECOND_VALUE)
.put(THIRD_KEY, THIRD_VALUE)
.remove(FIRST_KEY)
.remove(FIRST_KEY)
.build()
.asMap())
.containsExactly(entry(THIRD_KEY, THIRD_VALUE), entry(SECOND_KEY, SECOND_VALUE));
}

@Test
void addAndRemoveEntry() {
assertThat(
Expand Down
Loading