Skip to content

Conversation

carterkozak
Copy link
Contributor

@carterkozak carterkozak commented Aug 31, 2021

Before this PR

spans + deferredtracer.

After this PR

Removed unhelpful spans: executors, runnables, and callables wrapped without an operation name no longer produce spans with the generic name DeferredTracer(unnamed operation). It's possible that these provided value in some scenarios, however we strongly recommend providing a relevant name instead. These generic spans tend to be unhelpful debugging when things go wrong.

Some products trace thousands of items, sampled requests can result in a lot of allocation overhead deep-copying span stacks. DetachedSpan only peeks at one span.

==COMMIT_MSG==
Allow detachment from thread state without creating a new span.
==COMMIT_MSG==

Possible downsides?

This is confusing. The original span may close/end before the detached element, however that's already the case using DeferredTracer. This change allows us to leverage the nicer, more efficient DetachedSpan API as a replacement for DeferredTracer.

I could not update DeferredTracer because it has been used to transfer an entire span stack to another thread, which I do not intend to support with this model.

@changelog-app
Copy link

changelog-app bot commented Aug 31, 2021

Generate changelog in changelog/@unreleased

Type

  • Feature
  • Improvement
  • Fix
  • Break
  • Deprecation
  • Manual task
  • Migration

Description

Allow detachment from thread state without creating a new span.

Check the box to generate changelog(s)

  • Generate changelog entry

return sampled
? new SampledDetachedSpan(operation, type, traceId, requestId, parentSpan)
: new UnsampledDetachedSpan(traceId, requestId, parentSpan);
: new UnsampledDetachedSpan(traceId, requestId, Optional.empty());
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 is a simplification for readability: parentSpan is always empty here, but it wasn't obvious reading the code.

setTrace(newTrace);
// Do not complete the synthetic root span, it simply prevents nested spans from removing trace state, and
// allows
return maybeCurrentTrace == null ? REMOVE_TRACE : () -> Tracer.setTrace(maybeCurrentTrace);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These methods are duplicated with the sampled implementation. Once I'm happier with the API I'll revisit this.

Tracer.setSampler(AlwaysSampler.INSTANCE);
// Initialize a new trace for each test
Tracer.setTrace(Trace.of(true, "defaultTraceId", Optional.empty()));
Tracer.initTraceWithSpan(Observability.SAMPLE, "defaultTraceId", "rootOperation", SpanType.LOCAL);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixes the test, a sampled trace with zero spans is an illegal state that doesn't exist in the wild.

@carterkozak carterkozak changed the title [idea/wip] Detach without creating a new span Support detaching from thread state without creating a new span Sep 1, 2021
@carterkozak carterkozak marked this pull request as ready for review September 1, 2021 19:25
@policy-bot policy-bot bot requested a review from CRogers September 1, 2021 19:25
Comment on lines +364 to +367
Trace originalTrace = original;
if (originalTrace != null) {
Tracer.setTrace(originalTrace);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why do we need the originalTrace assignment here?

Copy link
Contributor

Choose a reason for hiding this comment

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

oh I see this was just copied from further down in the diff here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When we apply a trace somewhere else in a deferred way, it's important that it doesn't destroy any existing tracing that's already happening with a potentially (but not necessarily) different traceId.

Basically the tracing state before this is applied should exactly match the state after it's closed.

Copy link
Contributor

@tpetracca tpetracca left a comment

Choose a reason for hiding this comment

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

I'm a fan of removing those wrapped spans with the unhelpful/generic names. Agreed there are aspects of this that are confusing, but that's already the case anyway and this feels strictly better.

@bulldozer-bot bulldozer-bot bot merged commit 6c30881 into develop Sep 7, 2021
@bulldozer-bot bulldozer-bot bot deleted the ckozak/detach_without_span branch September 7, 2021 16:37
@svc-autorelease
Copy link
Collaborator

Released 6.2.0

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

Successfully merging this pull request may close these issues.

4 participants