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

Returns a new instance of Client.



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

def initialize(configuration)
  @configuration = configuration

  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

#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



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/sentry/client.rb', line 23

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

  scope.apply_to_event(event, hint)

  if async_block = configuration.async
    begin
      # 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
      configuration.logger.error(LOGGER_PROGNAME) { "async event sending failed: #{e.message}" }
      send_event(event, hint)
    end
  else
    if hint.fetch(:background, true)
      Sentry.background_worker.perform do
        send_event(event, hint)
      end
    else
      send_event(event, hint)
    end
  end

  event
end

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



57
58
59
60
61
62
63
64
65
# File 'lib/sentry/client.rb', line 57

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



67
68
69
70
71
72
# File 'lib/sentry/client.rb', line 67

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



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sentry/client.rb', line 74

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



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/sentry/client.rb', line 86

def send_event(event, hint = nil)
  event_type = event.is_a?(Event) ? event.type : event["type"]
  event = configuration.before_send.call(event, hint) if configuration.before_send && event_type == "event"

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

  transport.send_event(event)

  event
end