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

#initialize(max_breadcrumbs: nil) ⇒ Scope

Returns a new instance of Scope.



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

def initialize(max_breadcrumbs: nil)
  @max_breadcrumbs = max_breadcrumbs
  set_default_value
end

Class Method Details

.os_contextObject



194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/sentry/scope.rb', line 194

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



207
208
209
210
211
212
# File 'lib/sentry/scope.rb', line 207

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



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

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

#add_event_processor(&block) ⇒ Object



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

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

#apply_to_event(event, hint = nil) ⇒ Object



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

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



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

def clear
  set_default_value
end

#clear_breadcrumbsObject



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

def clear_breadcrumbs
  set_new_breadcrumb_buffer
end

#dupObject



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

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



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

def get_span
  span
end

#get_transactionObject



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

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



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

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

#set_contexts(contexts_hash) ⇒ Object



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

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

#set_extra(key, value) ⇒ Object



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

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

#set_extras(extras_hash) ⇒ Object



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

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

#set_fingerprint(fingerprint) ⇒ Object



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

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

  @fingerprint = fingerprint
end

#set_level(level) ⇒ Object



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

def set_level(level)
  @level = level
end

#set_rack_env(env) ⇒ Object



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

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

#set_span(span) ⇒ Object



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

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

#set_tag(key, value) ⇒ Object



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

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

#set_tags(tags_hash) ⇒ Object



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

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

#set_transaction_name(transaction_name) ⇒ Object



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

def set_transaction_name(transaction_name)
  @transaction_names << transaction_name
end

#set_user(user_hash) ⇒ Object



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

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

#transaction_nameObject



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

def transaction_name
  @transaction_names.last
end

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



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

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



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

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