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

First-class support for trace propagation across threads #1202

Closed
marcotc opened this issue Oct 7, 2020 · 1 comment
Closed

First-class support for trace propagation across threads #1202

marcotc opened this issue Oct 7, 2020 · 1 comment
Assignees
Labels
core Involves Datadog core libraries feature-request A request for a new feature or change to an existing one
Milestone

Comments

@marcotc
Copy link
Member

marcotc commented Oct 7, 2020

We currently support traces that span across multiple threads using the follow pattern:

parent_span = Datadog.tracer.active_span
Thread.new do 
  Datadog.tracer.trace("my.threaded.work") do |threaded_span|
    threaded_span.parent = parent_span
    # Threaded work goes here
  end
end

Despite parent_span and threaded_span having distinct thread-local contexts, this pattern works because we are manually setting the parent of the first span in the new thread with parent_span. The two distinct contexts will have their own spans and the backend will eventually stitch them back together into one large trace.

We should create an intentionally designed interface to provide threaded tracing. The current "support" is completely manual, and requires understanding of the internals of the tracer.

Ideally, transparent support would be achieved where all threads created from an existing tracing context get associated with the parent trace, but this might not be feasible. Consider the scenario where we spawn long-running threads:

def start_workers
   @workers.each(&:new_thread)
end

If a tracing context is active when start_workers is called, all work performed inside the worker threads will be associated with one very large trace if we have automatic context association.

A selective way to enable automatic propagation during the execution of a block could work to address this issue:

Datadog.tracer.active_span.thread_propagation = true

# or

Datadog.tracer.trace(..., thread_propagation: true) do # could also be configured to default to `true` instead
# ...
end

These are just suggestions on a possible approach.

@marcotc marcotc added core Involves Datadog core libraries feature-request A request for a new feature or change to an existing one labels Oct 7, 2020
@marcotc marcotc added this to the 1.0.0 milestone May 4, 2022
@marcotc
Copy link
Member Author

marcotc commented May 4, 2022

The continue_from argument added to Datadog::Tracing::Tracer#trace as well as the new method Datadog::Tracing::Tracer#continue_trace! allow for full support of async execution now.

These features were released in v1.0.0.

@marcotc marcotc closed this as completed May 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core Involves Datadog core libraries feature-request A request for a new feature or change to an existing one
Projects
None yet
Development

No branches or pull requests

1 participant