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 execution context 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:) ⇒ 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:) ⇒ 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 |
# 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: # 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 = @writer = writer 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 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.
235 236 237 238 239 240 241 |
# File 'lib/datadog/tracing/tracer.rb', line 235 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.
224 225 226 227 |
# File 'lib/datadog/tracing/tracer.rb', line 224 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.
213 214 215 |
# File 'lib/datadog/tracing/tracer.rb', line 213 def active_trace(key = nil) call_context(key).active_trace end |
#continue_trace!(digest, key = nil) { ... } ⇒ Object, Datadog::Tracing::TraceOperation
Setup a new trace execution context to continue from where another trace left off. This is useful to continue distributed or async traces.
The first span created in the restored context is a direct child of the active span from when the Datadog::Tracing::TraceDigest was created.
When no block is given, the trace context is restored in the current thread. It remains active until the first span created in this restored context is finished. After that, if a new span is created, it start a new, unrelated trace.
When a block is given, the trace context is restored inside the block execution. It remains active until the block ends, even when the first span created inside the block finishes. This means that multiple spans can be direct children of the active span from when the Datadog::Tracing::TraceDigest was created.
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/datadog/tracing/tracer.rb', line 265 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) # When we want the trace to be bound to a block, we cannot let # it auto finish when the local root span finishes. This would # create mutiple traces inside the block. Instead, we'll # expliclity finish the trace after the block finishes. auto_finish = !block trace = start_trace(continue_from: digest, auto_finish: auto_finish) # 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 if block # When a block is given, the trace will be active until the block finishes. context.activate!(trace) do yield ensure # We have to flush even when an error occurs # On block completion, force the trace to finish and flush its finished spans. # Unfinished spans are lost as the {TraceOperation} has ended. trace.finish! flush_trace(trace) end else # Otherwise, the trace will be bound to the current thread after this point context.activate!(trace) end end |
#sample_trace(trace_op) ⇒ Object
Sample a span, tagging the trace as appropriate.
302 303 304 305 306 307 308 |
# File 'lib/datadog/tracing/tracer.rb', line 302 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.
201 202 203 204 |
# File 'lib/datadog/tracing/tracer.rb', line 201 def () = .collect { |k, v| [k.to_s, v] }.to_h = .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.
341 342 343 344 345 |
# File 'lib/datadog/tracing/tracer.rb', line 341 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() “‘
132 133 134 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 |
# File 'lib/datadog/tracing/tracer.rb', line 132 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 # 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 |