Skip to content

Commit 79c7c7b

Browse files
authored
feat: Add breadcrumb hints (#49)
* feat: Add breadcrumb hints * Simplify argument parsing logic
1 parent b174641 commit 79c7c7b

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

sentry_sdk/hub.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,28 +154,28 @@ def capture_internal_exception(self, exc_info):
154154
if client is not None and client.options["debug"]:
155155
logger.debug("Internal error in sentry_sdk", exc_info=exc_info)
156156

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

164-
if not kwargs and len(args) == 1 and callable(args[0]):
165-
crumb = args[0]()
166-
else:
167-
crumb = dict(*args, **kwargs)
168-
if crumb is None:
164+
crumb = dict(crumb or ())
165+
crumb.update(kwargs)
166+
if not crumb:
169167
return
170168

169+
hint = dict(hint or ())
170+
171171
if crumb.get("timestamp") is None:
172172
crumb["timestamp"] = datetime.utcnow()
173173
if crumb.get("type") is None:
174174
crumb["type"] = "default"
175175

176176
original_crumb = crumb
177177
if client.options["before_breadcrumb"] is not None:
178-
crumb = client.options["before_breadcrumb"](crumb)
178+
crumb = client.options["before_breadcrumb"](crumb, hint)
179179

180180
if crumb is not None:
181181
scope._breadcrumbs.append(crumb)

sentry_sdk/integrations/logging.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ def _emit(self, record):
9090
capture_event(event, hint=hint)
9191

9292
with _internal_exceptions():
93-
add_breadcrumb(self._breadcrumb_from_record(record))
93+
add_breadcrumb(
94+
self._breadcrumb_from_record(record), hint={"log_record": record}
95+
)
9496

9597
def _logging_to_event_level(self, levelname):
9698
return {"critical": "fatal"}.get(levelname.lower(), levelname.lower())

sentry_sdk/integrations/requests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ def _record_request(response):
3030
or None,
3131
"reason": response is not None and response.reason or None,
3232
},
33+
hint={"requests_response": response},
3334
)
3435

3536
try:

tests/test_basics.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def before_send(event):
5555
event["extra"] = {"foo": "bar"}
5656
return event
5757

58-
def before_breadcrumb(crumb):
58+
def before_breadcrumb(crumb, hint):
59+
assert hint == {"foo": 42}
5960
if not drop_breadcrumbs:
6061
crumb["data"] = {"foo": "bar"}
6162
return crumb
@@ -64,7 +65,7 @@ def before_breadcrumb(crumb):
6465
events = capture_events()
6566

6667
def do_this():
67-
add_breadcrumb(message="Hello")
68+
add_breadcrumb(message="Hello", hint={"foo": 42})
6869
try:
6970
raise ValueError("aha!")
7071
except Exception:
@@ -84,3 +85,22 @@ def do_this():
8485
assert crumb["message"] == "Hello"
8586
assert crumb["data"] == {"foo": "bar"}
8687
assert crumb["type"] == "default"
88+
89+
90+
def test_breadcrumb_arguments(sentry_init, capture_events):
91+
assert_hint = {"bar": 42}
92+
93+
def before_breadcrumb(crumb, hint):
94+
assert crumb["foo"] == 42
95+
assert hint == assert_hint
96+
97+
sentry_init(before_breadcrumb=before_breadcrumb)
98+
99+
add_breadcrumb(foo=42, hint=dict(bar=42))
100+
add_breadcrumb(dict(foo=42), dict(bar=42))
101+
add_breadcrumb(dict(foo=42), hint=dict(bar=42))
102+
add_breadcrumb(crumb=dict(foo=42), hint=dict(bar=42))
103+
104+
assert_hint.clear()
105+
add_breadcrumb(foo=42)
106+
add_breadcrumb(crumb=dict(foo=42))

0 commit comments

Comments
 (0)