8000 ref: Event processors always overwrite by untitaker · Pull Request #225 · getsentry/sentry-python · GitHub
[go: up one dir, main page]

Skip to content

ref: Event processors always overwrite #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions sentry_sdk/integrations/aiohttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,16 @@ def aiohttp_processor(event, hint):

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

if "url" not in request_info:
request_info["url"] = "%s://%s%s" % (
request.scheme,
request.host,
request.path,
)

if "query_string" not in request_info:
request_info["query_string"] = request.query_string

if "method" not in request_info:
request_info["method"] = request.method

if "env" not in request_info:
request_info["env"] = {"REMOTE_ADDR": request.remote}
request_info["url"] = "%s://%s%s" % (
request.scheme,
request.host,
request.path,
)

if "headers" not in request_info:
request_info["headers"] = _filter_headers(dict(request.headers))
request_info["query_string"] = request.query_string
request_info["method"] = request.method
request_info["env"] = {"REMOTE_ADDR": request.remote}
request_info["headers"] = _filter_headers(dict(request.headers))

return event

Expand Down
25 changes: 16 additions & 9 deletions sentry_sdk/integrations/aws_lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,25 +130,32 @@ def event_processor(event, hint):

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

if "httpMethod" in aws_event and "method" not in request:
if "httpMethod" in aws_event:
request["method"] = aws_event["httpMethod"]
if "url" not in request:
request["url"] = _get_url(aws_event, aws_context)
if "queryStringParameters" in aws_event and "query_string" not in request:

request["url"] = _get_url(aws_event, aws_context)

if "queryStringParameters" in aws_event:
request["query_string"] = aws_event["queryStringParameters"]
if "headers" in aws_event and "headers" not in request:

if "headers" in aws_event:
request["headers"] = _filter_headers(aws_event["headers"])

if aws_event.get("body", None):
# Unfortunately couldn't find a way to get structured body from AWS
# event. Meaning every body is unstructured to us.
request["data"] = AnnotatedValue("", {"rem": [["!raw", "x", 0, 0]]})

if _should_send_default_pii():
user_info = event.setdefault("user", {})
if "id" not in user_info:
user_info["id"] = aws_event.get("identity", {}).get("userArn")
if "ip_address" not in user_info:
user_info["ip_address"] = aws_event.get("identity", {}).get("sourceIp")

id = aws_event.get("identity", {}).get("userArn")
if id is not None:
user_info["id"] = id

ip = aws_event.get("identity", {}).get("sourceIp")
if ip is not None:
user_info["ip_address"] = ip

return event

Expand Down
3 changes: 1 addition & 2 deletions sentry_sdk/integrations/celery.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def _handle_task_prerun(sender, task, args, kwargs, **_):
def _make_event_processor(args, kwargs, task):
def event_processor(event, hint):
with capture_internal_exceptions():
if "transaction" not in event:
event["transaction"] = task.name
event["transaction"] = task.name

with capture_internal_exceptions():
extra = event.setdefault("extra", {})
Expand Down
46 changes: 21 additions & 25 deletions sentry_sdk/integrations/django/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,15 @@ def event_processor(event, hint):
if request is None:
return event

if "transaction" not in event:
try:
if integration.transaction_style == "function_name":
event["transaction"] = transaction_from_function(
resolve(request.path).func
)
elif integration.transaction_style == "url":
event["transaction"] = LEGACY_RESOLVER.resolve(request.path)
except Exception:
pass
try:
if integration.transaction_style == "function_name":
event["transaction"] = transaction_from_function(
resolve(request.path).func
)
elif integration.transaction_style == "url":
event["transaction"] = LEGACY_RESOLVER.resolve(request.path)
except Exception:
pass

with capture_internal_exceptions():
DjangoRequestExtractor(request).extract_into_event(event)
Expand Down Expand Up @@ -185,23 +184,20 @@ def _set_user_info(request, event):
if user is None or not is_authenticated(user):
return

