class Opbeat::Client

@api private

Constants

KEY
LOCK

Attributes

config[R]
pending_transactions[R]
queue[R]

Public Class Methods

inst() click to toggle source

life cycle

# File lib/opbeat/client.rb, line 29
def self.inst
  @instance
end
new(config) click to toggle source
# File lib/opbeat/client.rb, line 51
def initialize config
  @config = config

  @http_client = HttpClient.new config
  @queue = Queue.new

  @data_builders = Struct.new(:transactions, :error_message).new(
    DataBuilders::Transactions.new(config),
    DataBuilders::Error.new(config)
  )

  unless config.disable_performance
    @transaction_info = TransactionInfo.new
    @subscriber = Subscriber.new config, self
  end

  @pending_transactions = []
  @last_sent_transactions = Time.now.utc
end
start!(config = nil) click to toggle source
# File lib/opbeat/client.rb, line 33
def self.start! config = nil
  return @instance if @instance

  LOCK.synchronize do
    return @instance if @instance
    @instance = new(config).start!
  end
end
stop!() click to toggle source
# File lib/opbeat/client.rb, line 42
def self.stop!
  LOCK.synchronize do
    return unless @instance

    @instance.stop!
    @instance = nil
  end
end

Public Instance Methods

capture() { || ... } click to toggle source
# File lib/opbeat/client.rb, line 214
def capture &block
  unless block_given?
    return Kernel.at_exit do
      if $!
        debug $!.inspect
        report $!
      end
    end
  end

  begin
    yield
  rescue Error => e
    raise # Don't capture Opbeat errors
  rescue Exception => e
    report e
    raise
  end
end
current_transaction() click to toggle source

metrics

# File lib/opbeat/client.rb, line 93
def current_transaction
  @transaction_info.current
end
current_transaction=(transaction) click to toggle source
# File lib/opbeat/client.rb, line 97
def current_transaction= transaction
  @transaction_info.current = transaction
end
flush_transactions() click to toggle source
# File lib/opbeat/client.rb, line 158
def flush_transactions
  return if @pending_transactions.empty?

  data = @data_builders.transactions.build(@pending_transactions)
  enqueue Worker::PostRequest.new('/transactions/', data)

  @last_sent_transactions = Time.now.utc
  @pending_transactions = []

  true
end
release(rel, opts = {}) click to toggle source

releases

# File lib/opbeat/client.rb, line 236
def release rel, opts = {}
  rev = rel[:rev]
  if opts[:inline]
    debug "Sending release #{rev}"
    @http_client.post '/releases/', rel
  else
    debug "Enqueuing release #{rev}"
    enqueue Worker::PostRequest.new('/releases/', rel)
  end
end
report(exception, opts = {}) click to toggle source
# File lib/opbeat/client.rb, line 186
def report exception, opts = {}
  return if config.disable_errors
  return unless exception

  ensure_worker_running

  unless exception.backtrace
    exception.set_backtrace caller
  end

  if error_message = ErrorMessage.from_exception(config, exception, opts)
    error_message.add_extra(@context) if @context
    data = @data_builders.error_message.build error_message
    enqueue Worker::PostRequest.new('/errors/', data)
  end
end
report_message(message, opts = {}) click to toggle source
# File lib/opbeat/client.rb, line 203
def report_message message, opts = {}
  return if config.disable_errors

  ensure_worker_running

  error_message = ErrorMessage.new(config, message, opts)
  error_message.add_extra(@context) if @context
  data = @data_builders.error_message.build error_message
  enqueue Worker::PostRequest.new('/errors/', data)
end
set_context(context) click to toggle source

errors

# File lib/opbeat/client.rb, line 172
def set_context context
  @context = context
end
start!() click to toggle source
# File lib/opbeat/client.rb, line 73
def start!
  info "Starting client"

  @subscriber.register! if @subscriber

  self
end
stop!() click to toggle source
# File lib/opbeat/client.rb, line 81
def stop!
  flush_transactions
  kill_worker
  unregister! if @subscriber
end
submit_transaction(transaction) click to toggle source
# File lib/opbeat/client.rb, line 142
def submit_transaction transaction
  ensure_worker_running

  if config.debug_traces
    unless transaction.endpoint == 'Rack'
      debug { Util::Inspector.new.transaction transaction, include_parents: true }
    end
  end

  @pending_transactions << transaction

  if should_send_transactions?
    flush_transactions
  end
end
trace(*args) { || ... } click to toggle source
# File lib/opbeat/client.rb, line 128
def trace *args, &block
  if config.disable_performance
    return yield if block_given?
    return nil
  end

  unless transaction = current_transaction
    return yield if block_given?
    return
  end

  transaction.trace(*args, &block)
end
transaction(endpoint, kind = nil, result = nil) { || ... } click to toggle source
# File lib/opbeat/client.rb, line 101
def transaction endpoint, kind = nil, result = nil, &block
  if config.disable_performance
    return yield if block_given?
    return nil
  end

  if transaction = current_transaction
    yield transaction if block_given?
    return transaction
  end

  transaction = Transaction.new self, endpoint, kind, result

  self.current_transaction = transaction
  return transaction unless block_given?

  begin
    yield transaction

  ensure
    self.current_transaction = nil
    transaction.done
  end

  transaction
end
with_context(context) { || ... } click to toggle source
# File lib/opbeat/client.rb, line 176
def with_context context, &block
  current = @context

  set_context((current || {}).merge(context))

  yield if block_given?
ensure
  set_context(current)
end

Private Instance Methods

enqueue(request) click to toggle source
# File lib/opbeat/client.rb, line 249
def enqueue request
  @queue << request
end
ensure_worker_running() click to toggle source
# File lib/opbeat/client.rb, line 282
def ensure_worker_running
  return if worker_running?

  LOCK.synchronize do
    return if worker_running?
    start_worker
  end
end
kill_worker() click to toggle source
# File lib/opbeat/client.rb, line 273
def kill_worker
  return unless worker_running?
  @queue << Worker::StopMessage.new
  unless @worker_thread.join(config.worker_quit_timeout)
    error "Failed to wait for worker, not all messages sent"
  end
  @worker_thread = nil
end
should_send_transactions?() click to toggle source
# File lib/opbeat/client.rb, line 299
def should_send_transactions?
  return true if config.transaction_post_interval.nil?

  Time.now.utc - @last_sent_transactions > config.transaction_post_interval
end
start_worker() click to toggle source
# File lib/opbeat/client.rb, line 253
def start_worker
  return if worker_running?

  if config.disable_worker
    return
  end

  info "Starting worker in thread"

  @worker_thread = Thread.new do
    begin
      Worker.new(config, @queue, @http_client).run
    rescue => e
      fatal "Failed booting worker:\n#{e.inspect}"
      debug e.backtrace.join("\n")
      raise
    end
  end
end
unregister!() click to toggle source
# File lib/opbeat/client.rb, line 295
def unregister!
  @subscriber.unregister!
end
worker_running?() click to toggle source
# File lib/opbeat/client.rb, line 291
def worker_running?
  @worker_thread && @worker_thread.alive?
end