Skip to content

Fix: preserve new baggage values in context on key conflict #1061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ public static Function<Context, Context> append(Map<String, String> baggage) {
private static Context append(Context context, Map<String, String> baggage) {
BaggageToPropagate baggageToPropagate = context.getOrDefault(ObservationAwareBaggageThreadLocalAccessor.KEY,
null);
Map<String, String> mergedBaggage = new HashMap<>(baggage);
Map<String, String> mergedBaggage = new HashMap<>();
if (baggageToPropagate != null) {
mergedBaggage.putAll(baggageToPropagate.getBaggage());
}
mergedBaggage.putAll(baggage);
BaggageToPropagate merged = new BaggageToPropagate(mergedBaggage);
return context.put(ObservationAwareBaggageThreadLocalAccessor.KEY, merged);
}
Expand All @@ -71,10 +72,10 @@ private static Context append(Context context, String key, String value) {
BaggageToPropagate baggageToPropagate = context.getOrDefault(ObservationAwareBaggageThreadLocalAccessor.KEY,
null);
Map<String, String> mergedBaggage = new HashMap<>();
mergedBaggage.put(key, value);
if (baggageToPropagate != null) {
mergedBaggage.putAll(baggageToPropagate.getBaggage());
}
mergedBaggage.put(key, value);
BaggageToPropagate merged = new BaggageToPropagate(mergedBaggage);
return context.put(ObservationAwareBaggageThreadLocalAccessor.KEY, merged);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/**
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.micrometer.tracing.contextpropagation.reactor;

import io.micrometer.tracing.contextpropagation.BaggageToPropagate;
import io.micrometer.tracing.contextpropagation.ObservationAwareBaggageThreadLocalAccessor;
import org.junit.jupiter.api.Test;
import reactor.util.context.Context;

import java.util.HashMap;
import java.util.Map;

import static org.assertj.core.api.BDDAssertions.then;

class ReactorBaggageTests {

@Test
void should_append_single_baggage_entry_to_context() {
Context updatedContext = ReactorBaggage.append("foo", "bar").apply(Context.empty());

BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
then(baggage).isNotNull();
then(baggage.getBaggage()).hasSize(1);
then(baggage.getBaggage()).containsEntry("foo", "bar");
}

@Test
void should_append_multiple_baggage_entries_to_context() {
Map<String, String> baggageEntries = new HashMap<>();
baggageEntries.put("foo", "bar");
baggageEntries.put("baz", "bar2");

Context updatedContext = ReactorBaggage.append(baggageEntries).apply(Context.empty());

BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
then(baggage).isNotNull();
then(baggage.getBaggage()).hasSize(2);
then(baggage.getBaggage()).containsExactlyInAnyOrderEntriesOf(baggageEntries);
}

@Test
void should_merge_existing_baggage_with_new_entries() {
Map<String, String> initialBaggage = new HashMap<>();
initialBaggage.put("foo", "bar");
initialBaggage.put("baz", "bar2");
Context context = Context.of(ObservationAwareBaggageThreadLocalAccessor.KEY,
new BaggageToPropagate(initialBaggage));

Map<String, String> newBaggage = new HashMap<>();
newBaggage.put("foo", "bar1");

Context updatedContext = ReactorBaggage.append(newBaggage).apply(context);

BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
then(baggage).isNotNull();
then(baggage.getBaggage()).hasSize(2);
then(baggage.getBaggage()).containsEntry("foo", "bar1").containsEntry("baz", "bar2");
}

@Test
void should_merge_existing_baggage_with_single_entry() {
Map<String, String> initialBaggage = new HashMap<>();
initialBaggage.put("foo", "bar");
initialBaggage.put("baz", "bar2");
Context context = Context.of(ObservationAwareBaggageThreadLocalAccessor.KEY,
new BaggageToPropagate(initialBaggage));

Context updatedContext = ReactorBaggage.append("foo", "bar1").apply(context);

BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
then(baggage).isNotNull();
then(baggage.getBaggage()).hasSize(2);
then(baggage.getBaggage()).containsEntry("foo", "bar1").containsEntry("baz", "bar2");
}

}