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
- #build_sampler(settings) ⇒ Object
- #build_span_sampler(settings) ⇒ Object
- #build_trace_flush(settings) ⇒ Object
- #build_tracer(settings, agent_settings, logger:) ⇒ Object
-
#build_writer(settings, agent_settings, options = settings.tracing.writer_options) ⇒ Object
TODO: Writer should be a top-level component.
-
#configure_tracing(settings) ⇒ Object
Configure non-privileged components.
- #subscribe_to_writer_events!(writer, sampler_delegator, test_mode) ⇒ Object
-
#writer_update_priority_sampler_rates_callback(sampler) ⇒ Object
Create new lambda for writer callback, capture the current sampler in the callback closure.
Instance Method Details
#build_sampler(settings) ⇒ Object
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 |
# File 'lib/datadog/tracing/component.rb', line 72 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
154 155 156 157 |
# File 'lib/datadog/tracing/component.rb', line 154 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
62 63 64 65 66 67 68 69 70 |
# File 'lib/datadog/tracing/component.rb', line 62 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
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 59 60 |
# File 'lib/datadog/tracing/component.rb', line 26 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: (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.
111 112 113 114 115 116 117 |
# File 'lib/datadog/tracing/component.rb', line 111 def build_writer(settings, agent_settings, = settings.tracing.) if (writer = settings.tracing.writer) return writer end Tracing::Writer.new(agent_settings: agent_settings, **) end |
#configure_tracing(settings) ⇒ Object
Configure non-privileged components.
160 161 162 |
# File 'lib/datadog/tracing/component.rb', line 160 def configure_tracing(settings) Datadog::Tracing::Contrib::Component.configure(settings) end |
#subscribe_to_writer_events!(writer, sampler_delegator, test_mode) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/datadog/tracing/component.rb', line 119 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.
144 145 146 147 148 149 150 151 152 |
# File 'lib/datadog/tracing/component.rb', line 144 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 |