Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion sentry_sdk/feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def add_feature_flag(flag, result):
Records a flag and its value to be sent on subsequent error events.
We recommend you do this on flag evaluations. Flags are buffered per Sentry scope.
"""
flags = sentry_sdk.get_current_scope().flags
flags = sentry_sdk.get_isolation_scope().flags
flags.set(flag, result)

span = sentry_sdk.get_current_span()
Expand Down
40 changes: 40 additions & 0 deletions tests/integrations/fastapi/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
from fastapi.testclient import TestClient
from fastapi.middleware.trustedhost import TrustedHostMiddleware

import sentry_sdk
from sentry_sdk import capture_message
from sentry_sdk.feature_flags import add_feature_flag
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.starlette import StarletteIntegration
Expand Down Expand Up @@ -714,3 +716,41 @@ async def subapp_route():
assert event["transaction"] == "/subapp"
else:
assert event["transaction"].endswith("subapp_route")


@pytest.mark.asyncio
async def test_feature_flags(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
integrations=[StarletteIntegration(), FastApiIntegration()],
)

events = capture_events()

app = FastAPI()

@app.get("/error")
async def _error():
add_feature_flag("hello", False)

with sentry_sdk.start_span(name="test-span"):
with sentry_sdk.start_span(name="test-span-2"):
raise ValueError("something is wrong!")

try:
client = TestClient(app)
client.get("/error")
except ValueError:
pass

found = False
for event in events:
if "exception" in event.keys():
assert event["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
found = True

assert found, "No event with exception found"
57 changes: 57 additions & 0 deletions tests/test_feature_flags.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,63 @@ def test_featureflags_integration(sentry_init, capture_events, uninstall_integra
}


@pytest.mark.asyncio
async def test_featureflags_integration_spans_async(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
)
events = capture_events()

add_feature_flag("hello", False)

try:
with sentry_sdk.start_span(name="test-span"):
with sentry_sdk.start_span(name="test-span-2"):
raise ValueError("something wrong!")
except ValueError as e:
sentry_sdk.capture_exception(e)

found = False
for event in events:
if "exception" in event.keys():
assert event["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
found = True

assert found, "No event with exception found"


def test_featureflags_integration_spans_sync(sentry_init, capture_events):
sentry_init(
traces_sample_rate=1.0,
)
events = capture_events()

add_feature_flag("hello", False)

try:
with sentry_sdk.start_span(name="test-span"):
with sentry_sdk.start_span(name="test-span-2"):
raise ValueError("something wrong!")
except ValueError as e:
sentry_sdk.capture_exception(e)

found = False
for event in events:
if "exception" in event.keys():
assert event["contexts"]["flags"] == {
"values": [
{"flag": "hello", "result": False},
]
}
found = True

assert found, "No event with exception found"


def test_featureflags_integration_threaded(
sentry_init, capture_events, uninstall_integration
):
Expand Down
Loading