Skip to content
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
14 changes: 7 additions & 7 deletions sentry_sdk/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,28 +154,28 @@ def capture_internal_exception(self, exc_info):
if client is not None and client.options["debug"]:
logger.debug("Internal error in sentry_sdk", exc_info=exc_info)

def add_breadcrumb(self, *args, **kwargs):
def add_breadcrumb(self, crumb=None, hint=None, **kwargs):
"""Adds a breadcrumb."""
client, scope = self._stack[-1]
if client is None:
logger.info("Dropped breadcrumb because no client bound")
return

if not kwargs and len(args) == 1 and callable(args[0]):
crumb = args[0]()
else:
crumb = dict(*args, **kwargs)
if crumb is None:
crumb = dict(crumb or ())
crumb.update(kwargs)
if not crumb:
return

hint = dict(hint or ())

if crumb.get("timestamp") is None:
crumb["timestamp"] = datetime.utcnow()
if crumb.get("type") is None:
crumb["type"] = "default"

original_crumb = crumb
if client.options["before_breadcrumb"] is not None:
crumb = client.options["before_breadcrumb"](crumb)
crumb = client.options["before_breadcrumb"](crumb, hint)

if crumb is not None:
scope._breadcrumbs.append(crumb)
Expand Down
4 changes: 3 additions & 1 deletion sentry_sdk/integrations/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ def _emit(self, record):
capture_event(event, hint=hint)

with _internal_exceptions():
add_breadcrumb(self._breadcrumb_from_record(record))
add_breadcrumb(
self._breadcrumb_from_record(record), hint={"log_record": record}
)

def _logging_to_event_level(self, levelname):
return {"critical": "fatal"}.get(levelname.lower(), levelname.lower())
Expand Down
1 change: 1 addition & 0 deletions sentry_sdk/integrations/requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def _record_request(response):
or None,
"reason": response is not None and response.reason or None,
},
hint={"requests_response": response},
)

try:
Expand Down
24 changes: 22 additions & 2 deletions tests/test_basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ def before_send(event):
event["extra"] = {"foo": "bar"}
return event

def before_breadcrumb(crumb):
def before_breadcrumb(crumb, hint):
assert hint == {"foo": 42}
if not drop_breadcrumbs:
crumb["data"] = {"foo": "bar"}
return crumb
Expand All @@ -64,7 +65,7 @@ def before_breadcrumb(crumb):
events = capture_events()

def do_this():
add_breadcrumb(message="Hello")
add_breadcrumb(message="Hello", hint={"foo": 42})
try:
raise ValueError("aha!")
except Exception:
Expand All @@ -84,3 +85,22 @@ def do_this():
assert crumb["message"] == "Hello"
assert crumb["data"] == {"foo": "bar"}
assert crumb["type"] == "default"


def test_breadcrumb_arguments(sentry_init, capture_events):
assert_hint = {"bar": 42}

def before_breadcrumb(crumb, hint):
assert crumb["foo"] == 42
assert hint == assert_hint

sentry_init(before_breadcrumb=before_breadcrumb)

add_breadcrumb(foo=42, hint=dict(bar=42))
add_breadcrumb(dict(foo=42), dict(bar=42))
add_breadcrumb(dict(foo=42), hint=dict(bar=42))
add_breadcrumb(crumb=dict(foo=42), hint=dict(bar=42))

assert_hint.clear()
add_breadcrumb(foo=42)
add_breadcrumb(crumb=dict(foo=42))