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
25 changes: 7 additions & 18 deletions sentry_sdk/integrations/pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
from sentry_sdk.integrations.wsgi import SentryWsgiMiddleware
from sentry_sdk.scope import should_send_default_pii
from sentry_sdk.traces import SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE
from sentry_sdk.tracing import SOURCE_FOR_STYLE as TRANSACTION_SOURCE_FOR_STYLE
from sentry_sdk.tracing_utils import has_span_streaming_enabled
from sentry_sdk.utils import (
capture_internal_exceptions,
ensure_integration_enabled,
Expand Down Expand Up @@ -81,16 +79,15 @@ def sentry_patched_call_view(

scope = sentry_sdk.get_isolation_scope()

if has_span_streaming_enabled(client.options):
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["user_info"]:
user_id = request.authenticated_userid
if user_id:
scope.set_user({"id": user_id})
elif should_send_default_pii():
if has_data_collection_enabled(client.options):
if client.options["data_collection"]["user_info"]:
user_id = request.authenticated_userid
if user_id:
scope.set_user({"id": user_id})
elif should_send_default_pii():
user_id = request.authenticated_userid
if user_id:
scope.set_user({"id": user_id})

scope.add_event_processor(
_make_event_processor(weakref.ref(request), integration)
Expand Down Expand Up @@ -168,17 +165,9 @@ def _set_transaction_name_and_source(
"route_name": request.matched_route.name,
"route_pattern": request.matched_route.pattern,
}
is_span_streaming_enabled = has_span_streaming_enabled(
sentry_sdk.get_client().options
)
source = (
SEGMENT_SOURCE_FOR_STYLE[transaction_style]
if is_span_streaming_enabled
else TRANSACTION_SOURCE_FOR_STYLE[transaction_style]
)
scope.set_transaction_name(
name_for_style[transaction_style],
source=source,
source=SEGMENT_SOURCE_FOR_STYLE[transaction_style],
)
except Exception:
pass
Expand Down
100 changes: 35 additions & 65 deletions tests/integrations/pyramid/test_pyramid.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ def hi2(request):
assert event["transaction"] == "hi2"


@pytest.mark.parametrize("span_streaming", [True, False])
@pytest.mark.parametrize(
"url,transaction_style,expected_transaction,expected_source",
[
Expand All @@ -128,39 +127,30 @@ def hi2(request):
def test_transaction_style(
sentry_init,
get_client,
capture_events,
capture_items,
url,
transaction_style,
expected_transaction,
expected_source,
span_streaming,
):
sentry_init(
integrations=[PyramidIntegration(transaction_style=transaction_style)],
traces_sample_rate=1.0,
trace_lifecycle="stream" if span_streaming else "static",
trace_lifecycle="stream",
)

if span_streaming:
items = capture_items("event", "span")
else:
events = capture_events()
items = capture_items("event", "span")

client = get_client()
client.get(url)

if span_streaming:
sentry_sdk.flush()
spans = [i.payload for i in items if i.type == "span"]
assert len(spans) == 1
(segment,) = spans
assert segment["name"] == expected_transaction
assert segment["attributes"]["sentry.segment.name.source"] == expected_source
else:
(_, transaction_event) = events
assert transaction_event["transaction"] == expected_transaction
assert transaction_event["transaction_info"] == {"source": expected_source}
sentry_sdk.flush()

spans = [i.payload for i in items if i.type == "span"]
assert len(spans) == 1
(segment,) = spans
assert segment["name"] == expected_transaction
assert segment["attributes"]["sentry.segment.name.source"] == expected_source


@pytest.mark.parametrize("max_value_length", [1024, None])
Expand Down Expand Up @@ -457,20 +447,19 @@ def index(request):
assert not errors


@pytest.mark.parametrize("span_streaming", [True, False])
def test_tracing_error(
sentry_init, capture_events, capture_items, route, get_client, span_streaming
sentry_init,
capture_items,
route,
get_client,
):
sentry_init(
integrations=[PyramidIntegration()],
traces_sample_rate=1.0,
trace_lifecycle="stream" if span_streaming else "static",
trace_lifecycle="stream",
)

if span_streaming:
items = capture_items("event", "span")
else:
events = capture_events()
items = capture_items("event", "span")

@route("/tracing-error")
def tracing_error(request):
Expand All @@ -480,66 +469,47 @@ def tracing_error(request):
with pytest.raises(ZeroDivisionError):
client.get("/tracing-error")

if span_streaming:
sentry_sdk.flush()
spans = [i.payload for i in items if i.type == "span"]
error_events = [i.payload for i in items if i.type == "event"]

assert len(spans) == 1
assert len(error_events) == 1

(segment,) = spans
(error_event,) = error_events
sentry_sdk.flush()
spans = [i.payload for i in items if i.type == "span"]
error_events = [i.payload for i in items if i.type == "event"]

assert segment["name"] == "tracing_error"
assert segment["status"] == SpanStatus.ERROR
assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid"
assert len(spans) == 1
assert len(error_events) == 1

assert error_event["exception"]["values"][-1]["type"] == "ZeroDivisionError"
assert error_event["exception"]["values"][-1]["mechanism"]["type"] == "pyramid"
else:
error_event, transaction_event = events
(segment,) = spans
(error_event,) = error_events

assert transaction_event["type"] == "transaction"
assert transaction_event["transaction"] == "tracing_error"
assert transaction_event["contexts"]["trace"]["status"] == "internal_error"
assert segment["name"] == "tracing_error"
assert segment["status"] == SpanStatus.ERROR
assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid"

assert error_event["exception"]["values"][-1]["type"] == "ZeroDivisionError"
assert error_event["exception"]["values"][-1]["mechanism"]["type"] == "pyramid"
assert error_event["exception"]["values"][-1]["type"] == "ZeroDivisionError"
assert error_event["exception"]["values"][-1]["mechanism"]["type"] == "pyramid"


@pytest.mark.parametrize("span_streaming", [True, False])
def test_span_origin(
sentry_init,
pyramid_config,
capture_events,
capture_items,
get_client,
span_streaming,
):
sentry_init(
integrations=[PyramidIntegration()],
traces_sample_rate=1.0,
trace_lifecycle="stream" if span_streaming else "static",
trace_lifecycle="stream",
)

if span_streaming:
items = capture_items("event", "span")
else:
events = capture_events()
items = capture_items("event", "span")

client = get_client()
client.get("/message")

if span_streaming:
sentry_sdk.flush()
spans = [i.payload for i in items if i.type == "span"]
assert len(spans) == 1
(segment,) = spans
assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid"
else:
(_, event) = events
assert event["contexts"]["trace"]["origin"] == "auto.http.pyramid"
sentry_sdk.flush()

spans = [i.payload for i in items if i.type == "span"]
assert len(spans) == 1
(segment,) = spans
assert segment["attributes"]["sentry.origin"] == "auto.http.pyramid"


@pytest.mark.parametrize("init_kwargs, expect_user", DATA_COLLECTION_USER_INFO_CASES)
Expand Down
Loading