Skip to content

Commit a8feff9

Browse files
committed
Merge branch '1.3.x' into 1.4.x
2 parents 6ac3b7c + e72dc6b commit a8feff9

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

micrometer-tracing/src/main/java/io/micrometer/tracing/contextpropagation/reactor/ReactorBaggage.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ public static Function<Context, Context> append(Map<String, String> baggage) {
5959
private static Context append(Context context, Map<String, String> baggage) {
6060
BaggageToPropagate baggageToPropagate = context.getOrDefault(ObservationAwareBaggageThreadLocalAccessor.KEY,
6161
null);
62-
Map<String, String> mergedBaggage = new HashMap<>(baggage);
62+
Map<String, String> mergedBaggage = new HashMap<>();
6363
if (baggageToPropagate != null) {
6464
mergedBaggage.putAll(baggageToPropagate.getBaggage());
6565
}
66+
mergedBaggage.putAll(baggage);
6667
BaggageToPropagate merged = new BaggageToPropagate(mergedBaggage);
6768
return context.put(ObservationAwareBaggageThreadLocalAccessor.KEY, merged);
6869
}
@@ -71,10 +72,10 @@ private static Context append(Context context, String key, String value) {
7172
BaggageToPropagate baggageToPropagate = context.getOrDefault(ObservationAwareBaggageThreadLocalAccessor.KEY,
7273
null);
7374
Map<String, String> mergedBaggage = new HashMap<>();
74-
mergedBaggage.put(key, value);
7575
if (baggageToPropagate != null) {
7676
mergedBaggage.putAll(baggageToPropagate.getBaggage());
7777
}
78+
mergedBaggage.put(key, value);
7879
BaggageToPropagate merged = new BaggageToPropagate(mergedBaggage);
7980
return context.put(ObservationAwareBaggageThreadLocalAccessor.KEY, merged);
8081
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Copyright 2025 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
* <p>
7+
* https://www.apache.org/licenses/LICENSE-2.0
8+
* <p>
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*/
14+
package io.micrometer.tracing.contextpropagation.reactor;
15+
16+
import io.micrometer.tracing.contextpropagation.BaggageToPropagate;
17+
import io.micrometer.tracing.contextpropagation.ObservationAwareBaggageThreadLocalAccessor;
18+
import org.junit.jupiter.api.Test;
19+
import reactor.util.context.Context;
20+
21+
import java.util.HashMap;
22+
import java.util.Map;
23+
24+
import static org.assertj.core.api.BDDAssertions.then;
25+
26+
class ReactorBaggageTests {
27+
28+
@Test
29+
void should_append_single_baggage_entry_to_context() {
30+
Context updatedContext = ReactorBaggage.append("foo", "bar").apply(Context.empty());
31+
32+
BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
33+
then(baggage).isNotNull();
34+
then(baggage.getBaggage()).hasSize(1);
35+
then(baggage.getBaggage()).containsEntry("foo", "bar");
36+
}
37+
38+
@Test
39+
void should_append_multiple_baggage_entries_to_context() {
40+
Map<String, String> baggageEntries = new HashMap<>();
41+
baggageEntries.put("foo", "bar");
42+
baggageEntries.put("baz", "bar2");
43+
44+
Context updatedContext = ReactorBaggage.append(baggageEntries).apply(Context.empty());
45+
46+
BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
47+
then(baggage).isNotNull();
48+
then(baggage.getBaggage()).hasSize(2);
49+
then(baggage.getBaggage()).containsExactlyInAnyOrderEntriesOf(baggageEntries);
50+
}
51+
52+
@Test
53+
void should_merge_existing_baggage_with_new_entries() {
54+
Map<String, String> initialBaggage = new HashMap<>();
55+
initialBaggage.put("foo", "bar");
56+
initialBaggage.put("baz", "bar2");
57+
Context context = Context.of(ObservationAwareBaggageThreadLocalAccessor.KEY,
58+
new BaggageToPropagate(initialBaggage));
59+
60+
Map<String, String> newBaggage = new HashMap<>();
61+
newBaggage.put("foo", "bar1");
62+
63+
Context updatedContext = ReactorBaggage.append(newBaggage).apply(context);
64+
65+
BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
66+
then(baggage).isNotNull();
67+
then(baggage.getBaggage()).hasSize(2);
68+
then(baggage.getBaggage()).containsEntry("foo", "bar1").containsEntry("baz", "bar2");
69+
}
70+
71+
@Test
72+
void should_merge_existing_baggage_with_single_entry() {
73+
Map<String, String> initialBaggage = new HashMap<>();
74+
initialBaggage.put("foo", "bar");
75+
initialBaggage.put("baz", "bar2");
76+
Context context = Context.of(ObservationAwareBaggageThreadLocalAccessor.KEY,
77+
new BaggageToPropagate(initialBaggage));
78+
79+
Context updatedContext = ReactorBaggage.append("foo", "bar1").apply(context);
80+
81+
BaggageToPropagate baggage = updatedContext.get(ObservationAwareBaggageThreadLocalAccessor.KEY);
82+
then(baggage).isNotNull();
83+
then(baggage.getBaggage()).hasSize(2);
84+
then(baggage.getBaggage()).containsEntry("foo", "bar1").containsEntry("baz", "bar2");
85+
}
86+
87+
}

0 commit comments

Comments
 (0)