Class: Sentry::Client

Inherits:
Object show all
Defined in:
lib/sentry/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ Client



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/sentry/client.rb', line 7

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

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



5
6
7
# File 'lib/sentry/client.rb', line 5

def configuration
  @configuration
end

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/sentry/client.rb', line 5

def logger
  @logger
end

#transportObject (readonly)

Returns the value of attribute transport.



5
6
7
# File 'lib/sentry/client.rb', line 5

def transport
  @transport
end

Instance Method Details

#capture_event(event, scope, hint = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sentry/client.rb', line 24

def capture_event(event, scope, hint = {})
  return unless configuration.sending_allowed?

  scope.apply_to_event(event, hint)

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

  event
rescue => e
  logger.error(LOGGER_PROGNAME) { "Event capturing failed: #{e.message}" }
  nil
end

#event_from_exception(exception, hint = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/sentry/client.rb', line 43

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 = {}) ⇒ Object



53
54
55
56
57
58
# File 'lib/sentry/client.rb', line 53

def event_from_message(message, hint = {})
  integration_meta = Sentry.integrations[hint[:integration]]
  event = Event.new(configuration: configuration, integration_meta: integration_meta, message: message)
  event.add_threads_interface(backtrace: caller)
  event
end

#event_from_transaction(transaction) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/sentry/client.rb', line 60

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

#send_event(event, hint = nil) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sentry/client.rb', line 72

def send_event(event, hint = nil)
  event_type = event.is_a?(Event) ? event.type : event["type"]

  if event_type == "event" && configuration.before_send
    event = configuration.before_send.call(event, hint)

    if event.nil?
      logger.info(LOGGER_PROGNAME) { "Discarded event because before_send returned nil" }
      return
    end
  end

  transport.send_event(event)

  event
rescue => e
  logger.error(LOGGER_PROGNAME) { "#{event_type.capitalize} sending failed: #{e.message}" }
  logger.error(LOGGER_PROGNAME) { "Unreported #{event_type.capitalize}: #{Event.get_log_message(event.to_hash)}" }
  raise
end