Module: Datadog::Tracing::Component

Defined in:
lib/datadog/tracing/component.rb

Overview

Tracing component

Defined Under Namespace

Classes: SamplerDelegatorComponent

Constant Summary collapse

WRITER_RECORD_ENVIRONMENT_INFORMATION_CALLBACK =
lambda do |_, responses|
  WRITER_RECORD_ENVIRONMENT_INFORMATION_ONLY_ONCE.run do
    Tracing::Diagnostics::EnvironmentLogger.collect_and_log!(responses: responses)
  end
end
WRITER_RECORD_ENVIRONMENT_INFORMATION_ONLY_ONCE =
Core::Utils::OnlyOnce.new

Class Method Summary collapse

Class Method Details

.build_native_transport(agent_settings) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/datadog/tracing/component.rb', line 114

def build_native_transport(agent_settings)
  require_relative "transport/native"

  unless Transport::Native.supported?
    Datadog.logger.warn(
      "Native transport requested but not available: #{Transport::Native::UNSUPPORTED_REASON}. " \
      "Falling back to default HTTP transport."
    )
    return nil
  end

  Transport::Native::Transport.new(
    agent_settings: agent_settings,
    logger: Datadog.logger
  )
end

.build_rate_limit_post_sampler(seconds:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build a post-sampler that limits the rate of traces to one per seconds. E.g.: build_rate_limit_post_sampler(seconds: 60) will limit the rate to one trace per minute.



214
215
216
217
218
219
# File 'lib/datadog/tracing/component.rb', line 214

def build_rate_limit_post_sampler(seconds:)
  Tracing::Sampling::RuleSampler.new(
    rate_limiter: Datadog::Core::TokenBucket.new(1.0 / seconds, 1.0),
    default_sample_rate: 1.0
  )
end

.build_sampler(settings) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datadog/tracing/component.rb', line 63

def build_sampler(settings)
  # A custom sampler is provided
  if (sampler = settings.tracing.sampler)
    return sampler
  end

  # APM Disablement means that we don't want to send traces that only contains APM data.
  # Other products can then put the sampling priority to MANUAL_KEEP if they want to keep traces.
  # (e.g.: AppSec will MANUAL_KEEP traces with AppSec events) and clients will be billed only for those traces.
  # But to keep the service alive on the backend side, we need to send one trace per minute.
  post_sampler = build_rate_limit_post_sampler(seconds: 60) unless settings.apm.tracing.enabled

  # Sampling rules are provided
  if (rules = settings.tracing.sampling.rules)
    post_sampler = Tracing::Sampling::RuleSampler.parse(
      rules,
      settings.tracing.sampling.rate_limit,
      settings.tracing.sampling.default_rate
    )
  end

  # The default sampler.
  # Used if no custom sampler is provided, or if sampling rule parsing fails.
  post_sampler ||= Tracing::Sampling::RuleSampler.new(
    rate_limit: settings.tracing.sampling.rate_limit,
    default_sample_rate: settings.tracing.sampling.default_rate
  )

  Tracing::Sampling::PrioritySampler.new(
    base_sampler: Tracing::Sampling::AllSampler.new,
    post_sampler: post_sampler
  )
end

.build_span_sampler(settings) ⇒ Object



166
167
168
169
# File 'lib/datadog/tracing/component.rb', line 166

def build_span_sampler(settings)
  rules = Tracing::Sampling::Span::RuleParser.parse_json(settings.tracing.sampling.span_rules)
  Tracing::Sampling::Span::Sampler.new(rules || [])
end

.build_test_mode_samplerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



228
229
230
231
232
233
234
235
# File 'lib/datadog/tracing/component.rb', line 228

def build_test_mode_sampler
  # Do not sample any spans for tests; all must be preserved.
  # Set priority sampler to ensure the agent doesn't drop any traces.
  Tracing::Sampling::PrioritySampler.new(
    base_sampler: Tracing::Sampling::AllSampler.new,
    post_sampler: Tracing::Sampling::AllSampler.new
  )
end

.build_test_mode_trace_flush(settings) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



222
223
224
225
# File 'lib/datadog/tracing/component.rb', line 222

def build_test_mode_trace_flush(settings)
  # If context flush behavior is provided, use it instead.
  settings.tracing.test_mode.trace_flush || build_trace_flush(settings)
end

.build_test_mode_writer(settings, agent_settings) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



238
239
240
241
242
243
244
245
# File 'lib/datadog/tracing/component.rb', line 238

def build_test_mode_writer(settings, agent_settings)
  writer_options = settings.tracing.test_mode.writer_options || {}

  return build_writer(settings, agent_settings, writer_options) if settings.tracing.test_mode.async

  # Flush traces synchronously, to guarantee they are written.
  Tracing::SyncWriter.new(agent_settings: agent_settings, **writer_options)
end

.build_trace_flush(settings) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/datadog/tracing/component.rb', line 53

def build_trace_flush(settings)
  if settings.tracing.partial_flush.enabled
    Tracing::Flush::Partial.new(
      min_spans_before_partial_flush: settings.tracing.partial_flush.min_spans_threshold
    )
  else
    Tracing::Flush::Finished.new
  end
end

.build_tracer(settings, agent_settings, logger:) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/datadog/tracing/component.rb', line 17

def build_tracer(settings, agent_settings, logger:)
  # If a custom tracer has been provided, use it instead.
  # Ignore all other options (they should already be configured.)
  tracer = settings.tracing.instance
  return tracer unless tracer.nil?

  # Apply test mode settings if test mode is activated
  if settings.tracing.test_mode.enabled
    trace_flush = build_test_mode_trace_flush(settings)
    sampler = build_test_mode_sampler
    writer = build_test_mode_writer(settings, agent_settings)
  else
    trace_flush = build_trace_flush(settings)
    sampler = build_sampler(settings)
    writer = build_writer(settings, agent_settings)
  end

  # The sampler instance is wrapped in a delegator,
  # so dynamic instrumentation can hot-swap it.
  # This prevents full tracer reinitialization on sampling changes.
  sampler_delegator = SamplerDelegatorComponent.new(sampler)

  subscribe_to_writer_events!(writer, sampler_delegator, settings.tracing.test_mode.enabled)

  Tracing::Tracer.new(
    default_service: settings.service,
    enabled: settings.tracing.enabled,
    logger: logger,
    trace_flush: trace_flush,
    sampler: sampler_delegator,
    span_sampler: build_span_sampler(settings),
    writer: writer,
    tags: build_tracer_tags(settings),
  )
end

.build_tracer_tags(settings) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



204
205
206
207
208
209
# File 'lib/datadog/tracing/component.rb', line 204

def build_tracer_tags(settings)
  settings.tags.dup.tap do |tags|
    tags[Core::Environment::Ext::TAG_ENV] = settings.env unless settings.env.nil?
    tags[Core::Environment::Ext::TAG_VERSION] = settings.version unless settings.version.nil?
  end
end

.build_writer(settings, agent_settings, options = settings.tracing.writer_options) ⇒ Object

TODO: Writer should be a top-level component. It is currently part of the Tracer initialization process, but can take a variety of options (including a fully custom instance) that makes the Tracer initialization process complex.



102
103
104
105
106
107
108
109
110
111
112
# File 'lib/datadog/tracing/component.rb', line 102

def build_writer(settings, agent_settings, options = settings.tracing.writer_options)
  if (writer = settings.tracing.writer)
    return writer
  end

  if settings.tracing.native_transport && (transport = build_native_transport(agent_settings))
    options = options.merge(transport: transport)
  end

  Tracing::Writer.new(agent_settings: agent_settings, **options)
end

.subscribe_to_writer_events!(writer, sampler_delegator, test_mode) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/datadog/tracing/component.rb', line 131

def subscribe_to_writer_events!(writer, sampler_delegator, test_mode)
  return unless writer.respond_to?(:events) # Check if it's a custom, external writer

  writer.events.after_send.subscribe(&WRITER_RECORD_ENVIRONMENT_INFORMATION_CALLBACK)

  # DEV: We need to ignore priority sampling updates coming from the agent in test mode
  # because test mode wants to *unconditionally* sample all traces.
  #
  # This can cause trace metrics to be overestimated, but that's a trade-off we take
  # here to achieve 100% sampling rate.
  return if test_mode

  writer.events.after_send.subscribe(&writer_update_priority_sampler_rates_callback(sampler_delegator))
end

.writer_update_priority_sampler_rates_callback(sampler) ⇒ Object

Create new lambda for writer callback, capture the current sampler in the callback closure.



156
157
158
159
160
161
162
163
164
# File 'lib/datadog/tracing/component.rb', line 156

def writer_update_priority_sampler_rates_callback(sampler)
  lambda do |_, responses|
    response = responses.last

    next unless response && !response.internal_error? && response.service_rates

    sampler.update(response.service_rates, decision: Tracing::Sampling::Ext::Decision::AGENT_RATE)
  end
end