Skip to content

Commit

Permalink
Fix test_breadcrumbs
Browse files Browse the repository at this point in the history
  • Loading branch information
sl0thentr0py committed Nov 15, 2024
1 parent aa9c5ca commit bf84550
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 69 deletions.
6 changes: 4 additions & 2 deletions sentry_sdk/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,10 @@ def __init__(

# Prepopulate some attrs so that they're accessible in traces_sampler
attributes = attributes or {}
attributes[SentrySpanAttribute.OP] = op
attributes[SentrySpanAttribute.SOURCE] = source
if op is not None:
attributes[SentrySpanAttribute.OP] = op
if source is not None:
attributes[SentrySpanAttribute.SOURCE] = source
if sampled is not None:
attributes[SentrySpanAttribute.CUSTOM_SAMPLED] = sampled

Expand Down
80 changes: 13 additions & 67 deletions tests/test_breadcrumbs.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from unittest import mock

import sentry_sdk
from sentry_sdk.consts import OP


def test_breadcrumbs(sentry_init, capture_events):
Expand All @@ -26,7 +25,7 @@ def test_breadcrumbs(sentry_init, capture_events):
},
}

with sentry_sdk.start_transaction(name="trx-breadcrumbs"):
with sentry_sdk.start_span(name="trx-breadcrumbs"):
sentry_sdk.add_breadcrumb(message="breadcrumb0", **add_breadcrumbs_kwargs)

with sentry_sdk.start_span(name="span1", op="function"):
Expand All @@ -37,41 +36,25 @@ def test_breadcrumbs(sentry_init, capture_events):
message="breadcrumb2", **add_breadcrumbs_kwargs
)

# Spans that create breadcrumbs automatically
with sentry_sdk.start_span(name="span3", op=OP.DB_REDIS) as span3:
span3.set_data("span3_data", "data on the redis span")
span3.set_tag("span3_tag", "tag on the redis span")

with sentry_sdk.start_span(name="span4", op=OP.HTTP_CLIENT) as span4:
span4.set_data("span4_data", "data on the http.client span")
span4.set_tag("span4_tag", "tag on the http.client span")

with sentry_sdk.start_span(name="span5", op=OP.SUBPROCESS) as span5:
span5.set_data("span5_data", "data on the subprocess span")
span5.set_tag("span5_tag", "tag on the subprocess span")

with sentry_sdk.start_span(name="span6", op="function") as span6:
# This data on the span is not added to custom breadcrumbs.
# Data from the span is only added to automatic breadcrumbs shown above
span6.set_data("span6_data", "data on span6")
span6.set_tag("span6_tag", "tag on the span6")
with sentry_sdk.start_span(name="span3", op="function"):
sentry_sdk.add_breadcrumb(
message="breadcrumb6", **add_breadcrumbs_kwargs
message="breadcrumb3", **add_breadcrumbs_kwargs
)

try:
1 / 0
except ZeroDivisionError as ex:
sentry_sdk.capture_exception(ex)

(error,) = events
assert len(events) == 2
error = events[0]

breadcrumbs = error["breadcrumbs"]["values"]

for crumb in breadcrumbs:
print(crumb)

assert len(breadcrumbs) == 7
assert len(breadcrumbs) == 4

# Check for my custom breadcrumbs
for i in range(0, 3):
Expand All @@ -88,53 +71,16 @@ def test_breadcrumbs(sentry_init, capture_events):
}
assert breadcrumbs[i]["timestamp"] == mock.ANY

# Check automatic redis breadcrumbs
assert breadcrumbs[3]["message"] == "span3"
assert breadcrumbs[3]["type"] == "redis"
assert breadcrumbs[3]["category"] == "redis"
assert "level" not in breadcrumbs[3]
assert "origin" not in breadcrumbs[3]
# Check for custom breadcrumbs on span3
assert breadcrumbs[3]["message"] == "breadcrumb3"
assert breadcrumbs[3]["type"] == "navigation"
assert breadcrumbs[3]["category"] == "unit_tests.breadcrumbs"
assert breadcrumbs[3]["level"] == "fatal"
assert breadcrumbs[3]["origin"] == "unit-tests"
assert breadcrumbs[3]["data"] == {
"span3_tag": "tag on the redis span",
}
assert breadcrumbs[3]["timestamp"] == mock.ANY

# Check automatic http.client breadcrumbs
assert "message" not in breadcrumbs[4]
assert breadcrumbs[4]["type"] == "http"
assert breadcrumbs[4]["category"] == "httplib"
assert "level" not in breadcrumbs[4]
assert "origin" not in breadcrumbs[4]
assert breadcrumbs[4]["data"] == {
"thread.id": mock.ANY,
"thread.name": mock.ANY,
"span4_data": "data on the http.client span",
}
assert breadcrumbs[4]["timestamp"] == mock.ANY

# Check automatic subprocess breadcrumbs
assert breadcrumbs[5]["message"] == "span5"
assert breadcrumbs[5]["type"] == "subprocess"
assert breadcrumbs[5]["category"] == "subprocess"
assert "level" not in breadcrumbs[5]
assert "origin" not in breadcrumbs[5]
assert breadcrumbs[5]["data"] == {
"thread.id": mock.ANY,
"thread.name": mock.ANY,
"span5_data": "data on the subprocess span",
}
assert breadcrumbs[5]["timestamp"] == mock.ANY

# Check for custom breadcrumbs on span6
assert breadcrumbs[6]["message"] == "breadcrumb6"
assert breadcrumbs[6]["type"] == "navigation"
assert breadcrumbs[6]["category"] == "unit_tests.breadcrumbs"
assert breadcrumbs[6]["level"] == "fatal"
assert breadcrumbs[6]["origin"] == "unit-tests"
assert breadcrumbs[6]["data"] == {
"string": "foobar",
"number": 4.2,
"array": [1, 2, 3],
"dict": {"foo": "bar"},
}
assert breadcrumbs[6]["timestamp"] == mock.ANY
assert breadcrumbs[3]["timestamp"] == mock.ANY

0 comments on commit bf84550

Please sign in to comment.