class Hoss::Agent

@api private

Constants

LOCK

Attributes

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

Public Class Methods

instance() click to toggle source

life cycle

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

  @central_config = CentralConfig.new(config)
  @transport = Transport::Base.new(config)
  @instrumenter = Instrumenter.new(
    config,
    metrics: nil,
    stacktrace_builder: stacktrace_builder
  ) { |event| enqueue event }
  @pid = Process.pid
end
running?() click to toggle source
# File lib/hoss/agent.rb, line 74
def self.running?
  !!@instance
end
start(config) click to toggle source
# File lib/hoss/agent.rb, line 45
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/hoss/agent.rb, line 65
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/hoss/agent.rb, line 211
def add_filter(key, callback)
  transport.add_filter(key, callback)
end
build_context(rack_env:, for_type:) click to toggle source
# File lib/hoss/agent.rb, line 175
def build_context(rack_env:, for_type:)
  @context_builder.build(rack_env: rack_env, for_type: for_type)
end
current_event() click to toggle source
# File lib/hoss/agent.rb, line 150
def current_event
  instrumenter.current_event
end
detect_forking!() click to toggle source
# File lib/hoss/agent.rb, line 221
def detect_forking!
  return if @pid == Process.pid

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

  central_config.handle_forking!
  transport.handle_forking!
  instrumenter.handle_forking!
  metrics.handle_forking!

  @pid = Process.pid
end
end_event() click to toggle source
# File lib/hoss/agent.rb, line 159
def end_event
  instrumenter.end_event
end
enqueue(obj) click to toggle source

transport

# File lib/hoss/agent.rb, line 146
def enqueue(obj)
  transport.submit obj
end
inspect() click to toggle source

misc

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

errors

# File lib/hoss/agent.rb, line 181
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/hoss/agent.rb, line 195
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/hoss/agent.rb, line 167
def set_custom_context(context)
  instrumenter.set_custom_context(context)
end
set_label(key, value) click to toggle source
# File lib/hoss/agent.rb, line 163
def set_label(key, value)
  instrumenter.set_label(key, value)
end
set_user(user) click to toggle source
# File lib/hoss/agent.rb, line 171
def set_user(user)
  instrumenter.set_user(user)
end
start() click to toggle source
# File lib/hoss/agent.rb, line 105
def start
  unless config.api_key
    config.logger.warn "Hoss API Key missing, not starting."
    return
  end
  unless config.disable_start_message?
    config.logger.info format(
      '[%s] Starting agent, reporting to %s',
      VERSION, config.api_host
    )
  end

  central_config.start
  transport.start
  instrumenter.start
  # metrics.start

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

  self
end
start_event() click to toggle source
# File lib/hoss/agent.rb, line 154
def start_event
  detect_forking!
  instrumenter.start_event
end
stop() click to toggle source
# File lib/hoss/agent.rb, line 130
def stop
  debug 'Stopping agent'

  central_config.stop
  instrumenter.stop
  transport.stop

  self
end