Skip to content

Commit 7986f9a

Browse files
committed
switch to propagate! alias for to_digest, add to_digest_without_propagate
1 parent f32301e commit 7986f9a

File tree

14 files changed

+76
-26
lines changed

14 files changed

+76
-26
lines changed

benchmarks/tracing_trace.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ def trace(x, depth)
7878
end
7979
end
8080

81-
def benchmark_to_digest
81+
def benchmark_propagate!
8282
Datadog::Tracing.trace('op.name') do |span, trace|
8383
Benchmark.ips do |x|
8484
x.config(**benchmark_time)
8585

86-
x.report("trace.to_digest") do
87-
trace.to_digest
86+
x.report("trace.propagate!") do
87+
trace.propagate!
8888
end
8989

9090
x.save! "#{File.basename(__FILE__)}-results.json" unless VALIDATE_BENCHMARK_MODE
@@ -108,13 +108,13 @@ def benchmark_log_correlation
108108
end
109109
end
110110

111-
def benchmark_to_digest_continue
111+
def benchmark_propagate_continue
112112
Datadog::Tracing.trace('op.name') do |span, trace|
113113
Benchmark.ips do |x|
114114
x.config(**benchmark_time)
115115

116-
x.report("trace.to_digest - Continue") do
117-
digest = trace.to_digest
116+
x.report("trace.propagate! - Continue") do
117+
digest = trace.propagate!
118118
Datadog::Tracing.continue_trace!(digest)
119119
end
120120

@@ -136,7 +136,7 @@ def benchmark_propagation_datadog
136136
end
137137

138138
Datadog::Tracing.trace('op.name') do |span, trace|
139-
injected_trace_digest = trace.to_digest
139+
injected_trace_digest = trace.propagate!
140140
Benchmark.ips do |x|
141141
x.config(**benchmark_time)
142142

@@ -159,7 +159,7 @@ def benchmark_propagation_trace_context
159159
end
160160

161161
Datadog::Tracing.trace('op.name') do |span, trace|
162-
injected_trace_digest = trace.to_digest
162+
injected_trace_digest = trace.propagate!
163163
Benchmark.ips do |x|
164164
x.config(**benchmark_time)
165165

@@ -190,9 +190,9 @@ def run_benchmark(&block)
190190
TracingTraceBenchmark.new.instance_exec do
191191
run_benchmark { benchmark_no_writer }
192192
run_benchmark { benchmark_no_network }
193-
run_benchmark { benchmark_to_digest }
193+
run_benchmark { benchmark_propagate! }
194194
run_benchmark { benchmark_log_correlation }
195-
run_benchmark { benchmark_to_digest_continue }
195+
run_benchmark { benchmark_propagate_continue }
196196
run_benchmark { benchmark_propagation_datadog }
197197
run_benchmark { benchmark_propagation_trace_context }
198198
end

docs/GettingStarted.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2418,7 +2418,7 @@ On the client:
24182418
```ruby
24192419
Datadog::Tracing.trace('web.call') do |span, trace|
24202420
# Inject trace headers into request headers (`env` must be a Hash)
2421-
Datadog::Tracing::Contrib::HTTP.inject(trace.to_digest, env)
2421+
Datadog::Tracing::Contrib::HTTP.inject(trace.propagate!, env)
24222422
end
24232423
```
24242424

lib/datadog/opentelemetry/trace.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def start_trace_copy(trace, parent_span: nil)
1717
digest = if parent_span
1818
digest_with_parent_span(trace, parent_span)
1919
else
20-
trace.to_digest
20+
trace.to_digest_without_propagate
2121
end
2222

2323
# Create a new TraceOperation, attached to the current Datadog Tracer.
@@ -30,7 +30,8 @@ def start_trace_copy(trace, parent_span: nil)
3030
# This supports the implementation of `OpenTelemetry::Trace.context_with_span`,
3131
# which allows you to specific any span as the arbitrary parent of a new span.
3232
def digest_with_parent_span(trace, parent_span)
33-
digest = trace.to_digest
33+
# sampling_priority added here right upon otel span creation
34+
digest = trace.gen_trace_ext_scope
3435

