Skip to content

Commit 32e7a05

Browse files
authored
ref: Event processors always overwrite (getsentry#225)
1 parent e41cf23 commit 32e7a05

File tree

12 files changed

+132
-132
lines changed

12 files changed

+132
-132
lines changed

sentry_sdk/integrations/aiohttp.py

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,24 +66,16 @@ def aiohttp_processor(event, hint):
6666

6767
request_info = event.setdefault("request", {})
6868

69-
if "url" not in request_info:
70-
request_info["url"] = "%s://%s%s" % (
71-
request.scheme,
72-
request.host,
73-
request.path,
74-
)
75-
76-
if "query_string" not in request_info:
77-
request_info["query_string"] = request.query_string
78-
79-
if "method" not in request_info:
80-
request_info["method"] = request.method
81-
82-
if "env" not in request_info:
83-
request_info["env"] = {"REMOTE_ADDR": request.remote}
69+
request_info["url"] = "%s://%s%s" % (
70+
request.scheme,
71+
request.host,
72+
request.path,
73+
)
8474

85-
if "headers" not in request_info:
86-
request_info["headers"] = _filter_headers(dict(request.headers))
75+
request_info["query_string"] = request.query_string
76+
request_info["method"] = request.method
77+
request_info["env"] = {"REMOTE_ADDR": request.remote}
78+
request_info["headers"] = _filter_headers(dict(request.headers))
8779

8880
return event
8981

sentry_sdk/integrations/aws_lambda.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,25 +130,32 @@ def event_processor(event, hint):
130130

131131
request = event.setdefault("request", {})
132132

133-
if "httpMethod" in aws_event and "method" not in request:
133+
if "httpMethod" in aws_event:
134134
request["method"] = aws_event["httpMethod"]
135-
if "url" not in request:
136-
request["url"] = _get_url(aws_event, aws_context)
137-
if "queryStringParameters" in aws_event and "query_string" not in request:
135+
136+
request["url"] = _get_url(aws_event, aws_context)
137+
138+
if "queryStringParameters" in aws_event:
138139
request["query_string"] = aws_event["queryStringParameters"]
139-
if "headers" in aws_event and "headers" not in request:
140+
141+
if "headers" in aws_event:
140142
request["headers"] = _filter_headers(aws_event["headers"])
143+
141144
if aws_event.get("body", None):
142145
# Unfortunately couldn't find a way to get structured body from AWS
143146
# event. Meaning every body is unstructured to us.
144147
request["data"] = AnnotatedValue("", {"rem": [["!raw", "x", 0, 0]]})
145148

146149
if _should_send_default_pii():
147150
user_info = event.setdefault("user", {})
148-
if "id" not in user_info:
149-
user_info["id"] = aws_event.get("identity", {}).get("userArn")
150-
if "ip_address" not in user_info:
151-
user_info["ip_address"] = aws_event.get("identity", {}).get("sourceIp")
151+
152+
id = aws_event.get("identity", {}).get("userArn")
153+
if id is not None:
154+
user_info["id"] = id
155+
156+
ip = aws_event.get("identity", {}).get("sourceIp")
157+
if ip is not None:
158+
user_info["ip_address"] = ip
152159

153160
return event
154161

sentry_sdk/integrations/celery.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def _handle_task_prerun(sender, task, args, kwargs, **_):
4848
def _make_event_processor(args, kwargs, task):
4949
def event_processor(event, hint):
5050
with capture_internal_exceptions():
51-
if "transaction" not in event:
52-
event["transaction"] = task.name
51+
event["transaction"] = task.name
5352

5453
with capture_internal_exceptions():
5554
extra = event.setdefault("extra", {})

sentry_sdk/integrations/django/__init__.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,16 +122,15 @@ def event_processor(event, hint):
122122
if request is None:
123123
return event
124124

125-
if "transaction" not in event:
126-
try:
127-
if integration.transaction_style == "function_name":
128-
event["transaction"] = transaction_from_function(
129-
resolve(request.path).func
130-
)
131-
elif integration.transaction_style == "url":
132-
event["transaction"] = LEGACY_RESOLVER.resolve(request.path)
133-
except Exception:
134-
pass
125+
try:
126+
if integration.transaction_style == "function_name":
127+
event["transaction"] = transaction_from_function(
128+
resolve(request.path).func
129+
)
130+
elif integration.transaction_style == "url":
131+
event["transaction"] = LEGACY_RESOLVER.resolve(request.path)
132+
except Exception:
133+
pass
135134

136135
with capture_internal_exceptions():
137136
DjangoRequestExtractor(request).extract_into_event(event)
@@ -185,23 +184,20 @@ def _set_user_info(request, event):
185184
if user is None or not is_authenticated(user):
186185
return
187186

188-
if "id" not in user_info:
189-
try:
190-
user_info["id"] = str(user.pk)
191-
except Exception:
192-
pass
187+
try:
188+
user_info["id"] = str(user.pk)
189+
except Exception:
190+
pass
193191

194-
if "email" not in user_info:
195-
try:
196-
user_info["email"] = user.email
197-
except Exception:
198-
pass
192+
try:
193+
user_info["email"] = user.email
194+
except Exception:
195+
pass
199196

200-
if "username" not in user_info:
201-
try:
202-
user_info["username"] = user.get_username()
203-
except Exception:
204-
pass
197+
try:
198+
user_info["username"] = user.get_username()
199+
except Exception:
200+
pass
205201

206202

207203
class _FormatConverter(object):

sentry_sdk/integrations/flask.py

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,13 @@ def inner(event, hint):
118118
if request is None:
119119
return event
120120

121-
if "transaction" not in event:
122-
try:
123-
if integration.transaction_style == "endpoint":
124-
event["transaction"] = request.url_rule.endpoint
125-
elif integration.transaction_style == "url":
126-
event["transaction"] = request.url_rule.rule
127-
except Exception:
128-
pass
121+
try:
122+
if integration.transaction_style == "endpoint":
123+
event["transaction"] = request.url_rule.endpoint
124+
elif integration.transaction_style == "url":
125+
event["transaction"] = request.url_rule.rule
126+
except Exception:
127+
pass
129128

130129
with capture_internal_exceptions():
131130
FlaskRequestExtractor(request).extract_into_event(event)
@@ -166,13 +165,12 @@ def _add_user_to_event(event):
166165

167166
user_info = event.setdefault("user", {})
168167

169-
if "id" not in user_info:
170-
try:
171-
user_info["id"] = user.get_id()
172-
# TODO: more configurable user attrs here
173-
except AttributeError:
174-
# might happen if:
175-
# - flask_login could not be imported
176-
# - flask_login is not configured
177-
# - no user is logged in
178-
pass
168+
try:
169+
user_info["id"] = user.get_id()
170+
# TODO: more configurable user attrs here
171+
except AttributeError:
172+
# might happen if:
173+
# - flask_login could not be imported
174+
# - flask_login is not configured
175+
# - no user is logged in
176+
pass

sentry_sdk/integrations/modules.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ def setup_once():
3232
@add_global_event_processor
3333
def processor(event, hint):
3434
if Hub.current.get_integration(ModulesIntegration) is not None:
35-
if "modules" not in event:
36-
event["modules"] = dict(_get_installed_modules())
35+
event["modules"] = dict(_get_installed_modules())
3736
return event

sentry_sdk/integrations/pyramid.py

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -138,23 +138,21 @@ def event_processor(event, hint):
138138
if request is None:
139139
return event
140140

141-
if "transaction" not in event:
142-
try:
143-
if integration.transaction_style == "route_name":
144-
event["transaction"] = request.matched_route.name
145-
elif integration.transaction_style == "route_pattern":
146-
event["transaction"] = request.matched_route.pattern
147-
except Exception:
148-
pass
141+
try:
142+
if integration.transaction_style == "route_name":
143+
event["transaction"] = request.matched_route.name
144+
elif integration.transaction_style == "route_pattern":
145+
event["transaction"] = request.matched_route.pattern
146+
except Exception:
147+
pass
149148

150149
with capture_internal_exceptions():
151150
PyramidRequestExtractor(request).extract_into_event(event)
152151

153152
if _should_send_default_pii():
154153
with capture_internal_exceptions():
155154
user_info = event.setdefault("user", {})
156-
if "id" not in user_info:
157-
user_info["id"] = authenticated_userid(request)
155+
user_info["id"] = authenticated_userid(request)
158156

159157
return event
160158

sentry_sdk/integrations/rq.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,7 @@ def event_processor(event, hint):
5656
job = weak_job()
5757
if job is not None:
5858
with capture_internal_exceptions():
59-
if "transaction" not in event:
60-
event["transaction"] = job.func_name
59+
event["transaction"] = job.func_name
6160

6261
with capture_internal_exceptions():
6362
extra = event.setdefault("extra", {})

sentry_sdk/integrations/sanic.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -125,24 +125,16 @@ def sanic_processor(event, hint):
125125
request_info = event["request"]
126126
urlparts = urlparse.urlsplit(request.url)
127127

128-
if "url" not in request_info:
129-
request_info["url"] = "%s://%s%s" % (
130-
urlparts.scheme,
131-
urlparts.netloc,
132-
urlparts.path,
133-
)
134-
135-
if "query_string" not in request_info:
136-
request_info["query_string"] = urlparts.query
137-
138-
if "method" not in request_info:
139-
request_info["method"] = request.method
140-
141-
if "env" not in request_info:
142-
request_info["env"] = {"REMOTE_ADDR": request.remote_addr}
143-
144-
if "headers" not in request_info:
145-
request_info["headers"] = _filter_headers(dict(request.headers))
128+
request_info["url"] = "%s://%s%s" % (
129+
urlparts.scheme,
130+
urlparts.netloc,
131+
urlparts.path,
132+
)
133+
134+
request_info["query_string"] = urlparts.query
135+
request_info["method"] = request.method
136+
request_info["env"] = {"REMOTE_ADDR": request.remote_addr}
137+
request_info["headers"] = _filter_headers(dict(request.headers))
146138

147139
return event
148140

sentry_sdk/integrations/tornado.py

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -92,35 +92,26 @@ def tornado_processor(event, hint):
9292

9393
request = handler.request
9494

95-
if "transaction" not in event:
96-
with capture_internal_exceptions():
97-
method = getattr(handler, handler.request.method.lower())
98-
event["transaction"] = transaction_from_function(method)
95+
with capture_internal_exceptions():
96+
method = getattr(handler, handler.request.method.lower())
97+
event["transaction"] = transaction_from_function(method)
9998

10099
with capture_internal_exceptions():
101100
extractor = TornadoRequestExtractor(request)
102101
extractor.extract_into_event(event)
103102

104103
request_info = event["request"]
105104

106-
if "url" not in request_info:
107-
request_info["url"] = "%s://%s%s" % (
108-
request.protocol,
109-
request.host,
110-
request.path,
111-
)
112-
113-
if "query_string" not in request_info:
114-
request_info["query_string"] = request.query
115-
116-
if "method" not in request_info:
117-
request_info["method"] = request.method
118-
119-
if "env" not in request_info:
120-
request_info["env"] = {"REMOTE_ADDR": request.remote_ip}
105+
request_info["url"] = "%s://%s%s" % (
106+
request.protocol,
107+
request.host,
108+
request.path,
109+
)
121110

122-
if "headers" not in request_info:
123-
request_info["headers"] = _filter_headers(dict(request.headers))
111+
request_info["query_string"] = request.query
112+
request_info["method"] = request.method
113+
request_info["env"] = {"REMOTE_ADDR": request.remote_ip}
114+
request_info["headers"] = _filter_headers(dict(request.headers))
124115

125116
with capture_internal_exceptions():
126117
if handler.current_user and _should_send_default_pii():

sentry_sdk/integrations/wsgi.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -189,14 +189,13 @@ def event_processor(event, hint):
189189

190190
if _should_send_default_pii():
191191
user_info = event.setdefault("user", {})
192-
if "ip_address" not in user_info:
193-
user_info.setdefault("ip_address", client_ip)
194-
195-
request_info.setdefault("url", request_url)
196-
request_info.setdefault("query_string", query_string)
197-
request_info.setdefault("method", method)
198-
request_info.setdefault("env", env)
199-
request_info.setdefault("headers", headers)
192+
user_info["ip_address"] = client_ip
193+
194+
request_info["url"] = request_url
195+
request_info["query_string"] = query_string
196+
request_info["method"] = method
197+
request_info["env"] = env
198+
request_info["headers"] = headers
200199

201200
return event
202201

tests/test_basics.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
push_scope,
88
configure_scope,
99
capture_exception,
10+
capture_message,
1011
add_breadcrumb,
1112
last_event_id,
1213
Hub,
@@ -271,3 +272,32 @@ def test_scope_popped_too_soon(sentry_init, caplog):
271272
record, = (x for x in caplog.records if x.levelname == "ERROR")
272273

273274
assert record.message == ("Scope popped too soon. Popped 1 scopes too many.")
275+
276+
277+
def test_scope_event_processor_order(sentry_init, capture_events):
278+
def before_send(event, hint):
279+
event["message"] += "baz"
280+
return event
281+
282+
sentry_init(debug=True, before_send=before_send)
283+
events = capture_events()
284+
285+
with push_scope() as scope:
286+
287+
@scope.add_event_processor
288+
def foo(event, hint):
289+
event["message"] += "foo"
290+
return event
291+
292+
with push_scope() as scope:
293+
294+
@scope.add_event_processor
295+
def bar(event, hint):
296+
event["message"] += "bar"
297+
return event
298+
299+
capture_message("hi")
300+
301+
event, = events
302+
303+
assert event["message"] == "hifoobarbaz"

0 commit comments

Comments
 (0)