Module: Datadog::Tracing::Component

Included in:
Core::Configuration::Components
Defined in:
lib/datadog/tracing/component.rb

Overview

Tracing component

Defined Under Namespace

Modules: InstanceMethods 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

Instance Method Summary collapse

Instance Method Details

#build_sampler(settings) ⇒ Object



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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/datadog/tracing/component.rb', line 70

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

  # AppSec events are sent to the backend using traces.
  # Standalone ASM billing means that we don't want to charge clients for APM traces,
  # so we want to send the minimum amount of traces possible (idealy only traces that contains security events),
  # but for features such as API Security, we need to send at least one trace per minute,
  # to keep the service alive on the backend side.
  if settings.appsec.standalone.enabled
    post_sampler = Tracing::Sampling::RuleSampler.new(
      [Tracing::Sampling::SimpleRule.new(sample_rate: 1.0)],
      rate_limiter: Datadog::Core::TokenBucket.new(1.0 / 60, 1.0),
      default_sample_rate: 1.0 / 60
    )
  end

  # 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



159
160
161
162
# File 'lib/datadog/tracing/component.rb', line 159

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_trace_flush(settings) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/datadog/tracing/component.rb', line 60

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



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
52
53
54
55
56
57
58
# File 'lib/datadog/tracing/component.rb', line 25

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,
    trace_flush: trace_flush,
    sampler: sampler_delegator,
    span_sampler: build_span_sampler(settings),
    writer: writer,
    tags: build_tracer_tags(settings),
  )
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.



116
117
118
119
120
121
122
# File 'lib/datadog/tracing/component.rb', line 116

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

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

#configure_tracing(settings) ⇒ Object

Configure non-privileged components.



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

def configure_tracing(settings)
  Datadog::Tracing::Contrib::Component.configure(settings)
end

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



124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/datadog/tracing/component.rb', line 124

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.



149
150
151
152
153
154
155
156
157
# File 'lib/datadog/tracing/component.rb', line 149

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