class ElasticAPM::Instrumenter

rubocop:disable Metrics/ClassLength @api private

Constants

SPAN_KEY
TRANSACTION_KEY

Attributes

config[R]
enqueue[R]
stacktrace_builder[R]

Public Class Methods

new(config, stacktrace_builder:, &enqueue) click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 41
def initialize(config, stacktrace_builder:, &enqueue)
  @config = config
  @stacktrace_builder = stacktrace_builder
  @enqueue = enqueue

  @current = Current.new
end

Public Instance Methods

current_span() click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 130
def current_span
  current_spans.last
end
current_spans() click to toggle source

spans

# File lib/elastic_apm/instrumenter.rb, line 126
def current_spans
  @current.spans
end
current_transaction() click to toggle source

transactions

# File lib/elastic_apm/instrumenter.rb, line 72
def current_transaction
  @current.transaction
end
current_transaction=(transaction) click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 76
def current_transaction=(transaction)
  @current.transaction = transaction
end
end_span() click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/PerceivedComplexity rubocop:enable Metrics/MethodLength, Metrics/CyclomaticComplexity

# File lib/elastic_apm/instrumenter.rb, line 175
def end_span
  return unless (span = current_spans.pop)

  span.done

  enqueue.call span

  span
end
end_transaction(result = nil) click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/elastic_apm/instrumenter.rb, line 112
def end_transaction(result = nil)
  return nil unless (transaction = current_transaction)

  self.current_transaction = nil

  transaction.done result

  enqueue.call transaction

  transaction
end
inspect() click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 204
def inspect
  '<ElasticAPM::Instrumenter ' \
    "current_transaction=#{current_transaction.inspect}" \
    '>'
end
set_custom_context(context) click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 194
def set_custom_context(context)
  return unless current_transaction
  current_transaction.context.custom.merge!(context)
end
set_tag(key, value) click to toggle source

metadata

# File lib/elastic_apm/instrumenter.rb, line 187
def set_tag(key, value)
  return unless current_transaction

  key = key.to_s.gsub(/[\."\*]/, '_').to_sym
  current_transaction.context.tags[key] = value.to_s
end
set_user(user) click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 199
def set_user(user)
  return unless current_transaction
  current_transaction.context.user = Context::User.infer(config, user)
end
start() click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 51
def start
  debug 'Starting instrumenter'
end
start_span( name, type = nil, backtrace: nil, context: nil, trace_context: nil ) click to toggle source

rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity

# File lib/elastic_apm/instrumenter.rb, line 136
def start_span(
  name,
  type = nil,
  backtrace: nil,
  context: nil,
  trace_context: nil
)
  return unless (transaction = current_transaction)
  return unless transaction.sampled?

  transaction.inc_started_spans!

  if transaction.max_spans_reached?(config)
    transaction.inc_dropped_spans!
    return
  end

  parent = current_span || transaction

  span = Span.new(
    name: name,
    transaction_id: transaction.id,
    trace_context: trace_context || parent.trace_context.child,
    type: type,
    context: context,
    stacktrace_builder: stacktrace_builder
  )

  if backtrace && config.span_frames_min_duration?
    span.original_backtrace = backtrace
  end

  current_spans.push span

  span.start
end
start_transaction( name = nil, type = nil, context: nil, trace_context: nil ) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/elastic_apm/instrumenter.rb, line 81
def start_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil
)
  return nil unless config.instrument?

  if (transaction = current_transaction)
    raise ExistingTransactionError,
      "Transactions may not be nested.\nAlready inside #{transaction}"
  end

  sampled = trace_context ? trace_context.recorded? : random_sample?

  transaction =
    Transaction.new(
      name,
      type,
      context: context,
      trace_context: trace_context,
      sampled: sampled,
      tags: config.default_tags
    )

  transaction.start

  self.current_transaction = transaction
end
stop() click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 55
def stop
  debug 'Stopping instrumenter'

  self.current_transaction = nil
  current_spans.pop until current_spans.empty?

  @subscriber.unregister! if @subscriber
end
subscriber=(subscriber) click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 64
def subscriber=(subscriber)
  debug 'Registering subscriber'
  @subscriber = subscriber
  @subscriber.register!
end

Private Instance Methods

random_sample?() click to toggle source
# File lib/elastic_apm/instrumenter.rb, line 212
def random_sample?
  rand <= config.transaction_sample_rate
end