Class: Atatus::OpenTracing::Tracer

Inherits:
Object
  • Object
show all
Defined in:
lib/atatus/opentracing.rb

Overview

A custom tracer to use the OpenTracing API with Atatus

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTracer

Returns a new instance of Tracer.



214
215
216
# File 'lib/atatus/opentracing.rb', line 214

def initialize
  @scope_manager = ScopeManager.new
end

Instance Attribute Details

#scope_managerObject (readonly)

Returns the value of attribute scope_manager.



218
219
220
# File 'lib/atatus/opentracing.rb', line 218

def scope_manager
  @scope_manager
end

Instance Method Details

#active_spanObject



220
221
222
# File 'lib/atatus/opentracing.rb', line 220

def active_span
  scope_manager.active&.span
end

#extract(format, carrier) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'lib/atatus/opentracing.rb', line 321

def extract(format, carrier)
  case format
  when ::OpenTracing::FORMAT_RACK
    SpanContext.from_header(
      carrier['HTTP_ATATUS_TRACEPARENT']
    )
  when ::OpenTracing::FORMAT_TEXT_MAP
    SpanContext.from_header(
      carrier['atatus-traceparent']
    )
  else
    warn 'Only extraction from HTTP headers via Rack or in ' \
      'text map format are available'
    nil
  end
rescue Atatus::TraceContext::InvalidTraceparentHeader
  nil
end

#inject(span_context, format, carrier) ⇒ Object

rubocop:enable Metrics/ParameterLists



311
312
313
314
315
316
317
318
319
# File 'lib/atatus/opentracing.rb', line 311

def inject(span_context, format, carrier)
  case format
  when ::OpenTracing::FORMAT_RACK, ::OpenTracing::FORMAT_TEXT_MAP
    carrier['atatus-traceparent'] =
      span_context.traceparent.to_header
  else
    warn 'Only injection via HTTP headers and Rack is available'
  end
end

#start_active_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: {}, ignore_active_scope: false, finish_on_close: true) ⇒ Object

rubocop:disable Metrics/ParameterLists



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/atatus/opentracing.rb', line 225

def start_active_span(
  operation_name,
  child_of: nil,
  references: nil,
  start_time: Time.now,
  tags: {},
  ignore_active_scope: false,
  finish_on_close: true,
  **
)
  span = start_span(
    operation_name,
    child_of: child_of,
    references: references,
    start_time: start_time,
    tags: tags,
    ignore_active_scope: ignore_active_scope
  )
  scope = scope_manager.activate(span, finish_on_close: finish_on_close)

  if block_given?
    begin
      return yield scope
    ensure
      scope.close
    end
  end

  scope
end

#start_span(operation_name, child_of: nil, references: nil, start_time: Time.now, tags: {}, ignore_active_scope: false) ⇒ Object

rubocop:disable Metrics/ParameterLists



258
259
260
261
262
263
264
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
300
301
302
303
304
305
306
307
# File 'lib/atatus/opentracing.rb', line 258

def start_span(
  operation_name,
  child_of: nil,
  references: nil,
  start_time: Time.now,
  tags: {},
  ignore_active_scope: false,
  **
)
  span_context = prepare_span_context(
    child_of: child_of,
    references: references,
    ignore_active_scope: ignore_active_scope
  )

  if span_context
    trace_context =
      span_context.respond_to?(:trace_context) &&
      span_context.trace_context
  end

  atatus_span =
    if Atatus.current_transaction
      Atatus.start_span(
        operation_name,
        trace_context: trace_context
      )
    else
      Atatus.start_transaction(
        operation_name,
        trace_context: trace_context
      )
    end

  # if no Atatus agent is running or transaction not sampled
  unless atatus_span
    return ::OpenTracing::Span::NOOP_INSTANCE
  end

  span_context ||=
    SpanContext.from_trace_context(atatus_span.trace_context)

  tags.each do |key, value|
    atatus_span.context.labels[key] = value
  end

  atatus_span.start Util.micros(start_time)

  Span.new(atatus_span, span_context)
end