3536
Tracing::TraceDigest.new(
3637
span_id: parent_span.id,

lib/datadog/tracing/contrib/concurrent_ruby/context_composite_executor_service.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(composited_executor)
1919
# post method runs the task within composited executor - in a different thread. The original arguments are
2020
# captured to be propagated to the composited executor post method
2121
def post(*args, &task)
22-
digest = Tracing.active_trace && Tracing.active_trace.to_digest
22+
digest = Tracing.active_trace && Tracing.active_trace.propagate!
2323
executor = @composited_executor.is_a?(Symbol) ? Concurrent.executor(@composited_executor) : @composited_executor
2424

2525
# Pass the original arguments to the composited executor, which

lib/datadog/tracing/contrib/ethon/multi_patch.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def datadog_multi_span
6262
Ext::SPAN_MULTI_REQUEST,
6363
service: datadog_configuration[:service_name]
6464
)
65-
@datadog_multi_trace_digest = Tracing.active_trace.to_digest
65+
@datadog_multi_trace_digest = Tracing.active_trace.propagate!
6666

6767
@datadog_multi_span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
6868
@datadog_multi_span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_MULTI_REQUEST)

lib/datadog/tracing/contrib/propagation/sql_comment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def self.prepend_comment(sql, span_op, trace_op, mode)
4545
# When tracing is disabled, trace_operation is a dummy object that does not contain data to build traceparent
4646
if config.tracing.enabled
4747
tags[Ext::KEY_TRACEPARENT] =
48-
Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_traceparent, trace_op.to_digest)
48+
Tracing::Distributed::TraceContext.new(fetcher: nil).send(:build_traceparent, trace_op.propagate!)
4949
else
5050
Datadog.logger.warn(
5151
'Sql comment propagation with `full` mode is aborted, because tracing is disabled. '\

lib/datadog/tracing/distributed/propagation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def inject!(digest, data)
5656
return nil
5757
end
5858

59-
digest = digest.to_digest if digest.respond_to?(:to_digest)
59+
digest = digest.propagate! if digest.respond_to?(:propagate!)
6060

6161
if digest.trace_id.nil?
6262
::Datadog.logger.debug('Cannot inject distributed trace data: digest.trace_id is nil.')

lib/datadog/tracing/trace_operation.rb

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,11 +301,42 @@ def flush!
301301
build_trace(spans, !finished)
302302
end
303303

304+
# DEV-3.0: Sampling is a side effect of generating the digest in to_digest. With 3.0 we should remove the side effect
305+
# and push users to use propagate! instead of to_digest. With that we can remove this method.
306+
def to_digest_no_propagation
307+
span_id = @active_span && @active_span.id
308+
span_id ||= @parent_span_id unless finished?
309+
TraceDigest.new(
310+
span_id: span_id,
311+
span_name: (@active_span && @active_span.name),
312+
span_resource: (@active_span && @active_span.resource),
313+
span_service: (@active_span && @active_span.service),
314+
span_type: (@active_span && @active_span.type),
315+
trace_distributed_tags: distributed_tags,
316+
trace_hostname: @hostname,
317+
trace_id: @id,
318+
trace_name: name,
319+
trace_origin: @origin,
320+
trace_process_id: Core::Environment::Identity.pid,
321+
trace_resource: resource,
322+
trace_runtime_id: Core::Environment::Identity.id,
323+
trace_sampling_priority: @sampling_priority,
324+
trace_service: service,
325+
trace_state: @trace_state,
326+
trace_state_unknown_fields: @trace_state_unknown_fields,
327+
span_remote: (@remote_parent && @active_span.nil?),
328+
).freeze
329+
end
330+
331+
def propagate!
332+
to_digest
333+
end
334+
304335
# Returns a set of trace headers used for continuing traces.
305336
# Used for propagation across execution contexts.
306337
# Data should reflect the active state of the trace.
307-
# DEV-3.0: Sampling is a side effect of generating the digest.
308-
# We should move the sample call to inject and right before moving to new contexts(threads, forking etc.)
338+
# DEV-3.0: Sampling is a side effect of generating the digest in to_digest.
339+
# With 3.0 we should remove the side effect and push users to use propagate! instead of to_digest.
309340
def to_digest
310341
# Resolve current span ID
311342
span_id = @active_span && @active_span.id
@@ -523,7 +554,7 @@ def build_trace(spans, partial = false)
523554
end
524555

525556
# Returns tracer tags that will be propagated if this span's context
526-
# is exported through {.to_digest}.
557+
# is exported through {.propagate!}.
527558
# @return [Hash] key value pairs of distributed tags
528559
def distributed_tags
529560
meta.select { |name, _| name.start_with?(Metadata::Ext::Distributed::TAGS_PREFIX) }

sig/datadog/tracing/trace_operation.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ module Datadog
3939
def build_span: (untyped op_name, ?events: untyped?, ?on_error: untyped?, ?resource: untyped?, ?service: untyped?, ?start_time: untyped?, ?tags: untyped?, ?type: untyped?) -> untyped
4040
def flush!: () { (untyped) -> untyped } -> untyped
4141
def to_digest: () -> untyped
42+
def propagate!: () -> untyped
43+
def to_digest_without_propagate: () -> untyped
4244
def fork_clone: () -> untyped
4345

4446
class Events

spec/datadog/tracing/contrib/propagation/sql_comment_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
end
6767
let(:trace_op) do
6868
double(
69-
to_digest: Datadog::Tracing::TraceDigest.new(
69+
propagate!: Datadog::Tracing::TraceDigest.new(
7070
trace_id: 0xC0FFEE,
7171
span_id: 0xBEE,
7272
trace_flags: 0xFE

spec/datadog/tracing/contrib/sql_comment_propagation_examples.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
expect(Datadog::Tracing::Contrib::Propagation::SqlComment).to have_received(:prepend_comment).with(
7070
sql_statement,
7171
a_span_operation_with(service: service_name),
72-
duck_type(:to_digest),
72+
duck_type(:propagate!),
7373
propagation_mode
7474
)
7575
end
@@ -90,7 +90,7 @@
9090
expect(Datadog::Tracing::Contrib::Propagation::SqlComment).to have_received(:prepend_comment).with(
9191
sql_statement,
9292
a_span_operation_with(service: service_name),
93-
duck_type(:to_digest),
93+
duck_type(:propagate!),
9494
propagation_mode
9595
)
9696
end

spec/datadog/tracing/trace_operation_spec.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,22 @@
136136
end
137137

138138
context 'given' do
139+
context ':trace_operation_triggers_trace_propagated_event' do
140+
let(:events) { instance_double('Events') }
141+
let(:trace_propagated) { instance_double('TracePropagated') }
142+
let(:trace_op) { described_class.new(events: events) }
143+
before do
144+
allow(events).to receive(:trace_propagated).and_return(trace_propagated)
145+
allow(trace_propagated).to receive(:publish)
146+
end
147+
describe '#propagate!' do
148+
it 'calls events.trace_propagated.publish with self' do
149+
expect(trace_propagated).to receive(:publish).with(trace_op)
150+
trace_op.to_digest
151+
end
152+
end
153+
end
154+
139155
context ':agent_sample_rate' do
140156
subject(:options) { { agent_sample_rate: agent_sample_rate } }
141157
let(:agent_sample_rate) { 0.5 }

spec/datadog/tracing/tracer_integration_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def lang_tag(span)
8484

8585
workers = nil
8686
tracer.trace('start_inserts', resource: 'inventory') do
87-
trace_digest = trace.to_digest
87+
trace_digest = trace.propagate!
8888

8989
workers = Array.new(5) do |index|
9090
Thread.new do
@@ -305,7 +305,7 @@ def lang_tag(span)
305305
describe 'distributed trace' do
306306
let(:extract) { Datadog::Tracing::Contrib::HTTP.extract(rack_headers) }
307307
let(:trace) { Datadog::Tracing.continue_trace!(extract) }
308-
let(:inject) { {}.tap { |env| Datadog::Tracing::Contrib::HTTP.inject(trace.to_digest, env) } }
308+
let(:inject) { {}.tap { |env| Datadog::Tracing::Contrib::HTTP.inject(trace.propagate!, env) } }
309309

310310
let(:rack_headers) { headers.map { |k, v| [RackSupport.header_to_rack(k), v] }.to_h }
311311

spec/datadog/tracing/tracer_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@
893893
trace_state_unknown_fields: 'any;field',
894894
)
895895
expect(digest.span_remote).to be true
896-
expect(trace.to_digest.span_remote).to be false
896+
expect(trace.propagate!.span_remote).to be false
897897

898898
expect(trace.send(:distributed_tags)).to eq('_dd.p.test' => 'value')
899899

0 commit comments

Comments
 (0)