Skip to content

Commit 21f68e6

Browse files
committed
✨ Add sentry integration
1 parent 6a86317 commit 21f68e6

File tree

2 files changed

+48
-1
lines changed

2 files changed

+48
-1
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ dependencies = [
3939
"httpx >= 0.27.0,< 0.28.0",
4040
"rich-toolkit >= 0.12.0",
4141
"pydantic >= 1.6.1",
42+
"sentry-sdk",
4243
]
4344

4445
[project.optional-dependencies]

src/fastapi_cloud_cli/cli.py

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
1+
import sentry_sdk
12
import typer
3+
from sentry_sdk.integrations import Integration
4+
from sentry_sdk.utils import (
5+
capture_internal_exceptions,
6+
event_from_exception,
7+
)
28

39
from .commands.deploy import deploy
410
from .commands.env import env_app
511
from .commands.login import login
612
from .commands.whoami import whoami
713

8-
app = typer.Typer(rich_markup_mode="rich")
14+
15+
def _make_excepthook(old_excepthook):
16+
def sentry_sdk_excepthook(type_, value, traceback):
17+
integration = sentry_sdk.get_client().get_integration(TyperIntegration)
18+
19+
if integration is None:
20+
return old_excepthook(type_, value, traceback)
21+
22+
with capture_internal_exceptions():
23+
event, hint = event_from_exception(
24+
(type_, value, traceback),
25+
client_options=sentry_sdk.get_client().options,
26+
mechanism={"type": "excepthook", "handled": False},
27+
)
28+
sentry_sdk.capture_event(event, hint=hint)
29+
30+
return old_excepthook(type_, value, traceback)
31+
32+
return sentry_sdk_excepthook
33+
34+
35+
def patch_typer():
36+
typer.main.except_hook = _make_excepthook(typer.main.except_hook)
37+
38+
39+
class TyperIntegration(Integration):
40+
identifier = "typer"
41+
42+
@staticmethod
43+
def setup_once():
44+
patch_typer()
45+
46+
47+
sentry_sdk.init(
48+
dsn="https://230250605ea4b58a0b69c768e9ec1168@o4506985151856640.ingest.us.sentry.io/4508449198899200",
49+
traces_sample_rate=1.0,
50+
integrations=[TyperIntegration()],
51+
)
52+
53+
54+
app = typer.Typer()
955

1056

1157
# TODO: use the app structure

0 commit comments

Comments
 (0)