Class: Sentry::Scope

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

Constant Summary collapse

ATTRIBUTES =
[:transaction_names, :contexts, :extra, :tags, :user, :level, :breadcrumbs, :fingerprint, :event_processors, :rack_env, :span]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScope

Returns a new instance of Scope.



12
13
14
# File 'lib/sentry/scope.rb', line 12

def initialize
  set_default_value
end

Class Method Details

.os_contextObject



188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/sentry/scope.rb', line 188

def os_context
  @os_context ||=
    begin
      uname = Etc.uname
      {
        name: uname[:sysname] || RbConfig::CONFIG["host_os"],
        version: uname[:version],
        build: uname[:release],
        kernel_version: uname[:version]
      }
    end
end

.runtime_contextObject



201
202
203
204
205
206
# File 'lib/sentry/scope.rb', line 201

def runtime_context
  @runtime_context ||= {
    name: RbConfig::CONFIG["ruby_install_name"],
    version: RUBY_DESCRIPTION || Sentry.sys_command("ruby -v")
  }
end

Instance Method Details

#add_breadcrumb(breadcrumb) ⇒ Object



45
46
47
# File 'lib/sentry/scope.rb', line 45

def add_breadcrumb(breadcrumb)
  breadcrumbs.record(breadcrumb)
end

#add_event_processor(&block) ⇒ Object



162
163
164
# File 'lib/sentry/scope.rb', line 162

def add_event_processor(&block)
  @event_processors << block
end

#apply_to_event(event, hint = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/sentry/scope.rb', line 20

def apply_to_event(event, hint = nil)
  event.tags = tags.merge(event.tags)
  event.user = user.merge(event.user)
  event.extra = extra.merge(event.extra)
  event.contexts = contexts.merge(event.contexts)

  if span
    event.contexts[:trace] = span.get_trace_context
  end

  event.fingerprint = fingerprint
  event.level = level
  event.transaction = transaction_names.last
  event.breadcrumbs = breadcrumbs
  event.rack_env = rack_env if rack_env

  unless @event_processors.empty?
    @event_processors.each do |processor_block|
      event = processor_block.call(event, hint)
    end
  end

  event
end

#clearObject



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

def clear
  set_default_value
end

#clear_breadcrumbsObject



49
50
51
# File 'lib/sentry/scope.rb', line 49

def clear_breadcrumbs
  @breadcrumbs = BreadcrumbBuffer.new
end

#dupObject



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/sentry/scope.rb', line 53

def dup
  copy = super
  copy.breadcrumbs = breadcrumbs.dup
  copy.contexts = contexts.deep_dup
  copy.extra = extra.deep_dup
  copy.tags = tags.deep_dup
  copy.user = user.deep_dup
  copy.transaction_names = transaction_names.deep_dup
  copy.fingerprint = fingerprint.deep_dup
  copy.span = span.deep_dup
  copy
end

#get_spanObject



152
153
154
# File 'lib/sentry/scope.rb', line 152

def get_span
  span
end

#get_transactionObject



147
148
149
150
# File 'lib/sentry/scope.rb', line 147

def get_transaction
  # transaction will always be the first in the span_recorder
  span.span_recorder.spans.first if span
end

#set_context(key, value) ⇒ Object



131
132
133
# File 'lib/sentry/scope.rb', line 131

def set_context(key, value)
  @contexts.merge!(key => value)
end

#set_contexts(contexts_hash) ⇒ Object



126
127
128
129
# File 'lib/sentry/scope.rb', line 126

def set_contexts(contexts_hash)
  check_argument_type!(contexts_hash, Hash)
  @contexts = contexts_hash
end

#set_extra(key, value) ⇒ Object



113
114
115
# File 'lib/sentry/scope.rb', line 113

def set_extra(key, value)
  @extra.merge!(key => value)
end

#set_extras(extras_hash) ⇒ Object



108
109
110
111
# File 'lib/sentry/scope.rb', line 108

def set_extras(extras_hash)
  check_argument_type!(extras_hash, Hash)
  @extra.merge!(extras_hash)
end

#set_fingerprint(fingerprint) ⇒ Object



156
157
158
159
160
# File 'lib/sentry/scope.rb', line 156

def set_fingerprint(fingerprint)
  check_argument_type!(fingerprint, Array)

  @fingerprint = fingerprint
end

#set_level(level) ⇒ Object



135
136
137
# File 'lib/sentry/scope.rb', line 135

def set_level(level)
  @level = level
end

#set_rack_env(env) ⇒ Object



93
94
95
96
# File 'lib/sentry/scope.rb', line 93

def set_rack_env(env)
  env = env || {}
  @rack_env = env
end

#set_span(span) ⇒ Object



98
99
100
101
# File 'lib/sentry/scope.rb', line 98

def set_span(span)
  check_argument_type!(span, Span)
  @span = span
end

#set_tag(key, value) ⇒ Object



122
123
124
# File 'lib/sentry/scope.rb', line 122

def set_tag(key, value)
  @tags.merge!(key => value)
end

#set_tags(tags_hash) ⇒ Object



117
118
119
120
# File 'lib/sentry/scope.rb', line 117

def set_tags(tags_hash)
  check_argument_type!(tags_hash, Hash)
  @tags.merge!(tags_hash)
end

#set_transaction_name(transaction_name) ⇒ Object



139
140
141
# File 'lib/sentry/scope.rb', line 139

def set_transaction_name(transaction_name)
  @transaction_names << transaction_name
end

#set_user(user_hash) ⇒ Object



103
104
105
106
# File 'lib/sentry/scope.rb', line 103

def set_user(user_hash)
  check_argument_type!(user_hash, Hash)
  @user = user_hash
end

#transaction_nameObject



143
144
145
# File 'lib/sentry/scope.rb', line 143

def transaction_name
  @transaction_names.last
end

#update_from_options(contexts: nil, extra: nil, tags: nil, user: nil, level: nil, fingerprint: nil) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/sentry/scope.rb', line 77

def update_from_options(
  contexts: nil,
  extra: nil,
  tags: nil,
  user: nil,
  level: nil,
  fingerprint: nil
)
  self.contexts.merge!(contexts) if contexts
  self.extra.merge!(extra) if extra
  self.tags.merge!(tags) if tags
  self.user = user if user
  self.level = level if level
  self.fingerprint = fingerprint if fingerprint
end

#update_from_scope(scope) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/sentry/scope.rb', line 66

def update_from_scope(scope)
  self.breadcrumbs = scope.breadcrumbs
  self.contexts = scope.contexts
  self.extra = scope.extra
  self.tags = scope.tags
  self.user = scope.user
  self.transaction_names = scope.transaction_names
  self.fingerprint = scope.fingerprint
  self.span = scope.span
end