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

Make active span be ignored by default; used as parent span if configured #110

Merged

Conversation

rnorth
Copy link
Contributor

@rnorth rnorth commented Oct 17, 2018

The ensures that the damage caused by accidental thread local leakage between requests is minimized.

Fixes #109

@Override
protected boolean shouldUseParentSpan() {
return true;
}
Copy link
Contributor Author

@rnorth rnorth Oct 17, 2018

Choose a reason for hiding this comment

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

Each of the existing IT classes is basically duplicated for the additional scenario, with the shouldUseParentSpan() method controlling:

  • whether or not the new opt-in parameter is set, and
  • what the assertion looks for.

protected abstract boolean shouldUseParentSpan();

@Override
protected void initTracing(ServletContextHandler 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.

This method is copied from the AbstractServerDefaultConfigurationTest, but with some logic to control whether or not we want to use the new withJoinExistingActiveSpan method in the builder.

* Default is false.
* @return builder
*/
public Builder withJoinExistingActiveSpan(boolean joinExistingActiveSpan) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Are you happy with the naming of this? I couldn't think of much better, but perhaps it could be improved.

@@ -64,6 +67,7 @@ public void filter(ContainerRequestContext requestContext) {
if (tracer != null) {

Tracer.SpanBuilder spanBuilder = tracer.buildSpan(operationNameProvider.operationName(requestContext))
.ignoreActiveSpan()
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This could probably always have been in the code as far as I can tell; the thing that actually seems to be more influential is:

if (parentSpanContext != null) {
  spanBuilder.asChildOf(parentSpanContext);
}

below.

@rnorth rnorth force-pushed the configurable-active-span-joining branch from 1a07cee to 40ab1aa Compare October 22, 2018 08:34
…ured.

The ensures that the damage caused by accidental thread local leakage between requests is minimized
Fixes opentracing-contrib#109
@rnorth rnorth force-pushed the configurable-active-span-joining branch from 40ab1aa to 6559b80 Compare October 22, 2018 08:36
@rnorth
Copy link
Contributor Author

rnorth commented Oct 22, 2018

Hmm, I pushed some changes to my branch but they're not appearing. Will try closing and reopening...

@rnorth rnorth closed this Oct 22, 2018
@rnorth
Copy link
Contributor Author

rnorth commented Oct 22, 2018

Hmm, I pushed some changes to my branch but they're not appearing in the PR. Will try to close and reopen...

@rnorth rnorth reopened this Oct 22, 2018
@rnorth
Copy link
Contributor Author

rnorth commented Oct 22, 2018

Ah never mind - GitHub is having issues at the moment: https://status.github.com/messages

@rnorth
Copy link
Contributor Author

rnorth commented Oct 23, 2018

It seems that yesterday's GitHub consistency issues are now resolved and this is showing the correct content. Ready for review!

@sjoerdtalsma
Copy link
Contributor

What is the idea of this change, that active span are ignored by default ?
Other than the active span threadlocal leakage issue do you have a compelling use-case for this?

I think the active span leakage could better be solved by something such as opentracing/opentracing-java#313

@rnorth
Copy link
Contributor Author

rnorth commented Oct 30, 2018

Hi @sjoerdtalsma
Thanks for pointing out opentracing/opentracing-java#313 and linking it together with the issues.

I would wholeheartedly say that opentracing/opentracing-java#313 ought to happen regardless - being able to clear the state at the end of handling a request is important and feels like a missing part of the API (it was the first thing I looked for when tackling this problem TBH)

Regarding this PR, though, I'd say that perhaps we should do both - tackle the problem at both ends. Clean up after ourselves, but don't assume the previous occupant of the thread did the same.

Turning things around, I don't really see a compelling use case to re-use active spans by default. There is the servlet filter + jersey filter scenario, but I'd argue that it's pretty niche, and the current model of not even being able to opt-out feels wrong.

If it helps, I'd be totally fine with modifying this PR to keep the current default behaviour unchanged and allow opt-out. @pavolloffay suggested changing the default but maybe he doesn't have particularly strong views on this.

Copy link
Collaborator

@pavolloffay pavolloffay left a comment

Choose a reason for hiding this comment

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

Thanks, It looks good just a couple of minor comments.

@@ -197,6 +201,18 @@ public Builder withSkipPattern(String skipPattern) {
return this;
}

/**
* @param joinExistingActiveSpan If true, any active span on the current scope will be used as the parent span.
Copy link
Collaborator

Choose a reason for hiding this comment

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

on the current thread. This can be used when combining with servlet instrumentation.

@@ -197,6 +201,18 @@ public Builder withSkipPattern(String skipPattern) {
return this;
}

/**
* @param joinExistingActiveSpan If true, any active span on the current scope will be used as the parent span.
* If false, and a parent span can be extracted from the HTTP request, that span
Copy link
Collaborator

Choose a reason for hiding this comment

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

If false parent span will be extracted from HTTP headers.

if (shouldUseParentSpan()) {
Assert.assertEquals(preceding.context().spanId(), original.parentId());
} else {
Assert.assertNotEquals(preceding.context().spanId(), original.parentId());
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can equal to 0 which means no parent

@pavolloffay
Copy link
Collaborator

I think the active span leakage could better be solved by something such as opentracing/opentracing-java#313

The problem is that we don't if the previous layer forgot to clear the context or it's indicating to join the trace.

@sjoerdtalsma
Copy link
Contributor

I think the active span leakage could better be solved by something such as opentracing/opentracing-java#313

The problem is that we don't if the previous layer forgot to clear the context or it's indicating to join the trace.

No problem if you clear after each request I think. Thread is returned to the threadpool anyway, so must be clear of lingering Spans.

@pavolloffay
Copy link
Collaborator

I think it might be a problem, the previous layer which set the context is responsible of clearing it.

@rnorth
Copy link
Contributor Author

rnorth commented Oct 31, 2018

No problem if you clear after each request I think. Thread is returned to the threadpool anyway, so must be clear of lingering Spans.

I'm afraid I'm wary of that assertion - the thread certainly should be free from dirty threadlocal state, but mistakes could happen.

The current behaviour seems to only be useful in quite niche cases. In the majority of cases it's not just unnecessary, it's potentially dangerous.

I think it might be a problem, the previous layer which set the context is responsible of clearing it.

This is a really good point; when it's available, opentracing/opentracing-java#313 will need to be configurable (opt-out?) so that the JAX-RS filter doesn't nuke the scope that an outer servlet filter is actually responsible for closing.

@pavolloffay pavolloffay merged commit 1228471 into opentracing-contrib:master Oct 31, 2018
@pavolloffay
Copy link
Collaborator

thanks @rnorth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants