Class: Datadog::Tracing::Tracer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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.

Parameters:

  • trace_flush (Datadog::Tracing::TraceFlush) (defaults to: Flush::Finished.new)

    responsible for flushing spans from the execution context

  • context_provider (Datadog::Tracing::DefaultContextProvider) (defaults to: DefaultContextProvider.new)

    ensures different execution contexts have distinct traces

  • default_service (String) (defaults to: Core::Environment::Ext::FALLBACK_SERVICE_NAME)

    A fallback value for Span#service, as spans without service are rejected

  • enabled (Boolean) (defaults to: true)

    set if the tracer submits or not spans to the local agent

  • sampler (Datadog::Tracing::Sampler) (defaults to: Sampling::PrioritySampler.new( base_sampler: Sampling::AllSampler.new, post_sampler: Sampling::RuleSampler.new ))

    a tracer sampler, responsible for filtering out spans when needed

  • tags (Hash) (defaults to: {})

    default tags added to all spans

  • writer (Datadog::Tracing::Writer)

    consumes traces returned by the provided trace_flush



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 = tags
  @writer = writer
  @components = components
end

Instance Attribute Details

#default_serviceObject

Returns the value of attribute default_service.



34
35
36
# File 'lib/datadog/tracing/tracer.rb', line 34

def default_service
  @default_service
end

#enabledObject

Returns the value of attribute enabled.



34
35
36
# File 'lib/datadog/tracing/tracer.rb', line 34

def enabled
  @enabled
end

#loggerObject (readonly)

Returns the value of attribute logger.



26
27
28
# File 'lib/datadog/tracing/tracer.rb', line 26

def logger
  @logger
end

#providerObject (readonly)

Returns the value of attribute provider.



26
27
28
# File 'lib/datadog/tracing/tracer.rb', line 26

def provider
  @provider
end

#samplerObject (readonly)

Returns the value of attribute sampler.



26
27
28
# File 'lib/datadog/tracing/tracer.rb', line 26

def sampler
  @sampler
end

#span_samplerObject (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

#tagsObject (readonly)

Returns the value of attribute tags.



26
27
28
# File 'lib/datadog/tracing/tracer.rb', line 26

def tags
  @tags
end

#trace_flushObject (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

#writerObject

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.

Parameters:

  • key (Thread) (defaults to: nil)

    Thread to retrieve trace from. Defaults to current thread. For internal use only.

Returns:



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.

Parameters:

  • key (Thread) (defaults to: nil)

    Thread to retrieve trace from. Defaults to current thread. For internal use only.

Returns:



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.

Parameters:

  • key (Thread) (defaults to: nil)

    Thread to retrieve trace from. Defaults to current thread. For internal use only.

Returns:



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.

Parameters:

Yields:

Returns:



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.

Examples:

tracer.set_tags('env' => 'prod', 'component' => 'core')


207
208
209
210
# File 'lib/datadog/tracing/tracer.rb', line 207

def set_tags(tags)
  string_tags = tags.collect { |k, v| [k.to_s, v] }.to_h
  @tags = @tags.merge(string_tags)
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.

Examples:

tracer.trace('operation_name', service='rake_tasks') do |span_op|
  span_op.set_tag('task.name', 'script')
end

tracer.shutdown!


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

Parameters:

  • name (String)
  • continue_from (Datadog::Tracing::TraceDigest) (defaults to: nil)

    continue a trace from a Datadog::Tracing::TraceDigest. Used for linking traces that are executed asynchronously.

  • on_error (Proc) (defaults to: nil)

    a block that overrides error handling behavior for this operation.

  • resource (String) (defaults to: nil)

    the resource this span refers, or name if it's missing

  • service (String) (defaults to: nil)

    the service name for this span.

  • start_time (Time) (defaults to: nil)

    time which the span should have started.

  • tags (Hash<String,String>) (defaults to: nil)

    extra tags which should be added to the span.

  • type (String) (defaults to: nil)

    the type of the span. See Metadata::Ext::AppTypes.

  • the (Integer)

    id of the new span.

Yields:

  • Optional block where new newly created SpanOperation captures the execution.

Yield Parameters:

Returns:



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: 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: tags,
      type: type,
      _trace: trace,
      id: id
    )
  end
end