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

feat: lazy sampling #3956

Draft
wants to merge 14 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/datadog/tracing/distributed/propagation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def initialize(
# DEV-2.0: if needed.
# DEV-2.0: Ideally, we'd have a separate stream to report tracer errors and never
# DEV-2.0: touch the active span.
# DEV-3.0: Sample trace here instead of when generating digest.
#
# @param digest [TraceDigest]
# @param data [Hash]
Expand Down
28 changes: 26 additions & 2 deletions lib/datadog/tracing/trace_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative '../core/environment/identity'
require_relative '../core/utils'

require_relative 'tracer'
require_relative 'event'
require_relative 'metadata/tagging'
require_relative 'sampling/ext'
Expand Down Expand Up @@ -75,7 +75,9 @@ def initialize(
metrics: nil,
trace_state: nil,
trace_state_unknown_fields: nil,
remote_parent: false
remote_parent: false,
tracer: nil

)
# Attributes
@id = id || Tracing::Utils::TraceId.next_id
Expand All @@ -98,6 +100,7 @@ def initialize(
@profiling_enabled = profiling_enabled
@trace_state = trace_state
@trace_state_unknown_fields = trace_state_unknown_fields
@tracer = tracer

# Generic tags
set_tags(tags) if tags
Expand Down Expand Up @@ -161,6 +164,23 @@ def resource
@resource || (root_span && root_span.resource)
end

# When retrieving tags or metrics we need to include root span tags for sampling purposes
def get_tag(key)
super || (root_span && root_span.get_tag(key))
end

def get_metric(key)
super || (root_span && root_span.get_metric(key))
end

def tags
all_tags = {}
all_tags.merge(root_span.tags) if root_span
all_tags.merge(@tags)
all_tags.merge(@metrics)
all_tags
end

# Returns true if the resource has been explicitly set
#
# @return [Boolean]
Expand Down Expand Up @@ -284,10 +304,14 @@ def flush!
# Returns a set of trace headers used for continuing traces.
# Used for propagation across execution contexts.
# Data should reflect the active state of the trace.
# DEV-3.0: Sampling is a side effect of generating the digest.
# We should move the sample call to inject and right before moving to new contexts(threads, forking etc.)
def to_digest
# Resolve current span ID
span_id = @active_span && @active_span.id
span_id ||= @parent_span_id unless finished?
# sample the trace_operation with the tracer
tracer&.sample_trace(self) unless priority_sampled?

TraceDigest.new(
span_id: span_id,
Expand Down
9 changes: 7 additions & 2 deletions lib/datadog/tracing/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,14 @@ def build_trace(digest = nil)
trace_state: digest.trace_state,
trace_state_unknown_fields: digest.trace_state_unknown_fields,
remote_parent: digest.span_remote,
tracer: self
)
else
TraceOperation.new(
hostname: hostname,
profiling_enabled: profiling_enabled,
remote_parent: false,
tracer: self
)
end
end
Expand All @@ -347,7 +349,6 @@ def bind_trace_events!(trace_op)
events.span_before_start.subscribe do |event_span_op, event_trace_op|
event_trace_op.service ||= @default_service
event_span_op.service ||= @default_service
sample_trace(event_trace_op) if event_span_op && event_span_op.parent_id == 0
end

events.span_finished.subscribe do |event_span, event_trace_op|
Expand Down Expand Up @@ -494,7 +495,11 @@ def sample_span(trace_op, span)
def flush_trace(trace_op)
begin
trace = @trace_flush.consume!(trace_op)
write(trace) if trace && !trace.empty?
if trace && !trace.empty?
# check if trace is not sampled
sample_trace(trace) unless trace.priority_sampled?
write(trace)
end
rescue StandardError => e
FLUSH_TRACE_LOG_ONLY_ONCE.run do
Datadog.logger.warn { "Failed to flush trace: #{e.class.name} #{e} at #{Array(e.backtrace).first}" }
Expand Down
Loading