Skip to content

Commit 8b63aa7

Browse files
committed
Merge branch '1.4.x' into 1.5.x
2 parents 99727bf + 9627271 commit 8b63aa7

File tree

2 files changed

+74
-24
lines changed

2 files changed

+74
-24
lines changed

micrometer-tracing-bridges/micrometer-tracing-bridge-otel/src/main/java/io/micrometer/tracing/otel/propagation/BaggageTextMapPropagator.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package io.micrometer.tracing.otel.propagation;
1717

18+
import io.micrometer.common.lang.Nullable;
1819
import io.micrometer.common.util.internal.logging.InternalLogger;
1920
import io.micrometer.common.util.internal.logging.InternalLoggerFactory;
2021
import io.micrometer.tracing.BaggageManager;
@@ -26,9 +27,10 @@
2627
import io.opentelemetry.context.propagation.TextMapPropagator;
2728
import io.opentelemetry.context.propagation.TextMapSetter;
2829

29-
import java.util.AbstractMap;
30+
import java.util.AbstractMap.SimpleEntry;
3031
import java.util.List;
3132
import java.util.Map;
33+
import java.util.Map.Entry;
3234
import java.util.stream.Collectors;
3335

3436
/**
@@ -84,22 +86,27 @@ private <C> List<Map.Entry<String, String>> applicableBaggageEntries(C c) {
8486
}
8587

8688
@Override
87-
public <C> Context extract(Context context, C c, TextMapGetter<C> getter) {
88-
Map<String, String> baggageEntries = this.remoteFields.stream()
89-
.map(s -> new AbstractMap.SimpleEntry<>(s, getter.get(c, s)))
89+
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
90+
BaggageBuilder newBaggage = Baggage.fromContext(context).toBuilder();
91+
92+
Map<String, String> carierEntries = extractFromCarrier(carrier, getter);
93+
carierEntries
94+
.forEach((key, value) -> newBaggage.put(key, value, BaggageEntryMetadata.create(PROPAGATION_UNLIMITED)));
95+
96+
return context.with(newBaggage.build());
97+
}
98+
99+
private <C> Map<String, String> extractFromCarrier(@Nullable C carrier, TextMapGetter<C> getter) {
100+
Map<String, String> carierEntries = this.remoteFields.stream()
101+
.map(s -> new SimpleEntry<>(s, getter.get(carrier, s)))
90102
.filter(e -> e.getValue() != null)
91-
.collect(Collectors.toMap((e) -> e.getKey(), (e) -> e.getValue()));
92-
BaggageBuilder builder = Baggage.current().toBuilder();
93-
baggageEntries
94-
.forEach((key, value) -> builder.put(key, value, BaggageEntryMetadata.create(PROPAGATION_UNLIMITED)));
95-
Baggage.fromContext(context)
96-
.forEach((s, baggageEntry) -> builder.put(s, baggageEntry.getValue(), baggageEntry.getMetadata()));
97-
Baggage baggage = builder.build();
98-
Context withBaggage = context.with(baggage);
103+
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
104+
99105
if (log.isDebugEnabled()) {
100-
log.debug("Will propagate new baggage context for entries " + baggageEntries);
106+
log.debug("Will propagate new baggage context for entries " + carierEntries);
101107
}
102-
return withBaggage;
108+
109+
return carierEntries;
103110
}
104111

105112
}

micrometer-tracing-bridges/micrometer-tracing-bridge-otel/src/test/java/io/micrometer/tracing/otel/propagation/BaggageTextMapPropagatorTests.java

Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,70 @@
2222
import io.opentelemetry.context.Context;
2323
import io.opentelemetry.context.propagation.TextMapGetter;
2424
import org.assertj.core.api.BDDAssertions;
25+
import org.junit.jupiter.api.BeforeEach;
2526
import org.junit.jupiter.api.Test;
2627

2728
import java.util.*;
2829

30+
import static java.util.Collections.emptyList;
31+
2932
class BaggageTextMapPropagatorTests {
3033

34+
private static final List<String> REMOTE_FIELDS = Arrays.asList("foo", "foo2");
35+
36+
private BaggageTextMapPropagator baggageTextMapPropagator;
37+
38+
private TextMapGetter<Map<String, String>> textMapGetter;
39+
40+
@BeforeEach
41+
void setup() {
42+
baggageTextMapPropagator = new BaggageTextMapPropagator(REMOTE_FIELDS,
43+
new OtelBaggageManager(new OtelCurrentTraceContext(), REMOTE_FIELDS, emptyList()));
44+
textMapGetter = textMapGetter(REMOTE_FIELDS);
45+
}
46+
3147
@Test
3248
void should_append_baggage_to_existing_one() {
33-
Baggage baggage = Baggage.current().toBuilder().put("foo", "bar").put("baz", "baz2").build();
34-
Context parent = Context.current().with(baggage);
35-
List<String> remoteFields = Arrays.asList("foo", "foo2");
36-
BaggageTextMapPropagator baggageTextMapPropagator = new BaggageTextMapPropagator(remoteFields,
37-
new OtelBaggageManager(new OtelCurrentTraceContext(), remoteFields, Collections.emptyList()));
49+
Baggage baggage = Baggage.empty().toBuilder().put("foo", "undesired").put("lorem", "ipsum").build();
50+
Context parent = Context.root().with(baggage);
51+
3852
Map<String, String> carrier = new HashMap<>();
3953
carrier.put("foo", "bar");
4054
carrier.put("foo2", "bar2");
4155

42-
Context extracted = baggageTextMapPropagator.extract(parent, carrier, new TextMapGetter<Map<String, String>>() {
56+
Context extracted = baggageTextMapPropagator.extract(parent, carrier, textMapGetter);
57+
58+
Map<String, BaggageEntry> extractedBaggage = Baggage.fromContext(extracted).asMap();
59+
BDDAssertions.then(extractedBaggage).containsOnlyKeys("foo", "foo2", "lorem");
60+
61+
BDDAssertions.then(extractedBaggage.get("foo").getValue()).isEqualTo("bar");
62+
BDDAssertions.then(extractedBaggage.get("foo2").getValue()).isEqualTo("bar2");
63+
BDDAssertions.then(extractedBaggage.get("lorem").getValue()).isEqualTo("ipsum");
64+
}
65+
66+
@Test
67+
void should_not_mix_baggage_with_current_baggage() {
68+
Baggage currentBaggage = Baggage.empty()
69+
.toBuilder()
70+
.put("current-key", "undesired")
71+
.put("foo", "undesired")
72+
.build();
73+
Context currentContext = Context.root().with(currentBaggage);
74+
75+
Context extracted = currentContext.wrapSupplier(() -> {
76+
Map<String, String> carrier = new HashMap<>();
77+
carrier.put("foo", "bar");
78+
79+
return baggageTextMapPropagator.extract(Context.root(), carrier, textMapGetter(REMOTE_FIELDS));
80+
}).get();
81+
82+
Map<String, BaggageEntry> extractedBaggage = Baggage.fromContext(extracted).asMap();
83+
BDDAssertions.then(extractedBaggage).doesNotContainKey("current-key");
84+
BDDAssertions.then(extractedBaggage.get("foo").getValue()).isEqualTo("bar");
85+
}
86+
87+
private TextMapGetter<Map<String, String>> textMapGetter(final List<String> remoteFields) {
88+
return new TextMapGetter<Map<String, String>>() {
4389
@Override
4490
public Iterable<String> keys(Map<String, String> carrier) {
4591
return remoteFields;
@@ -49,10 +95,7 @@ public Iterable<String> keys(Map<String, String> carrier) {
4995
public String get(Map<String, String> carrier, String key) {
5096
return carrier.get(key);
5197
}
52-
});
53-
54-
Map<String, BaggageEntry> newBaggage = Baggage.fromContext(extracted).asMap();
55-
BDDAssertions.then(newBaggage).containsOnlyKeys("foo", "foo2", "baz");
98+
};
5699
}
57100

58101
}

0 commit comments

Comments
 (0)