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| Tracing::Diagnostics::EnvironmentLogger.collect_and_log!(responses: responses) end
Instance Method Summary collapse
-
#build_sampler(settings) ⇒ Object
TODO: Sampler should be a top-level component.
- #build_span_sampler(settings) ⇒ Object
- #build_trace_flush(settings) ⇒ Object
- #build_tracer(settings, logger:) ⇒ Object
-
#build_writer(settings, agent_settings, options = settings.tracing.writer_options) ⇒ Object
TODO: Writer should be a top-level component.
- #ensure_priority_sampling(sampler, settings) ⇒ Object
- #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
TODO: Sampler 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.
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 110 111 112 113 114 115 116 |
# File 'lib/datadog/tracing/component.rb', line 78 def build_sampler(settings) if (sampler = settings.tracing.sampler) if settings.tracing.priority_sampling == false sampler else ensure_priority_sampling(sampler, settings) end elsif (rules = settings.tracing.sampling.rules) post_sampler = Tracing::Sampling::RuleSampler.parse( rules, settings.tracing.sampling.rate_limit, settings.tracing.sampling.default_rate ) post_sampler ||= # Fallback RuleSampler in case `rules` parsing fails 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 ) elsif settings.tracing.priority_sampling == false Tracing::Sampling::RuleSampler.new( rate_limit: settings.tracing.sampling.rate_limit, default_sample_rate: settings.tracing.sampling.default_rate ) else Tracing::Sampling::PrioritySampler.new( base_sampler: Tracing::Sampling::AllSampler.new, post_sampler: Tracing::Sampling::RuleSampler.new( rate_limit: settings.tracing.sampling.rate_limit, default_sample_rate: settings.tracing.sampling.default_rate ) ) end end |
#build_span_sampler(settings) ⇒ Object
176 177 178 179 |
# File 'lib/datadog/tracing/component.rb', line 176 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
63 64 65 66 67 68 69 70 71 |
# File 'lib/datadog/tracing/component.rb', line 63 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, 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 61 |
# File 'lib/datadog/tracing/component.rb', line 26 def build_tracer(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? agent_settings = Configuration::AgentSettingsResolver.call(settings, logger: logger) # 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: (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.
137 138 139 140 141 142 143 |
# File 'lib/datadog/tracing/component.rb', line 137 def build_writer(settings, agent_settings, = settings.tracing.) if (writer = settings.tracing.writer) return writer end Tracing::Writer.new(agent_settings: agent_settings, **) end |
#ensure_priority_sampling(sampler, settings) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/datadog/tracing/component.rb', line 118 def ensure_priority_sampling(sampler, settings) if sampler.is_a?(Tracing::Sampling::PrioritySampler) sampler else Tracing::Sampling::PrioritySampler.new( base_sampler: sampler, post_sampler: Tracing::Sampling::RuleSampler.new( rate_limit: settings.tracing.sampling.rate_limit, default_sample_rate: settings.tracing.sampling.default_rate ) ) end end |
#subscribe_to_writer_events!(writer, sampler_delegator, test_mode) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/datadog/tracing/component.rb', line 145 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.
166 167 168 169 170 171 172 173 174 |
# File 'lib/datadog/tracing/component.rb', line 166 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 |