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

Slight clean-up of recent baggage and span scope fix #8258

Merged
merged 1 commit into from
Jan 18, 2024
Merged
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 @@ -21,21 +21,16 @@

class OpenTelemetryScope implements Scope {
private final io.opentelemetry.context.Scope delegate;
private final io.opentelemetry.context.Scope baggageScope;
private final AtomicBoolean closed = new AtomicBoolean();

OpenTelemetryScope(io.opentelemetry.context.Scope scope, io.opentelemetry.context.Scope baggageScope) {
OpenTelemetryScope(io.opentelemetry.context.Scope scope) {
delegate = scope;
this.baggageScope = baggageScope;
}

@Override
public void close() {
if (closed.compareAndSet(false, true) && delegate != null) {
delegate.close();
if (baggageScope != null) {
baggageScope.close();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void status(Status status) {

@Override
public SpanContext context() {
return new OpenTelemetrySpanContext(Context.current().with(delegate));
return new OpenTelemetrySpanContext(otelContextWithSpanAndBaggage());
}

@Override
Expand All @@ -93,8 +93,8 @@ public void end(Throwable t) {

@Override
public Scope activate() {
io.opentelemetry.context.Scope baggageScope = baggage.makeCurrent();
return new OpenTelemetryScope(delegate.makeCurrent(), baggageScope);
io.opentelemetry.context.Scope scope = otelContextWithSpanAndBaggage().makeCurrent();
return new OpenTelemetryScope(scope);
}

@Override
Expand All @@ -119,6 +119,12 @@ private static Context getContext() {
.orElseGet(Context::current);
}

private Context otelContextWithSpanAndBaggage() {
// Because the Helidon tracing API links baggage with a span, any OTel context we create for the span
// needs to have the baggage with it.
return getContext().with(delegate).with(baggage);
}

private Attributes toAttributes(Map<String, ?> attributes) {
AttributesBuilder builder = Attributes.builder();
attributes.forEach((key, value) -> {
Expand Down