Skip to content

Commit a7eec58

Browse files
authored
Fix #22: Add Sentry support (#24)
1 parent 3c1c5e8 commit a7eec58

File tree

6 files changed

+90
-23
lines changed

6 files changed

+90
-23
lines changed

poetry.lock

Lines changed: 45 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ atlassian-python-api = "^3.20.1"
1717
dockerflow = "2022.1.0"
1818
Jinja2 = "^3.0.3"
1919
pydantic-yaml = {extras = ["pyyaml","ruamel"], version = "^0.6.1"}
20+
sentry-sdk = "^1.5.7"
2021

2122

2223
[tool.poetry.dev-dependencies]
@@ -54,6 +55,7 @@ testpaths = [
5455
load-plugins='pylint_pytest'
5556
ignore='third_party'
5657
ignore-patterns = "tests/*"
58+
extension-pkg-whitelist = "pydantic"
5759

5860
[tool.isort]
5961
profile = "black"

src/app/api.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import time
66
from datetime import datetime
77

8+
import sentry_sdk
89
import uvicorn # type: ignore
910
from fastapi import FastAPI, Request
1011
from fastapi.responses import RedirectResponse
1112
from fastapi.staticfiles import StaticFiles
13+
from sentry_sdk.integrations.asgi import SentryAsgiMiddleware
1214

1315
from src.app.environment import get_settings
1416
from src.app.log import configure_logging
@@ -17,6 +19,8 @@
1719

1820
settings = get_settings()
1921

22+
configure_logging()
23+
2024
app = FastAPI(
2125
title="Jira Bugzilla Integration (JBI)",
2226
description="JBI v2 Platform",
@@ -27,6 +31,11 @@
2731
app.include_router(jbi_router)
2832
app.mount("/static", StaticFiles(directory="src/static"), name="static")
2933

34+
sentry_sdk.init( # pylint: disable=abstract-class-instantiated # noqa: E0110
35+
dsn=settings.sentry_dsn
36+
)
37+
app.add_middleware(SentryAsgiMiddleware)
38+
3039

3140
@app.get("/", include_in_schema=False)
3241
def root(request: Request):
@@ -61,12 +70,6 @@ async def request_summary(request: Request, call_next):
6170
return response
6271

6372

64-
@app.on_event("startup")
65-
def startup_event():
66-
"""On app startup perform these setup operations"""
67-
configure_logging()
68-
69-
7073
if __name__ == "__main__":
7174
uvicorn.run(
7275
"app:app", host=settings.host, port=settings.port, reload=settings.app_reload

src/app/environment.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import json
55
from functools import lru_cache
66
from pathlib import Path
7+
from typing import Optional
78

8-
from pydantic import BaseSettings
9+
from pydantic import AnyUrl, BaseSettings
10+
11+
12+
class SentryDsn(AnyUrl):
13+
"""Url type to validate Sentry DSN"""
914

1015

1116
class Settings(BaseSettings):
@@ -30,6 +35,9 @@ class Settings(BaseSettings):
3035
# Logging
3136
log_level: str = "info"
3237

38+
# Sentry
39+
sentry_dsn: Optional[SentryDsn]
40+
3341

3442
@lru_cache()
3543
def get_settings() -> Settings:

src/jbi/router.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,6 @@ def execute_action(request: BugzillaWebhookRequest, action_map, settings):
6666
except IgnoreInvalidRequestError as exception:
6767
invalid_logger.debug("ignore-invalid-request: %s", exception)
6868
return JSONResponse(content={"error": str(exception)}, status_code=202)
69-
except Exception as exception: # pylint: disable=broad-except
70-
# TODO: Remove when sentry is enabled # pylint: disable=fixme
71-
jbi_logger.info(
72-
"unknown-exception (%s): %s", type(exception), exception, exc_info=True
73-
)
74-
raise exception
7569

7670

7771
@api_router.post("/bugzilla_webhook")

tests/unit/app/test_sentry.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
Module for testing Sentry integration
3+
"""
4+
# pylint: disable=cannot-enumerate-pytest-fixtures
5+
6+
import json
7+
import os.path
8+
from unittest.mock import patch
9+
10+
import pytest
11+
12+
from src.jbi.bugzilla import BugzillaWebhookRequest
13+
14+
15+
def test_errors_are_reported_to_sentry(
16+
anon_client, webhook_request_example: BugzillaWebhookRequest
17+
):
18+
with patch("sentry_sdk.hub.Hub.capture_event") as mocked:
19+
with patch("src.jbi.router.execute_action", side_effect=ValueError):
20+
with pytest.raises(ValueError):
21+
anon_client.post(
22+
"/bugzilla_webhook", data=webhook_request_example.json()
23+
)
24+
25+
assert mocked.called, "Sentry captured the exception"

0 commit comments

Comments
 (0)