class Atatus::Agent

@api private

Constants

LOCK

Attributes

central_config[R]
collector[R]
config[R]
context_builder[R]
error_builder[R]
instrumenter[R]
metrics[R]
stacktrace_builder[R]

Public Class Methods

instance() click to toggle source

life cycle

# File lib/atatus/agent.rb, line 42
def self.instance # rubocop:disable Style/TrivialAccessors
  @instance
end
new(config) click to toggle source
# File lib/atatus/agent.rb, line 79
def initialize(config)
  @stacktrace_builder = StacktraceBuilder.new(config)
  @context_builder = ContextBuilder.new(config)
  @error_builder = ErrorBuilder.new(self)

  @central_config = CentralConfig.new(config)
  @collector = Collector::Base.new(config)
  @metrics = Metrics.new(config) { |event| enqueue event }
  @instrumenter = Instrumenter.new(
    config,
    metrics: metrics,
    stacktrace_builder: stacktrace_builder
  ) { |event| enqueue event }
  @pid = Process.pid
end
running?() click to toggle source
# File lib/atatus/agent.rb, line 75
def self.running?
  !!@instance
end
start(config) click to toggle source
# File lib/atatus/agent.rb, line 46
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.enabled?
      config.logger.debug format(
        "%sAgent disabled with `enabled: false'",
        Logging::PREFIX
      )
      return
    end

    @instance = new(config).start
  end
end
stop() click to toggle source
# File lib/atatus/agent.rb, line 66
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/atatus/agent.rb, line 286
def add_filter(key, callback)
  # transport.add_filter(key, callback)
end
build_context(rack_env:, for_type:) click to toggle source
# File lib/atatus/agent.rb, line 250
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/atatus/agent.rb, line 176
def current_span
  instrumenter.current_span
end
current_transaction() click to toggle source

instrumentation

# File lib/atatus/agent.rb, line 172
def current_transaction
  instrumenter.current_transaction
end
detect_forking!() click to toggle source
# File lib/atatus/agent.rb, line 296
def detect_forking!
  return if @pid == Process.pid

  config.logger.debug "Detected forking,
    restarting threads in process [PID:#{Process.pid}]"

  central_config.handle_forking!
  collector.handle_forking!
  instrumenter.handle_forking!
  metrics.handle_forking!

  @pid = Process.pid
end
end_span() click to toggle source

rubocop:enable Metrics/ParameterLists

# File lib/atatus/agent.rb, line 234
def end_span
  instrumenter.end_span
end
end_transaction(result = nil) click to toggle source
# File lib/atatus/agent.rb, line 198
def end_transaction(result = nil)
  instrumenter.end_transaction(result)
end
enqueue(obj) click to toggle source

transport

# File lib/atatus/agent.rb, line 156
def enqueue(obj)
  # transport.submit obj
  case obj
  when Atatus::Transaction
    collector.add_txn(obj)
  when Atatus::Span
    collector.add_span(obj)
  when Atatus::Error
    collector.add_error(obj)
  when Atatus::Metricset
    collector.add_metrics(obj)
  end
end
inspect() click to toggle source

misc

Calls superclass method
# File lib/atatus/agent.rb, line 292
def inspect
  super.split.first + '>'
end
report(exception, context: nil, handled: true) click to toggle source

errors

# File lib/atatus/agent.rb, line 256
def report(exception, context: nil, handled: true)
  return unless config.recording?
  detect_forking!
  return if config.filter_exception_types.include?(exception.class.to_s)

  error = @error_builder.build_exception(
    exception,
    context: context,
    handled: handled
  )
  enqueue error
  error.id
end
report_message(message, context: nil, backtrace: nil, **attrs) click to toggle source
# File lib/atatus/agent.rb, line 270
def report_message(message, context: nil, backtrace: nil, **attrs)
  return unless config.recording?
  detect_forking!

  error = @error_builder.build_log(
    message,
    context: context,
    backtrace: backtrace,
    **attrs
  )
  enqueue error
  error.id
end
set_custom_context(context) click to toggle source
# File lib/atatus/agent.rb, line 242
def set_custom_context(context)
  instrumenter.set_custom_context(context)
end
set_label(key, value) click to toggle source
# File lib/atatus/agent.rb, line 238
def set_label(key, value)
  instrumenter.set_label(key, value)
end
set_user(user) click to toggle source
# File lib/atatus/agent.rb, line 246
def set_user(user)
  instrumenter.set_user(user)
end
start() click to toggle source
# File lib/atatus/agent.rb, line 108
def start
  unless config.disable_start_message?
    config.logger.info format(
      '[%s] Starting atatus ruby agent',
      VERSION
    )
  end

  if config.license_key.nil? || config.app_name.nil?
    if config.license_key.nil? && config.app_name.nil?
      error 'Atatus configuration license_key and app_name are missing'
    elsif config.license_key.nil?
      error 'Atatus configuration license_key is missing'
    elsif config.app_name.nil?
      error 'Atatus configuration app_name is missing'
    end
  end

  central_config.start
  collector.start
  instrumenter.start
  metrics.start

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

  self
end
start_span( name = nil, type = nil, subtype: nil, action: nil, backtrace: nil, context: nil, trace_context: nil, parent: nil, sync: nil ) click to toggle source

rubocop:disable Metrics/ParameterLists

# File lib/atatus/agent.rb, line 203
def start_span(
  name = nil,
  type = nil,
  subtype: nil,
  action: nil,
  backtrace: nil,
  context: nil,
  trace_context: nil,
  parent: nil,
  sync: nil
)
  detect_forking!

  # We don't check config.recording? because the span
  # will not be created if there's no transaction.
  # We want to use the recording value from the config
  # that existed when start_transaction was called. ~estolfo
  instrumenter.start_span(
    name,
    type,
    subtype: subtype,
    action: action,
    backtrace: backtrace,
    context: context,
    trace_context: trace_context,
    parent: parent,
    sync: sync
  )
end
start_transaction( name = nil, type = nil, context: nil, trace_context: nil ) click to toggle source
# File lib/atatus/agent.rb, line 180
def start_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil
)
  return unless config.recording?
  detect_forking!

  instrumenter.start_transaction(
    name,
    type,
    config: config,
    context: context,
    trace_context: trace_context
  )
end
stop() click to toggle source
# File lib/atatus/agent.rb, line 139
def stop
  debug 'Stopping agent'

  central_config.stop
  metrics.stop
  instrumenter.stop
  collector.stop

  self
end