-
Notifications
You must be signed in to change notification settings - Fork 394
Tracing.continue_from! with a block maintains active trace until the block ends #4941
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
Open
marcotc
wants to merge
1
commit into
master
Choose a base branch
from
fix-trace-auto-finish
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -149,10 +149,10 @@ def trace( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
context = call_context | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
active_trace = context.active_trace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
trace = if continue_from || active_trace.nil? | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
start_trace(continue_from: continue_from) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
active_trace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
start_trace(continue_from: continue_from) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
active_trace | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was this rubocop autocorrected? If yes this branch needs master merged into it I think. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
rescue => e | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger.debug { "Failed to trace: #{e}" } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -241,10 +241,21 @@ def active_correlation(key = nil) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
trace.to_correlation | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Setup a new trace to continue from where another | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Setup a new trace execution context to continue from where another | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# trace left off. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# This is useful to continue distributed or async traces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# The first span created in the restored context is a direct child of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# active span from when the {Datadog::Tracing::TraceDigest} was created. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# When no block is given, the trace context is restored in the current thread. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# It remains active until the first span created in this restored context is finished. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# After that, if a new span is created, it start a new, unrelated trace. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Used to continue distributed or async traces. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# When a block is given, the trace context is restored inside the block execution. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# It remains active until the block ends, even when the first span created inside | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# the block finishes. This means that multiple spans can be direct children of the | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# active span from when the {Datadog::Tracing::TraceDigest} was created. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# @param [Datadog::Tracing::TraceDigest] digest continue from the {Datadog::Tracing::TraceDigest}. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# @param [Thread] key Thread to retrieve trace from. Defaults to current thread. For internal use only. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -260,13 +271,32 @@ def continue_trace!(digest, key = nil, &block) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
# Start a new trace from the digest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
context = call_context(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
original_trace = active_trace(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
trace = start_trace(continue_from: digest) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# When we want the trace to be bound to a block, we cannot let | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# it auto finish when the local root span finishes. This would | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# create mutiple traces inside the block. Instead, we'll | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# expliclity finish the trace after the block finishes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto_finish = !block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
trace = start_trace(continue_from: digest, auto_finish: auto_finish) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# If block hasn't been given; we need to manually deactivate | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# this trace. Subscribe to the trace finished event to do this. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
subscribe_trace_deactivation!(context, trace, original_trace) unless block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.activate!(trace, &block) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if block | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# When a block is given, the trace will be active until the block finishes. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.activate!(trace) do | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
yield | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
ensure # We have to flush even when an error occurs | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# On block completion, force the trace to finish and flush its finished spans. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Unfinished spans are lost as the {TraceOperation} has ended. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
trace.finish! | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
flush_trace(trace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Otherwise, the trace will be bound to the current thread after this point | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
context.activate!(trace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+286
to
+299
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be streamlined with a guard-clause
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Sample a span, tagging the trace as appropriate. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -329,15 +359,15 @@ def call_context(key = nil) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
@provider.context(key) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
def build_trace(digest = nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def build_trace(digest, auto_finish) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Resolve hostname if configured | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
hostname = Core::Environment::Socket.hostname if Datadog.configuration.tracing.report_hostname | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
hostname = (hostname && !hostname.empty?) ? hostname : nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
hostname = hostname && !hostname.empty? ? hostname : nil | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
if digest | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
sampling_priority = if propagate_sampling_priority?(upstream_tags: digest.trace_distributed_tags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
digest.trace_sampling_priority | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
digest.trace_sampling_priority | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
TraceOperation.new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
logger: logger, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
hostname: hostname, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -353,7 +383,8 @@ def build_trace(digest = nil) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
trace_state_unknown_fields: digest.trace_state_unknown_fields, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
remote_parent: digest.span_remote, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tracer: self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
baggage: digest.baggage | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
baggage: digest.baggage, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto_finish: auto_finish | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
TraceOperation.new( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -362,13 +393,13 @@ def build_trace(digest = nil) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
profiling_enabled: profiling_enabled, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
apm_tracing_enabled: apm_tracing_enabled, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
remote_parent: false, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tracer: self | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tracer: self, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
auto_finish: auto_finish | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:enable Metrics/MethodLength | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:disable Metrics/MethodLength | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def bind_trace_events!(trace_op) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
events = trace_op.send(:events) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -387,13 +418,12 @@ def bind_trace_events!(trace_op) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
flush_trace(event_trace_op) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:enable Metrics/MethodLength | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Creates a new TraceOperation, with events bounds to this Tracer instance. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# @return [TraceOperation] | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def start_trace(continue_from: nil) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def start_trace(continue_from: nil, auto_finish: true) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Build a new trace using digest if provided. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
trace = build_trace(continue_from) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
trace = build_trace(continue_from, auto_finish) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Bind trace events: sample trace, set default service, flush spans. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
bind_trace_events!(trace) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -402,7 +432,6 @@ def start_trace(continue_from: nil) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:disable Lint/UnderscorePrefixedVariableName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:disable Metrics/MethodLength | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def start_span( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
name, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
continue_from: nil, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -454,18 +483,17 @@ def start_span( | |||||||||||||||||||||||||||||||||||||||||||||||||||||
span | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:enable Lint/UnderscorePrefixedVariableName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:enable Metrics/MethodLength | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
# rubocop:enable Lint/UnderscorePrefixedVariableName | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
def resolve_tags(tags, service) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
merged_tags = if @tags.any? && tags | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Combine default tags with provided tags, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# preferring provided tags. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
@tags.merge(tags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Use provided tags or default tags if none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tags || @tags.dup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Combine default tags with provided tags, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# preferring provided tags. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
@tags.merge(tags) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Use provided tags or default tags if none. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
tags || @tags.dup | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
end | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
# Remove version tag if service is not the default service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
if merged_tags.key?(Core::Environment::Ext::TAG_VERSION) && service && service != @default_service | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
merged_tags.delete(Core::Environment::Ext::TAG_VERSION) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.