class Sentry::Client

Attributes

configuration[R]
logger[R]
transport[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/sentry/client.rb, line 9
def initialize(configuration)
  @configuration = configuration
  @logger = configuration.logger

  if transport_class = configuration.transport.transport_class
    @transport = transport_class.new(configuration)
  else
    @transport =
      case configuration.dsn&.scheme
      when 'http', 'https'
        HTTPTransport.new(configuration)
      else
        DummyTransport.new(configuration)
      end
  end
end

Public Instance Methods

capture_event(event, scope, hint = {}) click to toggle source
# File lib/sentry/client.rb, line 26
def capture_event(event, scope, hint = {})
  return unless configuration.sending_allowed?

  event = scope.apply_to_event(event, hint)

  if event.nil?
    log_info("Discarded event because one of the event processors returned nil")
    return
  end

  if async_block = configuration.async
    dispatch_async_event(async_block, event, hint)
  elsif configuration.background_worker_threads != 0 && hint.fetch(:background, true)
    dispatch_background_event(event, hint)
  else
    send_event(event, hint)
  end

  event
rescue => e
  log_error("Event capturing failed", e, debug: configuration.debug)
  nil
end
event_from_exception(exception, hint = {}) click to toggle source
# File lib/sentry/client.rb, line 50
def event_from_exception(exception, hint = {})
  integration_meta = Sentry.integrations[hint[:integration]]
  return unless @configuration.exception_class_allowed?(exception)

  Event.new(configuration: configuration, integration_meta: integration_meta).tap do |event|
    event.add_exception_interface(exception)
    event.add_threads_interface(crashed: true)
  end
end
event_from_message(message, hint = {}, backtrace: nil) click to toggle source
# File lib/sentry/client.rb, line 60
def event_from_message(message, hint = {}, backtrace: nil)
  integration_meta = Sentry.integrations[hint[:integration]]
  event = Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
  event.add_threads_interface(backtrace: backtrace || caller)
  event
end
event_from_transaction(transaction) click to toggle source
# File lib/sentry/client.rb, line 67
def event_from_transaction(transaction)
  TransactionEvent.new(configuration: configuration).tap do |event|
    event.transaction = transaction.name
    event.contexts.merge!(trace: transaction.get_trace_context)
    event.timestamp = transaction.timestamp
    event.start_timestamp = transaction.start_timestamp

    finished_spans = transaction.span_recorder.spans.select { |span| span.timestamp && span != transaction }
    event.spans = finished_spans.map(&:to_hash)
  end
end
generate_sentry_trace(span) click to toggle source
# File lib/sentry/client.rb, line 103
def generate_sentry_trace(span)
  return unless configuration.propagate_traces

  trace = span.to_sentry_trace
  log_debug("[Tracing] Adding #{SENTRY_TRACE_HEADER_NAME} header to outgoing request: #{trace}")
  trace
end
send_event(event, hint = nil) click to toggle source
# File lib/sentry/client.rb, line 79
def send_event(event, hint = nil)
  event_type = event.is_a?(Event) ? event.type : event["type"]

  if event_type != TransactionEvent::TYPE && configuration.before_send
    event = configuration.before_send.call(event, hint)

    if event.nil?
      log_info("Discarded event because before_send returned nil")
      return
    end
  end

  transport.send_event(event)

  event
rescue => e
  loggable_event_type = (event_type || "event").capitalize
  log_error("#{loggable_event_type} sending failed", e, debug: configuration.debug)

  event_info = Event.get_log_message(event.to_hash)
  log_info("Unreported #{loggable_event_type}: #{event_info}")
  raise
end

Private Instance Methods

dispatch_async_event(async_block, event, hint) click to toggle source
# File lib/sentry/client.rb, line 119
def dispatch_async_event(async_block, event, hint)
  # We have to convert to a JSON-like hash, because background job
  # processors (esp ActiveJob) may not like weird types in the event hash
  event_hash = event.to_json_compatible

  if async_block.arity == 2
    hint = JSON.parse(JSON.generate(hint))
    async_block.call(event_hash, hint)
  else
    async_block.call(event_hash)
  end
rescue => e
  loggable_event_type = event_hash["type"] || "event"
  log_error("Async #{loggable_event_type} sending failed", e, debug: configuration.debug)
  send_event(event, hint)
end
dispatch_background_event(event, hint) click to toggle source
# File lib/sentry/client.rb, line 113
def dispatch_background_event(event, hint)
  Sentry.background_worker.perform do
    send_event(event, hint)
  end
end