Skip to content

Commit 51c82e5

Browse files
committed
✨ Add sentry integration
1 parent 6a86317 commit 51c82e5

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-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: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,61 @@
1+
from typing import BaseException, Callable, TracebackType, Type
2+
3+
import sentry_sdk
14
import typer
5+
from sentry_sdk.integrations import Integration
6+
from sentry_sdk.utils import (
7+
capture_internal_exceptions,
8+
event_from_exception,
9+
)
210

311
from .commands.deploy import deploy
412
from .commands.env import env_app
513
from .commands.login import login
614
from .commands.whoami import whoami
715

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

1060

1161
# TODO: use the app structure

0 commit comments

Comments
 (0)