Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
sentry-python/sentry_sdk/scope.py at master · pythonthings/sentry-python · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
Uh oh!
There was an error while loading.
Please reload this page
.
pythonthings
/
sentry-python
Public
forked from
getsentry/sentry-python
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Files
Expand file tree
master
Breadcrumbs
sentry-python
/
sentry_sdk
/
scope.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
271 lines (211 loc) · 7.95 KB
master
Breadcrumbs
sentry-python
/
sentry_sdk
/
scope.py
Copy path
Top
File metadata and controls
Code
Blame
271 lines (211 loc) · 7.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
from
copy
import
copy
from
collections
import
deque
from
functools
import
wraps
from
itertools
import
chain
from
sentry_sdk
.
utils
import
logger
,
capture_internal_exceptions
MYPY
=
False
if
MYPY
:
from
typing
import
Any
from
typing
import
Dict
from
typing
import
Optional
from
typing
import
Deque
from
typing
import
List
from
typing
import
Callable
from
typing
import
TypeVar
from
sentry_sdk
.
utils
import
Breadcrumb
,
Event
,
EventProcessor
,
ErrorProcessor
,
Hint
F
=
TypeVar
(
"F"
,
bound
=
Callable
[...,
Any
])
global_event_processors
=
[]
# type: List[EventProcessor]
def
add_global_event_processor
(
processor
):
# type: (EventProcessor) -> None
global_event_processors
.
append
(
processor
)
def
_attr_setter
(
fn
):
return
property
(
fset
=
fn
,
doc
=
fn
.
__doc__
)
def
_disable_capture
(
fn
):
# type: (F) -> F
@
wraps
(
fn
)
def
wrapper
(
self
,
*
args
,
**
kwargs
):
# type: (Any, *Dict[str, Any], **Any) -> Any
if
not
self
.
_should_capture
:
return
try
:
self
.
_should_capture
=
False
return
fn
(
self
,
*
args
,
**
kwargs
)
finally
:
self
.
_should_capture
=
True
return
wrapper
# type: ignore
class
Scope
(
object
):
"""The scope holds extra information that should be sent with all
events that belong to it.
"""
__slots__
=
(
"_level"
,
"_name"
,
"_fingerprint"
,
"_transaction"
,
"_user"
,
"_tags"
,
"_contexts"
,
"_extras"
,
"_breadcrumbs"
,
"_event_processors"
,
"_error_processors"
,
"_should_capture"
,
"_span"
,
)
def
__init__
(
self
):
# type: () -> None
self
.
_event_processors
=
[]
# type: List[EventProcessor]
self
.
_error_processors
=
[]
# type: List[ErrorProcessor]
self
.
_name
=
None
# type: Optional[str]
self
.
clear
()
@
_attr_setter
def
level
(
self
,
value
):
"""When set this overrides the level."""
self
.
_level
=
value
@
_attr_setter
def
fingerprint
(
self
,
value
):
"""When set this overrides the default fingerprint."""
self
.
_fingerprint
=
value
@
_attr_setter
def
transaction
(
self
,
value
):
"""When set this forces a specific transaction name to be set."""
self
.
_transaction
=
value
@
_attr_setter
def
user
(
self
,
value
):
"""When set a specific user is bound to the scope."""
self
.
_user
=
value
def
set_span_context
(
self
,
span_context
):
"""Sets the span context."""
self
.
_span
=
span_context
def
set_tag
(
self
,
key
,
value
):
# type: (str, Any) -> None
"""Sets a tag for a key to a specific value."""
self
.
_tags
[
key
]
=
value
def
remove_tag
(
self
,
key
):
# type: (str) -> None
"""Removes a specific tag."""
self
.
_tags
.
pop
(
key
,
None
)
def
set_context
(
self
,
key
,
value
):
# type: (str, Any) -> None
"""Binds a context at a certain key to a specific value."""
self
.
_contexts
[
key
]
=
value
def
remove_context
(
self
,
key
):
# type: (str) -> None
"""Removes a context."""
self
.
_contexts
.
pop
(
key
,
None
)
def
set_extra
(
self
,
key
,
value
):
# type: (str, Any) -> None
"""Sets an extra key to a specific value."""
self
.
_extras
[
key
]
=
value
def
remove_extra
(
self
,
key
):
# type: (str) -> None
"""Removes a specific extra key."""
self
.
_extras
.
pop
(
key
,
None
)
def
clear
(
self
):
# type: () -> None
"""Clears the entire scope."""
self
.
_level
=
None
self
.
_fingerprint
=
None
self
.
_transaction
=
None
self
.
_user
=
None
self
.
_tags
=
{}
# type: Dict[str, Any]
self
.
_contexts
=
{}
# type: Dict[str, Dict[str, Any]]
self
.
_extras
=
{}
# type: Dict[str, Any]
self
.
clear_breadcrumbs
()
self
.
_should_capture
=
True
self
.
_span
=
None
def
clear_breadcrumbs
(
self
):
# type: () -> None
"""Clears breadcrumb buffer."""
self
.
_breadcrumbs
=
deque
()
# type: Deque[Breadcrumb]
def
add_event_processor
(
self
,
func
):
# type: (EventProcessor) -> None
""""Register a scope local event processor on the scope.
This function behaves like `before_send.`
"""
self
.
_event_processors
.
append
(
func
)
def
add_error_processor
(
self
,
func
,
cls
=
None
):
# type: (ErrorProcessor, Optional[type]) -> None
""""Register a scope local error processor on the scope.
The error processor works similar to an event processor but is
invoked with the original exception info triple as second argument.
"""
if
cls
is
not
None
:
cls_
=
cls
# For mypy.
real_func
=
func
def
func
(
event
,
exc_info
):
try
:
is_inst
=
isinstance
(
exc_info
[
1
],
cls_
)
except
Exception
:
is_inst
=
False
if
is_inst
:
return
real_func
(
event
,
exc_info
)
return
event
self
.
_error_processors
.
append
(
func
)
@
_disable_capture
def
apply_to_event
(
self
,
event
,
hint
):
# type: (Event, Hint) -> Optional[Event]
"""Applies the information contained on the scope to the given event."""
def
_drop
(
event
,
cause
,
ty
):
# type: (Dict[str, Any], Any, str) -> Optional[Any]
logger
.
info
(
"%s (%s) dropped event (%s)"
,
ty
,
cause
,
event
)
return
None
if
self
.
_level
is
not
None
:
event
[
"level"
]
=
self
.
_level
event
.
setdefault
(
"breadcrumbs"
, []).
extend
(
self
.
_breadcrumbs
)
if
event
.
get
(
"user"
)
is
None
and
self
.
_user
is
not
None
:
event
[
"user"
]
=
self
.
_user
if
event
.
get
(
"transaction"
)
is
None
and
self
.
_transaction
is
not
None
:
event
[
"transaction"
]
=
self
.
_transaction
if
event
.
get
(
"fingerprint"
)
is
None
and
self
.
_fingerprint
is
not
None
:
event
[
"fingerprint"
]
=
self
.
_fingerprint
if
self
.
_extras
:
event
.
setdefault
(
"extra"
, {}).
update
(
self
.
_extras
)
if
self
.
_tags
:
event
.
setdefault
(
"tags"
, {}).
update
(
self
.
_tags
)
if
self
.
_contexts
:
event
.
setdefault
(
"contexts"
, {}).
update
(
self
.
_contexts
)
if
self
.
_span
is
not
None
:
event
.
setdefault
(
"contexts"
, {})[
"trace"
]
=
{
"trace_id"
:
self
.
_span
.
trace_id
,
"span_id"
:
self
.
_span
.
span_id
,
}
exc_info
=
hint
.
get
(
"exc_info"
)
if
exc_info
is
not
None
:
for
error_processor
in
self
.
_error_processors
:
new_event
=
error_processor
(
event
,
exc_info
)
if
new_event
is
None
:
return
_drop
(
event
,
error_processor
,
"error processor"
)
event
=
new_event
for
event_processor
in
chain
(
global_event_processors
,
self
.
_event_processors
):
new_event
=
event
with
capture_internal_exceptions
():
new_event
=
event_processor
(
event
,
hint
)
if
new_event
is
None
:
return
_drop
(
event
,
event_processor
,
"event processor"
)
event
=
new_event
return
event
def
__copy__
(
self
):
# type: () -> Scope
rv
=
object
.
__new__
(
self
.
__class__
)
# type: Scope
rv
.
_level
=
self
.
_level
rv
.
_name
=
self
.
_name
rv
.
_fingerprint
=
self
.
_fingerprint
rv
.
_transaction
=
self
.
_transaction
rv
.
_user
=
self
.
_user
rv
.
_tags
=
dict
(
self
.
_tags
)
rv
.
_contexts
=
dict
(
self
.
_contexts
)
rv
.
_extras
=
dict
(
self
.
_extras
)
rv
.
_breadcrumbs
=
copy
(
self
.
_breadcrumbs
)
rv
.
_event_processors
=
list
(
self
.
_event_processors
)
rv
.
_error_processors
=
list
(
self
.
_error_processors
)
rv
.
_should_capture
=
self
.
_should_capture
rv
.
_span
=
self
.
_span
return
rv
def
__repr__
(
self
):
# type: () -> str
return
"<%s id=%s name=%s>"
%
(
self
.
__class__
.
__name__
,
hex
(
id
(
self
)),
self
.
_name
,
)
You can’t perform that action at this time.