if "id" not in user_info:
try:
user_info["id"] = str(user.pk)
except Exception:
pass
try:
user_info["id"] = str(user.pk)
except Exception:
pass

if "email" not in user_info:
try:
user_info["email"] = user.email
except Exception:
pass
try:
user_info["email"] = user.email
except Exception:
pass

if "username" not in user_info:
try:
user_info["username"] = user.get_username()
except Exception:
pass
try:
user_info["username"] = user.get_username()
except Exception:
pass


class _FormatConverter(object):
Expand Down
34 changes: 16 additions & 18 deletions sentry_sdk/integrations/flask.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,13 @@ def inner(event, hint):
if request is None:
return event

if "transaction" not in event:
try:
if integration.transaction_style == "endpoint":
event["transaction"] = request.url_rule.endpoint
elif integration.transaction_style == "url":
event["transaction"] = request.url_rule.rule
except Exception:
pass
try:
if integration.transaction_style == "endpoint":
event["transaction"] = request.url_rule.endpoint
elif integration.transaction_style == "url":
event["transaction"] = request.url_rule.rule
except Exception:
pass

with capture_internal_exceptions():
FlaskRequestExtractor(request).extract_into_event(event)
Expand Down Expand Up @@ -166,13 +165,12 @@ def _add_user_to_event(event):

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

if "id" not in user_info:
try:
user_info["id"] = user.get_id()
# TODO: more configurable user attrs here
except AttributeError:
# might happen if:
# - flask_login could not be imported
# - flask_login is not configured
# - no user is logged in
pass
try:
user_info["id"] = user.get_id()
# TODO: more configurable user attrs here
except AttributeError:
# might happen if:
# - flask_login could not be imported
# - flask_login is not configured
# - no user is logged in
pass
3 changes: 1 addition & 2 deletions sentry_sdk/integrations/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,5 @@ def setup_once():
@add_global_event_processor
def processor(event, hint):
if Hub.current.get_integration(ModulesIntegration) is not None:
if "modules" not in event:
event["modules"] = dict(_get_installed_modules())
event["modules"] = dict(_get_installed_modules())
return event
18 changes: 8 additions & 10 deletions sentry_sdk/integrations/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,23 +138,21 @@ def event_processor(event, hint):
if request is None:
return event

if "transaction" not in event:
try:
if integration.transaction_style == "route_name":
event["transaction"] = request.matched_route.name
elif integration.transaction_style == "route_pattern":
event["transaction"] = request.matched_route.pattern
except Exception:
pass
try:
if integration.transaction_style == "route_name":
event["transaction"] = request.matched_route.name
elif integration.transaction_style == "route_pattern":
event["transaction"] = request.matched_route.pattern
except Exception:
pass

with capture_internal_exceptions():
PyramidRequestExtractor(request).extract_into_event(event)

if _should_send_default_pii():
with capture_internal_exceptions():
user_info = event.setdefault("user", {})
if "id" not in user_info:
user_info["id"] = authenticated_userid(request)
user_info["id"] = authenticated_userid(request)

return event

Expand Down
3 changes: 1 addition & 2 deletions sentry_sdk/integrations/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ def event_processor(event, hint):
job = weak_job()
if job is not None:
with capture_internal_exceptions():
if "transaction" not in event:
event["transaction"] = job.func_name
event["transaction"] = job.func_name

with capture_internal_exceptions():
extra = event.setdefault("extra", {})
Expand Down
28 changes: 10 additions & 18 deletions sentry_sdk/integrations/sanic.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,16 @@ def sanic_processor(event, hint):
request_info = event["request"]
urlparts = urlparse.urlsplit(request.url)

if "url" not in request_info:
request_info["url"] = "%s://%s%s" % (
urlparts.scheme,
urlparts.netloc,
urlparts.path,
)

if "query_string" not in request_info:
request_info["query_string"] = urlparts.query

