Skip to content
Merged
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: 20 additions & 5 deletions app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,26 @@ def __init__(self):

def setup_sentry(self):
if os.getenv("ENV") == "production":
sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
traces_sample_rate=0.25,
profiles_sample_rate=1.0,
)
try:
# Explicitly configure integrations to avoid auto-enabling Strawberry
# which causes crashes when Strawberry is not installed
from sentry_sdk.integrations.fastapi import FastApiIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.integrations.stdlib import StdlibIntegration

sentry_sdk.init(
dsn=os.getenv("SENTRY_DSN"),
traces_sample_rate=0.25,
profiles_sample_rate=1.0,
default_integrations=False,
integrations=[
FastApiIntegration(),
LoggingIntegration(),
StdlibIntegration(),
],
)
except Exception as e:
logging.warning(f"Sentry initialization failed (non-fatal): {e}")
Comment on lines +62 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Good approach to make Sentry optional with explicit integrations.

The explicit integration configuration correctly prevents auto-enabling of optional integrations like Strawberry. The try-except wrapper makes initialization non-fatal as intended.

Regarding the static analysis hint about catching Exception: In this context, it's acceptable because:

  1. Sentry is an optional observability feature, not core functionality
  2. The exception is logged with context (line 81), not silently swallowed
  3. Making it non-fatal allows the application to continue without Sentry

However, ensure that after fixing the top-level import issue (line 5), the application starts successfully both with and without sentry-sdk installed:


🏁 Script executed:

#!/bin/bash
# Verify Sentry is truly optional by checking for any remaining hard dependencies

# Check for any other sentry_sdk imports at module level
rg -n "^import sentry_sdk|^from sentry_sdk" --type=py

# Check if sentry_sdk is in required dependencies (should be optional)
fd -e txt -e toml -e cfg --exec grep -l "sentry" {} \;

Length of output: 50



Let me check the exact state of the import at line 5 and the dependency status:



🏁 Script executed:

#!/bin/bash
# Get context around line 5 of app/main.py
head -n 15 app/main.py | tail -n +1

# Get requirements.txt content
cat ./requirements.txt

Length of output: 2170


🏁 Script executed:

#!/bin/bash
# View lines 1-20 of app/main.py to see the import context
sed -n '1,20p' app/main.py

# Check if sentry-sdk is optional in requirements.txt
grep -i "sentry" ./requirements.txt

Length of output: 828


Line 5 unconditional import prevents Sentry from being optional—fix required before trying approach is meaningful.

The module-level import sentry_sdk at line 5 will crash the app with an ImportError if sentry-sdk is not installed, bypassing the try-except block at lines 62–81. Since sentry-sdk[fastapi]==2.20.0 is currently a required dependency in requirements.txt, the initialization wrapping alone does not achieve the goal of optional Sentry.

To make Sentry truly optional:

  1. Wrap the import at line 5 in a try-except:

    try:
        import sentry_sdk
    except ImportError:
        sentry_sdk = None
  2. Add a guard before calling sentry_sdk.init() at line 62:

    if sentry_sdk is not None:
        try:
            # existing init code
  3. Mark sentry-sdk as an optional dependency in requirements.txt (if using extras) or document it as an optional add-on.

🧰 Tools
🪛 Ruff (0.14.5)

80-80: Do not catch blind exception: Exception

(BLE001)

🤖 Prompt for AI Agents
In app/main.py around lines 62 to 81 (and addressing the module-level import at
line 5), the module-level "import sentry_sdk" will raise ImportError when
sentry-sdk isn't installed and bypasses the runtime try/except around init; wrap
the top-level import in a try/except that sets sentry_sdk = None on ImportError,
then guard the block at lines 62-81 so you only call sentry_sdk.init(...) when
sentry_sdk is not None (i.e., skip initialization and keep the existing except
logging otherwise); finally, mark or document sentry-sdk as optional in
requirements.txt or extras so the dependency is not required by default.


def setup_phoenix_tracing(self):
try:
Expand Down