Skip to content

Commit 7fe57cd

Browse files
committed
use onEnding to add baggage keys that are added while the span is active
1 parent 6d3a142 commit 7fe57cd

File tree

3 files changed

+25
-18
lines changed

3 files changed

+25
-18
lines changed

baggage-processor/src/main/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessor.java

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import io.opentelemetry.context.Context;
1010
import io.opentelemetry.sdk.trace.ReadWriteSpan;
1111
import io.opentelemetry.sdk.trace.ReadableSpan;
12-
import io.opentelemetry.sdk.trace.SpanProcessor;
12+
import io.opentelemetry.sdk.trace.internal.ExtendedSpanProcessor;
1313
import java.util.function.Predicate;
1414

1515
/**
1616
* This span processor copies attributes stored in {@link Baggage} into each newly created {@link
1717
* io.opentelemetry.api.trace.Span}.
1818
*/
19-
public class BaggageSpanProcessor implements SpanProcessor {
19+
public class BaggageSpanProcessor implements ExtendedSpanProcessor {
2020
private final Predicate<String> baggageKeyPredicate;
2121

2222
/** use {@link #allowAllBaggageKeys()} instead */
@@ -39,19 +39,11 @@ public static BaggageSpanProcessor allowAllBaggageKeys() {
3939
}
4040

4141
@Override
42-
public void onStart(Context parentContext, ReadWriteSpan span) {
43-
Baggage.fromContext(parentContext)
44-
.forEach(
45-
(s, baggageEntry) -> {
46-
if (baggageKeyPredicate.test(s)) {
47-
span.setAttribute(s, baggageEntry.getValue());
48-
}
49-
});
50-
}
42+
public void onStart(Context parentContext, ReadWriteSpan span) {}
5143

5244
@Override
5345
public boolean isStartRequired() {
54-
return true;
46+
return false;
5547
}
5648

5749
@Override
@@ -61,4 +53,20 @@ public void onEnd(ReadableSpan span) {}
6153
public boolean isEndRequired() {
6254
return false;
6355
}
56+
57+
@Override
58+
public void onEnding(ReadWriteSpan span) {
59+
Baggage.current()
60+
.forEach(
61+
(s, baggageEntry) -> {
62+
if (baggageKeyPredicate.test(s)) {
63+
span.setAttribute(s, baggageEntry.getValue());
64+
}
65+
});
66+
}
67+
68+
@Override
69+
public boolean isOnEndingRequired() {
70+
return true;
71+
}
6472
}

baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageProcessorCustomizerTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSp
160160
try (BaggageSpanProcessor processor =
161161
BaggageProcessorCustomizer.createBaggageSpanProcessor(Collections.singletonList("*"))) {
162162
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
163-
processor.onStart(Context.current(), span);
163+
processor.onEnding(span);
164164
verify(span).setAttribute("key", "value");
165165
}
166166
}
@@ -177,7 +177,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
177177
.put("other", "value")
178178
.build()
179179
.makeCurrent()) {
180-
processor.onStart(Context.current(), span);
180+
processor.onEnding(span);
181181
verify(span).setAttribute("key", "value");
182182
verify(span, Mockito.never()).setAttribute("other", "value");
183183
}

baggage-processor/src/test/java/io/opentelemetry/contrib/baggage/processor/BaggageSpanProcessorTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
package io.opentelemetry.contrib.baggage.processor;
77

88
import io.opentelemetry.api.baggage.Baggage;
9-
import io.opentelemetry.context.Context;
109
import io.opentelemetry.context.Scope;
1110
import io.opentelemetry.sdk.trace.ReadWriteSpan;
1211
import java.util.regex.Pattern;
@@ -23,7 +22,7 @@ public class BaggageSpanProcessorTest {
2322
public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) {
2423
try (BaggageSpanProcessor processor = BaggageSpanProcessor.allowAllBaggageKeys()) {
2524
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
26-
processor.onStart(Context.current(), span);
25+
processor.onEnding(span);
2726
Mockito.verify(span).setAttribute("key", "value");
2827
}
2928
}
@@ -39,7 +38,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
3938
.put("other", "value")
4039
.build()
4140
.makeCurrent()) {
42-
processor.onStart(Context.current(), span);
41+
processor.onEnding(span);
4342
Mockito.verify(span).setAttribute("key", "value");
4443
Mockito.verify(span, Mockito.never()).setAttribute("other", "value");
4544
}
@@ -58,7 +57,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
5857
.put("other", "value")
5958
.build()
6059
.makeCurrent()) {
61-
processor.onStart(Context.current(), span);
60+
processor.onEnding(span);
6261
Mockito.verify(span).setAttribute("key", "value");
6362
Mockito.verify(span, Mockito.never()).setAttribute("other", "value");
6463
}

0 commit comments

Comments
 (0)