Skip to content

Fix feature flags in potel #4353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 5, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/test-integrations-ai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7","3.9","3.11","3.12"]
python-version: ["3.9","3.11","3.12"]
os: [ubuntu-22.04]
steps:
- uses: actions/[email protected]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test-integrations-misc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7","3.8","3.10","3.11","3.12","3.13"]
python-version: ["3.7","3.8","3.9","3.10","3.11","3.12","3.13"]
os: [ubuntu-22.04]
steps:
- uses: actions/[email protected]
Expand Down
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 @@ -671,3 +673,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