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, sampler: Sampling::PrioritySampler.new( base_sampler: Sampling::AllSampler.new, post_sampler: Sampling::RuleSampler.new ), span_sampler: Sampling::Span::Sampler.new, tags: {}, writer: Writer.new) ⇒ 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) (defaults to: Writer.new)

    consumes traces returned by the provided trace_flush



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/datadog/tracing/tracer.rb', line 48

def initialize(
  trace_flush: Flush::Finished.new,
  context_provider: DefaultContextProvider.new,
  default_service: Core::Environment::Ext::FALLBACK_SERVICE_NAME,
  enabled: true,
  sampler: Sampling::PrioritySampler.new(
    base_sampler: Sampling::AllSampler.new,
    post_sampler: Sampling::RuleSampler.new
  ),
  span_sampler: Sampling::Span::Sampler.new,
  tags: {},
  writer: Writer.new
)
  @trace_flush = trace_flush
  @default_service = default_service
  @enabled = enabled
  @provider = context_provider
  @sampler = sampler
  @span_sampler = span_sampler
  @tags = tags
  @writer = writer
end

Instance Attribute Details

#default_serviceObject

Returns the value of attribute default_service.



31
32
33
# File 'lib/datadog/tracing/tracer.rb', line 31

def default_service
  @default_service
end

#enabledObject

Returns the value of attribute enabled.



31
32
33
# File 'lib/datadog/tracing/tracer.rb', line 31

def enabled
  @enabled
end

#providerObject (readonly)

Returns the value of attribute provider.



24
25
26
# File 'lib/datadog/tracing/tracer.rb', line 24

def provider
  @provider
end

#samplerObject (readonly)

Returns the value of attribute sampler.



24
25
26
# File 'lib/datadog/tracing/tracer.rb', line 24

def sampler
  @sampler
end

#span_samplerObject (readonly)

Returns the value of attribute span_sampler.



24
25
26
# File 'lib/datadog/tracing/tracer.rb', line 24

def span_sampler
  @span_sampler
end

#tagsObject (readonly)

Returns the value of attribute tags.



24
25
26
# File 'lib/datadog/tracing/tracer.rb', line 24

def tags
  @tags
end

#trace_flushObject (readonly)

Returns the value of attribute trace_flush.



24
25
26
# File 'lib/datadog/tracing/tracer.rb', line 24

def trace_flush
  @trace_flush
end

#writerObject

Returns the value of attribute writer.



31
32
33
# File 'lib/datadog/tracing/tracer.rb', line 31

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:



229
230
231
232
233
234
# File 'lib/datadog/tracing/tracer.rb', line 229

def active_correlation(key = nil)
  trace = active_trace(key)
  Correlation.identifier_from_digest(
    trace && trace.to_digest
  )
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:



218
219
220
221
# File 'lib/datadog/tracing/tracer.rb', line 218

def active_span(key = nil)
  trace = active_trace(key)
  trace.active_span if trace
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:



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

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:



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/datadog/tracing/tracer.rb', line 247

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

#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')


195
196
197
198
# File 'lib/datadog/tracing/tracer.rb', line 195

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!


295
296
297
298
299
# File 'lib/datadog/tracing/tracer.rb', line 295

def shutdown!
  return unless @enabled

  @writer.stop if @writer
end

#trace(name, continue_from: nil, on_error: nil, resource: nil, service: nil, start_time: nil, tags: nil, type: nil, span_type: nil, _context: 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 Lint/UnderscorePrefixedVariableName rubocop:disable Metrics/MethodLength

Parameters:

  • name (String)

    Span operation name. See / Primary Operations in Services.

  • 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.

Yields:

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

Yield Parameters:

Returns:



123
124
125
126
127
128
129
130
131
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
# File 'lib/datadog/tracing/tracer.rb', line 123

def trace(
  name,
  continue_from: nil,
  on_error: nil,
  resource: nil,
  service: nil,
  start_time: nil,
  tags: nil,
  type: nil,
  span_type: nil,
  _context: nil,
  &block
)
  return skip_trace(name, &block) unless enabled

  context, trace = nil

  # Resolve the trace
  begin
    context = _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 StandardError => e
    Datadog.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: span_type || type,
        _trace: trace,
        &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: span_type || type,
      _trace: trace
    )
  end
end