Class: Datadog::Tracing::Tracer
- Inherits:
-
Object
- Object
- Datadog::Tracing::Tracer
- Defined in:
- lib/datadog/tracing/tracer.rb
Overview
A Tracer keeps track of the time spent by an application processing a single operation. For example, a trace can be used to track the entire time spent processing a complicated web request. Even though the request may require multiple resources and machines to handle the request, all of these function calls and sub-requests would be encapsulated within a single trace.
Defined Under Namespace
Classes: TraceCompleted
Instance Attribute Summary collapse
-
#default_service ⇒ Object
Returns the value of attribute default_service.
-
#enabled ⇒ Object
Returns the value of attribute enabled.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#sampler ⇒ Object
readonly
Returns the value of attribute sampler.
-
#span_sampler ⇒ Object
readonly
Returns the value of attribute span_sampler.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#trace_flush ⇒ Object
readonly
Returns the value of attribute trace_flush.
-
#writer ⇒ Object
Returns the value of attribute writer.
Instance Method Summary collapse
-
#active_correlation(key = nil) ⇒ Datadog::Tracing::Correlation::Identifier
Information about the currently active trace.
-
#active_span(key = nil) ⇒ Datadog::Tracing::SpanOperation?
The active, unfinished span, representing the currently instrumented application section.
-
#active_trace(key = nil) ⇒ Datadog::Tracing::TraceSegment?
The active, unfinished trace, representing the current instrumentation context.
-
#continue_trace!(digest, key = nil) { ... } ⇒ Object, Datadog::Tracing::TraceOperation
Setup a new trace to continue from where another trace left off.
-
#initialize(trace_flush: Flush::Finished.new, context_provider: DefaultContextProvider.new, default_service: Core::Environment::Ext::FALLBACK_SERVICE_NAME, enabled: true, logger: Datadog.logger, sampler: Sampling::PrioritySampler.new( base_sampler: Sampling::AllSampler.new, post_sampler: Sampling::RuleSampler.new ), span_sampler: Sampling::Span::Sampler.new, tags: {}, writer:, components: nil) ⇒ Tracer
constructor
Initialize a new Tracer used to create, sample and submit spans that measure the time of sections of code.
-
#sample_trace(trace_op) ⇒ Object
Sample a span, tagging the trace as appropriate.
-
#set_tags(tags) ⇒ Object
Set the given key / value tag pair at the tracer level.
-
#shutdown! ⇒ Object
Shorthand that calls the
shutdown!method of a registered worker. - #trace(name, continue_from: nil, on_error: nil, resource: nil, service: nil, start_time: nil, tags: nil, type: nil, id: nil) {|span_op, trace_op| ... } ⇒ Object, Datadog::Tracing::SpanOperation
Constructor Details
#initialize(trace_flush: Flush::Finished.new, context_provider: DefaultContextProvider.new, default_service: Core::Environment::Ext::FALLBACK_SERVICE_NAME, enabled: true, logger: Datadog.logger, sampler: Sampling::PrioritySampler.new( base_sampler: Sampling::AllSampler.new, post_sampler: Sampling::RuleSampler.new ), span_sampler: Sampling::Span::Sampler.new, tags: {}, writer:, components: nil) ⇒ Tracer
Initialize a new Datadog::Tracing::Tracer used to create, sample and submit spans that measure the time of sections of code.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/datadog/tracing/tracer.rb', line 51 def initialize( # rubocop:disable Style/KeywordParametersOrder # https://github.com/rubocop/rubocop/issues/13933 trace_flush: Flush::Finished.new, context_provider: DefaultContextProvider.new, default_service: Core::Environment::Ext::FALLBACK_SERVICE_NAME, enabled: true, logger: Datadog.logger, sampler: Sampling::PrioritySampler.new( base_sampler: Sampling::AllSampler.new, post_sampler: Sampling::RuleSampler.new ), span_sampler: Sampling::Span::Sampler.new, tags: {}, # writer is not defaulted because creating it requires agent_settings, # which we do not have here and otherwise do not need. writer:, components: nil # rubocop:enable Style/KeywordParametersOrder ) @trace_flush = trace_flush @default_service = default_service @enabled = enabled @logger = logger @provider = context_provider @sampler = sampler @span_sampler = span_sampler @tags = @writer = writer @components = components end |
Instance Attribute Details
#default_service ⇒ Object
Returns the value of attribute default_service.
34 35 36 |
# File 'lib/datadog/tracing/tracer.rb', line 34 def default_service @default_service end |
#enabled ⇒ Object
Returns the value of attribute enabled.
34 35 36 |
# File 'lib/datadog/tracing/tracer.rb', line 34 def enabled @enabled end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
26 27 28 |
# File 'lib/datadog/tracing/tracer.rb', line 26 def logger @logger end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
26 27 28 |
# File 'lib/datadog/tracing/tracer.rb', line 26 def provider @provider end |
#sampler ⇒ Object (readonly)
Returns the value of attribute sampler.
26 27 28 |
# File 'lib/datadog/tracing/tracer.rb', line 26 def sampler @sampler end |
#span_sampler ⇒ Object (readonly)
Returns the value of attribute span_sampler.
26 27 28 |
# File 'lib/datadog/tracing/tracer.rb', line 26 def span_sampler @span_sampler end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
26 27 28 |
# File 'lib/datadog/tracing/tracer.rb', line 26 def @tags end |
#trace_flush ⇒ Object (readonly)
Returns the value of attribute trace_flush.
26 27 28 |
# File 'lib/datadog/tracing/tracer.rb', line 26 def trace_flush @trace_flush end |
#writer ⇒ Object
Returns the value of attribute writer.
34 35 36 |
# File 'lib/datadog/tracing/tracer.rb', line 34 def writer @writer end |
Instance Method Details
#active_correlation(key = nil) ⇒ Datadog::Tracing::Correlation::Identifier
Information about the currently active trace.
The most common use cases are tagging log messages and metrics.
241 242 243 244 245 246 247 |
# File 'lib/datadog/tracing/tracer.rb', line 241 def active_correlation(key = nil) trace = active_trace(key) return Datadog::Tracing::Correlation::Identifier.new unless trace trace.to_correlation end |
#active_span(key = nil) ⇒ Datadog::Tracing::SpanOperation?
The active, unfinished span, representing the currently instrumented application section.
The active span belongs to an Datadog::Tracing.active_trace.
230 231 232 233 |
# File 'lib/datadog/tracing/tracer.rb', line 230 def active_span(key = nil) trace = active_trace(key) trace&.active_span end |
#active_trace(key = nil) ⇒ Datadog::Tracing::TraceSegment?
The active, unfinished trace, representing the current instrumentation context.
The active trace is fiber-local.
219 220 221 |
# File 'lib/datadog/tracing/tracer.rb', line 219 def active_trace(key = nil) call_context(key).active_trace end |
#continue_trace!(digest, key = nil) { ... } ⇒ Object, Datadog::Tracing::TraceOperation
Setup a new trace to continue from where another trace left off.
Used to continue distributed or async traces.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/datadog/tracing/tracer.rb', line 260 def continue_trace!(digest, key = nil, &block) # Only accept {TraceDigest} as a digest. # Otherwise, create a new execution context. digest = nil unless digest.is_a?(TraceDigest) # Start a new trace from the digest context = call_context(key) original_trace = active_trace(key) trace = start_trace(continue_from: digest) # If block hasn't been given; we need to manually deactivate # this trace. Subscribe to the trace finished event to do this. subscribe_trace_deactivation!(context, trace, original_trace) unless block context.activate!(trace, &block) end |
#sample_trace(trace_op) ⇒ Object
Sample a span, tagging the trace as appropriate.
278 279 280 281 282 283 284 |
# File 'lib/datadog/tracing/tracer.rb', line 278 def sample_trace(trace_op) @sampler.sample!(trace_op) if trace_op.sampling_priority.nil? rescue => e SAMPLE_TRACE_LOG_ONLY_ONCE.run do logger.warn { "Failed to sample trace: #{e.class.name} #{e} at #{Array(e.backtrace).first}" } end end |
#set_tags(tags) ⇒ Object
Set the given key / value tag pair at the tracer level. These tags will be appended to each span created by the tracer. Keys and values must be strings.
207 208 209 210 |
# File 'lib/datadog/tracing/tracer.rb', line 207 def () = .collect { |k, v| [k.to_s, v] }.to_h @tags = @tags.merge() end |
#shutdown! ⇒ Object
Shorthand that calls the shutdown! method of a registered worker.
It's useful to ensure that the Trace Buffer is properly flushed before
shutting down the application.
317 318 319 320 321 |
# File 'lib/datadog/tracing/tracer.rb', line 317 def shutdown! return unless @enabled @writer&.stop end |
#trace(name, continue_from: nil, on_error: nil, resource: nil, service: nil, start_time: nil, tags: nil, type: nil, id: nil) {|span_op, trace_op| ... } ⇒ Object, Datadog::Tracing::SpanOperation
Return a span_op and trace_op
that will trace an operation called name.
You could trace your code using a do-block like:
tracer.trace('web.request') do |span_op, trace_op|
span_op.service = 'my-web-site'
span_op.resource = '/'
span_op.set_tag('http.method', request.request_method)
do_something()
end
The #trace method can also be used without a block in this way:
span_op = tracer.trace('web.request', service: 'my-web-site')
do_something()
span_op.finish()
Remember that in this case, calling SpanOperation#finish is mandatory.
When a Trace is started, #trace will store the created span; subsequent spans will become its children and will inherit some properties:
parent = tracer.trace('parent') # has no parent span
child = tracer.trace('child') # is a child of 'parent'
child.finish()
parent.finish()
parent2 = tracer.trace('parent2') # has no parent span
parent2.finish()
rubocop:disable Metrics/MethodLength
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/datadog/tracing/tracer.rb', line 135 def trace( name, continue_from: nil, on_error: nil, resource: nil, service: nil, start_time: nil, tags: nil, type: nil, id: nil, &block ) return skip_trace(name, &block) unless enabled # Check Cloudwise probe state - if suspended, don't create spans return skip_trace(name, &block) if cloudwise_probe_suspended? # Resolve the trace begin context = call_context active_trace = context.active_trace trace = if continue_from || active_trace.nil? start_trace(continue_from: continue_from) else active_trace end rescue => e logger.debug { "Failed to trace: #{e}" } # Tracing failed: fallback and run code without tracing. return skip_trace(name, &block) end # Activate and start the trace if block context.activate!(trace) do start_span( name, on_error: on_error, resource: resource, service: service, start_time: start_time, tags: , type: type, _trace: trace, id: id, &block ) end else # Setup trace activation/deactivation manual_trace_activation!(context, trace) # Return the new span start_span( name, on_error: on_error, resource: resource, service: service, start_time: start_time, tags: , type: type, _trace: trace, id: id ) end end |