class ElasticAPM::Instrumenter
@api private
rubocop:disable Metrics/ClassLength
def current_span
def current_span @current.span end
def current_span=(span)
def current_span=(span) @current.span = span end
def current_transaction
def current_transaction @current.transaction end
def current_transaction=(transaction)
def current_transaction=(transaction) @current.transaction = transaction end
def end_span
def end_span return unless (span = current_span) span.done self.current_span = span.parent&.is_a?(Span) && span.parent || nil enqueue.call span span end
def end_transaction(result = nil)
def end_transaction(result = nil) return nil unless (transaction = current_transaction) self.current_transaction = nil transaction.done result enqueue.call transaction transaction end
def initialize(config, &enqueue)
def initialize(config, &enqueue) @config = config @enqueue = enqueue @current = Current.new end
def inspect
def inspect '<ElasticAPM::Instrumenter ' \ "current_transaction=#{current_transaction.inspect}" \ '>' end
def random_sample?
def random_sample? rand <= config.transaction_sample_rate end
def set_custom_context(context)
def set_custom_context(context) return unless current_transaction current_transaction.context.custom.merge!(context) end
def set_tag(key, value)
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
def set_user(user)
def set_user(user) return unless current_transaction current_transaction.context.user = Context::User.new(config, user) end
def span_frames_min_duration?
def span_frames_min_duration? config.span_frames_min_duration != 0 end
def start
def start debug 'Starting instrumenter' end
def start_span(name, type = nil, backtrace: nil, context: nil)
def start_span(name, type = nil, backtrace: nil, 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 span = Span.new( name, type, transaction: transaction, parent: current_span || transaction, context: context ) if backtrace && span_frames_min_duration? span.original_backtrace = backtrace end self.current_span = span span.start end
def start_transaction(
def start_transaction( name = nil, type = nil, context: nil, traceparent: nil ) return nil unless config.instrument? if (transaction = current_transaction) raise ExistingTransactionError, "Transactions may not be nested.\nAlready inside #{transaction}" end sampled = traceparent ? traceparent.recorded? : random_sample? transaction = Transaction.new( name, type, context: context, traceparent: traceparent, sampled: sampled ) transaction.start self.current_transaction = transaction end
def stop
def stop debug 'Stopping instrumenter' self.current_transaction = nil self.current_span = nil @subscriber.unregister! if @subscriber end
def subscriber=(subscriber)
def subscriber=(subscriber) @subscriber = subscriber @subscriber.register! end