Skip to content
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

use onEnding to add baggage keys that are added while the span is active #1633

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 @@ -9,14 +9,14 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.SpanProcessor;
import io.opentelemetry.sdk.trace.internal.ExtendedSpanProcessor;
import java.util.function.Predicate;

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

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

@Override
public void onStart(Context parentContext, ReadWriteSpan span) {
Baggage.fromContext(parentContext)
.forEach(
(s, baggageEntry) -> {
if (baggageKeyPredicate.test(s)) {
span.setAttribute(s, baggageEntry.getValue());
}
});
}
public void onStart(Context parentContext, ReadWriteSpan span) {}

@Override
public boolean isStartRequired() {
return true;
return false;
}

@Override
Expand All @@ -61,4 +53,20 @@ public void onEnd(ReadableSpan span) {}
public boolean isEndRequired() {
return false;
}

@Override
public void onEnding(ReadWriteSpan span) {
Baggage.current()
.forEach(
(s, baggageEntry) -> {
if (baggageKeyPredicate.test(s)) {
span.setAttribute(s, baggageEntry.getValue());
}
});
}

@Override
public boolean isOnEndingRequired() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSp
try (BaggageSpanProcessor processor =
BaggageProcessorCustomizer.createBaggageSpanProcessor(Collections.singletonList("*"))) {
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
processor.onStart(Context.current(), span);
processor.onEnding(span);
verify(span).setAttribute("key", "value");
}
}
Expand All @@ -177,7 +177,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
.put("other", "value")
.build()
.makeCurrent()) {
processor.onStart(Context.current(), span);
processor.onEnding(span);
verify(span).setAttribute("key", "value");
verify(span, Mockito.never()).setAttribute("other", "value");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package io.opentelemetry.contrib.baggage.processor;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.sdk.trace.ReadWriteSpan;
import java.util.regex.Pattern;
Expand All @@ -23,7 +22,7 @@ public class BaggageSpanProcessorTest {
public void test_baggageSpanProcessor_adds_attributes_to_spans(@Mock ReadWriteSpan span) {
try (BaggageSpanProcessor processor = BaggageSpanProcessor.allowAllBaggageKeys()) {
try (Scope ignore = Baggage.current().toBuilder().put("key", "value").build().makeCurrent()) {
processor.onStart(Context.current(), span);
processor.onEnding(span);
Mockito.verify(span).setAttribute("key", "value");
}
}
Expand All @@ -39,7 +38,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
.put("other", "value")
.build()
.makeCurrent()) {
processor.onStart(Context.current(), span);
processor.onEnding(span);
Mockito.verify(span).setAttribute("key", "value");
Mockito.verify(span, Mockito.never()).setAttribute("other", "value");
}
Expand All @@ -58,7 +57,7 @@ public void test_baggageSpanProcessor_adds_attributes_to_spans_when_key_filter_m
.put("other", "value")
.build()
.makeCurrent()) {
processor.onStart(Context.current(), span);
processor.onEnding(span);
Mockito.verify(span).setAttribute("key", "value");
Mockito.verify(span, Mockito.never()).setAttribute("other", "value");
}
Expand Down
Loading