if "method" not in request_info:
request_info["method"] = request.method

if "env" not in request_info:
request_info["env"] = {"REMOTE_ADDR": request.remote_addr}

if "headers" not in request_info:
request_info["headers"] = _filter_headers(dict(request.headers))
request_info["url"] = "%s://%s%s" % (
urlparts.scheme,
urlparts.netloc,
urlparts.path,
)

request_info["query_string"] = urlparts.query
request_info["method"] = request.method
request_info["env"] = {"REMOTE_ADDR": request.remote_addr}
request_info["headers"] = _filter_headers(dict(request.headers))

return event

Expand Down
33 changes: 12 additions & 21 deletions sentry_sdk/integrations/tornado.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,35 +92,26 @@ def tornado_processor(event, hint):

request = handler.request

if "transaction" not in event:
with capture_internal_exceptions():
method = getattr(handler, handler.request.method.lower())
event["transaction"] = transaction_from_function(method)
with capture_internal_exceptions():
method = getattr(handler, handler.request.method.lower())
event["transaction"] = transaction_from_function(method)

with capture_internal_exceptions():
extractor = TornadoRequestExtractor(request)
extractor.extract_into_event(event)

request_info = event["request"]

if "url" not in request_info:
request_info["url"] = "%s://%s%s" % (
request.protocol,
request.host,
request.path,
)

if "query_string" not in request_info:
request_info["query_string"] = request.query

if "method" not in request_info:
request_info["method"] = request.method

if "env" not in request_info:
request_info["env"] = {"REMOTE_ADDR": request.remote_ip}
request_info["url"] = "%s://%s%s" % (
request.protocol,
request.host,
request.path,
)

if "headers" not in request_info:
request_info["headers"] = _filter_headers(dict(request.headers))
request_info["query_string"] = request.query
request_info["method"] = request.method
request_info["env"] = {"REMOTE_ADDR": request.remote_ip}
request_info["headers"] = _filter_headers(dict(request.headers))

with capture_internal_exceptions():
if handler.current_user and _should_send_default_pii():
Expand Down
15 changes: 7 additions & 8 deletions sentry_sdk/integrations/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,13 @@ def event_processor(event, hint):

if _should_send_default_pii():
user_info = event.setdefault("user", {})
if "ip_address" not in user_info:
user_info.setdefault("ip_address", client_ip)

request_info.setdefault("url", request_url)
request_info.setdefault("query_string", query_string)
request_info.setdefault("method", method)
request_info.setdefault("env", env)
request_info.setdefault("headers", headers)
user_info["ip_address"] = client_ip

request_info["url"] = request_url
request_info["query_string"] = query_string
request_info["method"] = method
request_info["env"] = env
request_info["headers"] = headers

return event

Expand Down
30 changes: 30 additions & 0 deletions tests/test_basics.py
< 58E2 tr data-hunk="3d47147a5667dcbb7c13d5378e67822a4c21b8c9e95e1c54d8a05893f9edfcab" class="show-top-border">
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
push_scope,
configure_scope,
capture_exception,
capture_message,
add_breadcrumb,
last_event_id,
Hub,
Expand Down Expand Up @@ -271,3 +272,32 @@ def test_scope_popped_too_soon(sentry_init, caplog):
record, = (x for x in caplog.records if x.levelname == "ERROR")

assert record.message == ("Scope popped too soon. Popped 1 scopes too many.")


def test_scope_event_processor_order(sentry_init, capture_events):
def before_send(event, hint):
event["message"] += "baz"
return event

sentry_init(debug=True, before_send=before_send)
events = capture_events()

with push_scope() as scope:

@scope.add_event_processor
def foo(event, hint):
event["message"] += "foo"
return event

with push_scope() as scope:

@scope.add_event_processor
def bar(event, hint):
event["message"] += "bar"
return event

capture_message("hi")

event, = events

assert event["message"] == "hifoobarbaz"
0