Class: Tobox::Plugins::Sentry::EventHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/tobox/plugins/sentry.rb

Constant Summary collapse

TOBOX_NAME =
"tobox"

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ EventHandler

Returns a new instance of EventHandler.



26
27
28
29
30
31
# File 'lib/tobox/plugins/sentry.rb', line 26

def initialize(config)
  @config = config
  @db_table = @config[:table]
  @db_scheme = URI(@config[:database_uri]).scheme if @config[:database_uri]
  @max_attempts = @config[:max_attempts]
end

Instance Method Details

#on_error(event, error) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/tobox/plugins/sentry.rb', line 81

def on_error(event, error)
  return unless ::Sentry.initialized?

  capture_exception(event, error)

  transaction = retrieve_transaction(event)

  return unless transaction

  finish_transaction(transaction, 500)
end

#on_finish(event) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tobox/plugins/sentry.rb', line 68

def on_finish(event)
  return unless ::Sentry.initialized?

  transaction = retrieve_transaction(event)

  return unless transaction

  finish_transaction(transaction, 200)

  scope = ::Sentry.get_current_scope
  scope.clear
end

#on_start(event) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tobox/plugins/sentry.rb', line 33

def on_start(event)
  return unless ::Sentry.initialized?

  ::Sentry.clone_hub_to_current_thread

  scope = ::Sentry.get_current_scope

  scope.set_contexts(tobox: {
                       id: event[:id],
                       type: event[:type],
                       attempts: event[:attempts],
                       created_at: event[:created_at],
                       run_at: event[:run_at],
                       last_error: event[:last_error]&.byteslice(0..1000),
                       version: Tobox::VERSION,
                       db_adapter: @db_scheme
                     })
  scope.set_tags(
    outbox: @db_table,
    event_id: event[:id],
    event_type: event[:type]
  )

  scope.set_transaction_name("#{TOBOX_NAME}/#{event[:type]}") unless scope.transaction_name

  transaction = start_transaction(scope.transaction_name, event[:metadata].to_h["sentry_trace"])

  return unless transaction

  scope.set_span(transaction)

  # good for thread pool, good for fiber pool
  store_transaction(event, transaction)
end