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

create propagate span event and publish it in to_digest #4193

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
13 changes: 11 additions & 2 deletions lib/datadog/tracing/trace_operation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ def to_digest
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 sampling_priority
events.propagate.publish(@active_span, self)
Copy link
Contributor

Choose a reason for hiding this comment

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

It dawned on me, I don't think to_digest is just for propagation, it may also be used for log correlations. My concern is that if we print a log message with correlation behavior on, say at the beginning of a web request, then the sampling decision will actually be made very early (based only off the root span in the worst case.)

Should check this isn't the case...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So I couldn't find it being used in log correlation, however we do use to_digest for opentelemetry here: https://github.com/DataDog/dd-trace-rb/blob/master/lib/datadog/opentelemetry/trace.rb#L20-L33 Which seems like it could be an issue if I'm understanding the code correctly because this basically runs to_digest every time we create an opentelemetry context? Let me know if I'm off here.


TraceDigest.new(
span_id: span_id,
Expand Down Expand Up @@ -380,12 +380,14 @@ class Events
attr_reader \
:span_before_start,
:span_finished,
:trace_finished
:trace_finished,
:propagate

def initialize
@span_before_start = SpanBeforeStart.new
@span_finished = SpanFinished.new
@trace_finished = TraceFinished.new
@propagate = Propagate.new
end

# Triggered before a span starts.
Expand All @@ -402,6 +404,13 @@ def initialize
end
end

# Triggered when trace is being propagated between applications or contexts
class Propagate < Tracing::Event
ZStriker19 marked this conversation as resolved.
Show resolved Hide resolved
def initialize
super(:propagate)
end
end

# Triggered when the trace finishes, regardless of error.
class TraceFinished < Tracing::Event
def initialize
Expand Down
6 changes: 5 additions & 1 deletion lib/datadog/tracing/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,12 @@ def bind_trace_events!(trace_op)
event_span_op.service ||= @default_service
end

events.propagate.subscribe do |_event_span, event_trace_op|
ZStriker19 marked this conversation as resolved.
Show resolved Hide resolved
sample_trace(event_trace_op) unless event_trace_op.sampling_priority
end

events.span_finished.subscribe do |event_span, event_trace_op|
sample_trace(trace_op) unless trace_op.sampling_priority
ZStriker19 marked this conversation as resolved.
Show resolved Hide resolved
sample_span(event_trace_op, event_span)
flush_trace(event_trace_op)
end
Expand Down Expand Up @@ -498,7 +503,6 @@ def sample_span(trace_op, span)

# Flush finished spans from the trace buffer, send them to writer.
def flush_trace(trace_op)
sample_trace(trace_op) unless trace_op.sampling_priority
begin
trace = @trace_flush.consume!(trace_op)
write(trace) if trace && !trace.empty?
Expand Down
19 changes: 2 additions & 17 deletions spec/datadog/tracing/trace_operation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,6 @@
end

context 'given' do
context ':trace_operation_samples' do
let(:tracer) { instance_double(Datadog::Tracing::Tracer) }
let(:trace_op) { described_class.new(tracer: tracer) }

describe '#to_digest' do
before do
allow(tracer).to receive(:sample_trace)
end

it 'calls tracer.sample_trace' do
expect(tracer).to receive(:sample_trace).with(trace_op)
trace_op.to_digest
end
end
end

context ':agent_sample_rate' do
subject(:options) { { agent_sample_rate: agent_sample_rate } }
let(:agent_sample_rate) { 0.5 }
Expand Down Expand Up @@ -2326,7 +2310,8 @@ def span
[
:span_before_start,
:span_finished,
:trace_finished
:trace_finished,
:propagate,
].each do |event|
expect(new_events.send(event).subscriptions).to eq(old_events.send(event).subscriptions)
end
Expand Down
Loading