class Atatus::OpenTracing::Tracer

A custom tracer to use the OpenTracing API with Atatus

Attributes

scope_manager[R]

Public Class Methods

new() click to toggle source
# File lib/atatus/opentracing.rb, line 212
def initialize
  @scope_manager = ScopeManager.new
end

Public Instance Methods

active_span() click to toggle source
# File lib/atatus/opentracing.rb, line 218
def active_span
  scope_manager.active&.span
end
extract(format, carrier) click to toggle source
# File lib/atatus/opentracing.rb, line 319
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) click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/atatus/opentracing.rb, line 309
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, ** ) { |scope| ... } click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/atatus/opentracing.rb, line 223
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, ** ) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/atatus/opentracing.rb, line 256
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

Private Instance Methods

context_from_active_scope(ignore_active_scope) click to toggle source
# File lib/atatus/opentracing.rb, line 368
def context_from_active_scope(ignore_active_scope)
  if ignore_active_scope
    Atatus.agent&.config&.logger&.warn(
      'ignore_active_scope might lead to unexpected results'
    )
    return
  end
  @scope_manager.active&.span&.context
end
context_from_child_of(child_of) click to toggle source
# File lib/atatus/opentracing.rb, line 353
def context_from_child_of(child_of)
  return unless child_of
  child_of.respond_to?(:context) ? child_of.context : child_of
end
context_from_references(references) click to toggle source
# File lib/atatus/opentracing.rb, line 358
def context_from_references(references)
  return if !references || references.none?

  child_of = references.find do |reference|
    reference.type == ::OpenTracing::Reference::CHILD_OF
  end

  (child_of || references.first).context
end
prepare_span_context( child_of:, references:, ignore_active_scope: ) click to toggle source
# File lib/atatus/opentracing.rb, line 340
def prepare_span_context(
  child_of:,
  references:,
  ignore_active_scope:
)
  context = context_from_child_of(child_of) ||
            context_from_references(references) ||
            context_from_active_scope(ignore_active_scope)
  return context.child if context&.respond_to?(:child)

  context
end