Class: Sentry::Hub

Inherits:
Object show all
Includes:
ArgumentCheckingHelper
Defined in:
lib/sentry/hub.rb

Defined Under Namespace

Classes: Layer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, scope) ⇒ Hub

Returns a new instance of Hub.



10
11
12
13
14
# File 'lib/sentry/hub.rb', line 10

def initialize(client, scope)
  first_layer = Layer.new(client, scope)
  @stack = [first_layer]
  @last_event_id = nil
end

Instance Attribute Details

#last_event_idObject (readonly)

Returns the value of attribute last_event_id.



8
9
10
# File 'lib/sentry/hub.rb', line 8

def last_event_id
  @last_event_id
end

Instance Method Details

#add_breadcrumb(breadcrumb, hint: {}) ⇒ Object



125
126
127
128
129
130
131
132
133
# File 'lib/sentry/hub.rb', line 125

def add_breadcrumb(breadcrumb, hint: {})
  if before_breadcrumb = current_client.configuration.before_breadcrumb
    breadcrumb = before_breadcrumb.call(breadcrumb, hint)
  end

  return unless breadcrumb

  current_scope.add_breadcrumb(breadcrumb)
end

#bind_client(client) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/sentry/hub.rb', line 38

def bind_client(client)
  layer = current_layer

  if layer
    layer.client = client
  end
end

#capture_event(event, **options, &block) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/sentry/hub.rb', line 103

def capture_event(event, **options, &block)
  return unless current_client

  check_argument_type!(event, Sentry::Event)

  hint = options.delete(:hint) || {}
  scope = current_scope.dup

  if block
    block.call(scope)
  elsif custom_scope = options[:scope]
    scope.update_from_scope(custom_scope)
  elsif !options.empty?
    scope.update_from_options(**options)
  end

  event = current_client.capture_event(event, scope, hint)

  @last_event_id = event&.event_id
  event
end

#capture_exception(exception, **options, &block) ⇒ Object



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

def capture_exception(exception, **options, &block)
  return unless current_client

  check_argument_type!(exception, ::Exception)

  options[:hint] ||= {}
  options[:hint][:exception] = exception
  event = current_client.event_from_exception(exception, options[:hint])

  return unless event

  capture_event(event, **options, &block)
end

#capture_message(message, **options, &block) ⇒ Object



94
95
96
97
98
99
100
101
# File 'lib/sentry/hub.rb', line 94

def capture_message(message, **options, &block)
  return unless current_client

  options[:hint] ||= {}
  options[:hint][:message] = message
  event = current_client.event_from_message(message, options[:hint])
  capture_event(event, **options, &block)
end

#cloneObject



28
29
30
31
32
33
34
35
36
# File 'lib/sentry/hub.rb', line 28

def clone
  layer = current_layer

  if layer
    scope = layer.scope&.dup

    Hub.new(layer.client, scope)
  end
end

#configure_scope(&block) ⇒ Object



46
47
48
# File 'lib/sentry/hub.rb', line 46

def configure_scope(&block)
  block.call(current_scope)
end

#current_clientObject



20
21
22
# File 'lib/sentry/hub.rb', line 20

def current_client
  current_layer&.client
end

#current_scopeObject



24
25
26
# File 'lib/sentry/hub.rb', line 24

def current_scope
  current_layer&.scope
end

#new_from_topObject



16
17
18
# File 'lib/sentry/hub.rb', line 16

def new_from_top
  Hub.new(current_client, current_scope)
end

#pop_scopeObject



68
69
70
# File 'lib/sentry/hub.rb', line 68

def pop_scope
  @stack.pop
end

#push_scopeObject



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

def push_scope
  new_scope =
    if current_scope
      current_scope.dup
    else
      Scope.new
    end

  @stack << Layer.new(current_client, new_scope)
end

#start_transaction(transaction: nil, configuration: Sentry.configuration, **options) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/sentry/hub.rb', line 72

def start_transaction(transaction: nil, configuration: Sentry.configuration, **options)
  return unless configuration.tracing_enabled?

  transaction ||= Transaction.new(**options)
  transaction.set_initial_sample_decision(configuration: current_client.configuration)
  transaction
end

#with_scope(&block) ⇒ Object



50
51
52
53
54
55
# File 'lib/sentry/hub.rb', line 50

def with_scope(&block)
  push_scope
  yield(current_scope)
ensure
  pop_scope
end