class ElasticAPM::Agent

rubocop:disable Metrics/ClassLength @api private

Constants

LOCK

Attributes

config[R]
context_builder[R]
error_builder[R]
instrumenter[R]
metrics[R]
stacktrace_builder[R]
transport[R]

Public Class Methods

instance() click to toggle source

life cycle

# File lib/elastic_apm/agent.rb, line 21
def self.instance # rubocop:disable Style/TrivialAccessors
  @instance
end
new(config) click to toggle source
# File lib/elastic_apm/agent.rb, line 60
def initialize(config)
  @config = config

  @stacktrace_builder = StacktraceBuilder.new(config)
  @context_builder = ContextBuilder.new(config)
  @error_builder = ErrorBuilder.new(self)

  @transport = Transport::Base.new(config)
  @instrumenter = Instrumenter.new(
    config,
    stacktrace_builder: stacktrace_builder
  ) { |event| enqueue event }
  @metrics = Metrics.new(config) { |event| enqueue event }
end
running?() click to toggle source
# File lib/elastic_apm/agent.rb, line 56
def self.running?
  !!@instance
end
start(config) click to toggle source

rubocop:disable Metrics/MethodLength

# File lib/elastic_apm/agent.rb, line 26
def self.start(config)
  return @instance if @instance

  config = Config.new(config) unless config.is_a?(Config)

  LOCK.synchronize do
    return @instance if @instance

    unless config.active?
      config.logger.debug format(
        "%sAgent disabled with `active: false'",
        Logging::PREFIX
      )
      return
    end

    @instance = new(config).start
  end
end
stop() click to toggle source

rubocop:enable Metrics/MethodLength

# File lib/elastic_apm/agent.rb, line 47
def self.stop
  LOCK.synchronize do
    return unless @instance

    @instance.stop
    @instance = nil
  end
end

Public Instance Methods

add_filter(key, callback) click to toggle source

filters

# File lib/elastic_apm/agent.rb, line 206
def add_filter(key, callback)
  transport.add_filter(key, callback)
end
build_context(rack_env:, for_type:) click to toggle source
# File lib/elastic_apm/agent.rb, line 177
def build_context(rack_env:, for_type:)
  @context_builder.build(rack_env: rack_env, for_type: for_type)
end
current_span() click to toggle source
# File lib/elastic_apm/agent.rb, line 123
def current_span
  instrumenter.current_span
end
current_transaction() click to toggle source

instrumentation

# File lib/elastic_apm/agent.rb, line 119
def current_transaction
  instrumenter.current_transaction
end
end_span() click to toggle source
# File lib/elastic_apm/agent.rb, line 161
def end_span
  instrumenter.end_span
end
end_transaction(result = nil) click to toggle source
# File lib/elastic_apm/agent.rb, line 141
def end_transaction(result = nil)
  instrumenter.end_transaction(result)
end
enqueue(obj) click to toggle source

transport

# File lib/elastic_apm/agent.rb, line 113
def enqueue(obj)
  transport.submit obj
end
report(exception, context: nil, handled: true) click to toggle source

errors

# File lib/elastic_apm/agent.rb, line 183
def report(exception, context: nil, handled: true)
  return if config.filter_exception_types.include?(exception.class.to_s)

  error = @error_builder.build_exception(
    exception,
    context: context,
    handled: handled
  )
  enqueue error
end
report_message(message, context: nil, backtrace: nil, **attrs) click to toggle source
# File lib/elastic_apm/agent.rb, line 194
def report_message(message, context: nil, backtrace: nil, **attrs)
  error = @error_builder.build_log(
    message,
    context: context,
    backtrace: backtrace,
    **attrs
  )
  enqueue error
end
set_custom_context(context) click to toggle source
# File lib/elastic_apm/agent.rb, line 169
def set_custom_context(context)
  instrumenter.set_custom_context(context)
end
set_tag(key, value) click to toggle source
# File lib/elastic_apm/agent.rb, line 165
def set_tag(key, value)
  instrumenter.set_tag(key, value)
end
set_user(user) click to toggle source
# File lib/elastic_apm/agent.rb, line 173
def set_user(user)
  instrumenter.set_user(user)
end
start() click to toggle source

rubocop:disable Metrics/AbcSize, Metrics/MethodLength

# File lib/elastic_apm/agent.rb, line 79
def start
  unless config.disable_start_message
    info '[%s] Starting agent, reporting to %s', VERSION, config.server_url
  end

  transport.start
  instrumenter.start
  metrics.start

  config.enabled_spies.each do |lib|
    debug "Requiring spy: #{lib}"
    require "elastic_apm/spies/#{lib}"
  end

  self
end
start_span( name = nil, type = nil, backtrace: nil, context: nil, trace_context: nil ) click to toggle source
# File lib/elastic_apm/agent.rb, line 145
def start_span(
  name = nil,
  type = nil,
  backtrace: nil,
  context: nil,
  trace_context: nil
)
  instrumenter.start_span(
    name,
    type,
    backtrace: backtrace,
    context: context,
    trace_context: trace_context
  )
end
start_transaction( name = nil, type = nil, context: nil, trace_context: nil ) click to toggle source
# File lib/elastic_apm/agent.rb, line 127
def start_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil
)
  instrumenter.start_transaction(
    name,
    type,
    context: context,
    trace_context: trace_context
  )
end
stop() click to toggle source

rubocop:enable Metrics/AbcSize, Metrics/MethodLength

# File lib/elastic_apm/agent.rb, line 97
def stop
  debug 'Stopping agent'

  metrics.stop
  instrumenter.stop
  transport.stop

  self
end