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

Properly clears the scopes #2280

Merged
merged 1 commit into from
Apr 14, 2023
Merged

Properly clears the scopes #2280

merged 1 commit into from
Apr 14, 2023

Conversation

marcingrzejszczak
Copy link
Contributor

without this fix when calling maybeScope(null) or withSpan(null) or newScope(null) we ended up with creating a null scope that would allocate additional resources and not clear things.

with this fix we're clearing all the scopes and removing thread locals

final brave.propagation.CurrentTraceContext delegate;

public BraveCurrentTraceContext(brave.propagation.CurrentTraceContext delegate) {
this.delegate = delegate;
}

@Override
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Override isn't needed anymore?

/**
* Brave implementation of a {@link CurrentTraceContext}.
*
* @author Marcin Grzejszczak
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not we have this, since it is a public class?

return new BraveTraceContext(context);
CurrentTraceContext.Scope previous = this.scopes.get();
CurrentTraceContext.Scope current = new BraveScope(this.delegate.newScope(BraveTraceContext.toBrave(context)));
return new RevertingScope(this, previous, current);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If RevertingScope deals with the scopes ThreadLocal, would not it be better to let it get the previous one on it's own instead of passing it? That way all logic that touched scopes would be in the same place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate? I don't think i follow

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually now i wonder if we shouldn't do it on the level of sleuth api rather than Braves. The same way we do it with micrometer tracing...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant moving this:

CurrentTraceContext.Scope previous = this.scopes.get();

Into the ctor of RevertingScope but maybe we can also move current:

CurrentTraceContext.Scope current = new BraveScope(this.delegate.newScope(BraveTraceContext.toBrave(context)));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't move all of that into reverting scpoe cause there's one reverting scope but two delegates (maybeScope, newScope). I can improve this constructor however.

We also can't move the code to sleuth's api cause there are no implementations there - just interfaces. It has to stay here & in OTel.

without this fix when calling `maybeScope(null)` or `withSpan(null)` or `newScope(null)` we ended up with creating a null scope that would allocate additional resources and not clear things.

with this fix we're clearing all the scopes and removing thread locals
@marcingrzejszczak marcingrzejszczak merged commit 3d19830 into 3.1.x Apr 14, 2023
@marcingrzejszczak marcingrzejszczak deleted the clearing_scopes branch April 14, 2023 11:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants