Skip to content
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
51 changes: 28 additions & 23 deletions src/psrt_ghsa_bot/_sentry_monitoring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
"""Sentry cron monitoring integration for the PSRT GHSA bot."""

import contextlib
import os
import time

import sentry_sdk
from sentry_sdk import crons
Expand All @@ -14,36 +16,39 @@

def init_sentry() -> None:
"""Initialize the Sentry SDK with the DSN from the env"""
dsn = os.environ.get("SENTRY_DSN")
if not dsn:
if dsn := os.environ.get("SENTRY_DSN"):
print("Initializing Sentry monitoring")
else:
print("No SENTRY_DSN set, skipping Sentry monitoring")
return

print("Initializing Sentry monitoring")
sentry_sdk.init(
dsn=dsn,
enable_tracing=False,
)


def capture_checkin(monitor_slug, status, duration=None, check_in_id=None) -> None | str:
"""Capture a Sentry cron check-in."""
if not os.environ.get("SENTRY_DSN"):
return None

@contextlib.contextmanager
def cron():
start_time = time.monotonic()
try:
return crons.capture_checkin(
monitor_slug=monitor_slug,
status=status,
duration=duration,
check_in_id=check_in_id,
check_in_id = crons.capture_checkin(
monitor_slug=MONITOR_SLUG_GHSA,
status=STATUS_IN_PROGRESS,
)
except ImportError, AttributeError:
return None


def capture_exception() -> None:
"""Capture an exception if Sentry is active."""
if not os.environ.get("SENTRY_DSN"):
return
sentry_sdk.capture_exception()
check_in_id = None
try:
yield
except Exception:
sentry_sdk.capture_exception()
status = STATUS_ERROR
raise
else:
status = STATUS_OK
finally:
if check_in_id is not None:
crons.capture_checkin(
monitor_slug=MONITOR_SLUG_GHSA,
status=status,
duration=time.monotonic() - start_time,
check_in_id=check_in_id,
)
48 changes: 15 additions & 33 deletions src/psrt_ghsa_bot/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import json
import os
import re
import time
import typing
import urllib.parse
from collections import abc
Expand All @@ -18,15 +17,7 @@
from githubkit.exception import RequestFailed, RequestError
from requests.exceptions import HTTPError, RequestException

from psrt_ghsa_bot._sentry_monitoring import (
MONITOR_SLUG_GHSA,
STATUS_ERROR,
STATUS_IN_PROGRESS,
STATUS_OK,
capture_checkin,
capture_exception,
init_sentry,
)
from psrt_ghsa_bot._sentry_monitoring import cron, init_sentry

load_dotenv()

Expand Down Expand Up @@ -87,7 +78,7 @@ def get_repository_advisories(
return
# Capture the original exception in Sentry (private)
# and emit a sanitized public exception.
capture_exception()
sentry_sdk.capture_exception()
raise RuntimeError("Request to paginate advisories failed.")


Expand Down Expand Up @@ -136,7 +127,7 @@ def credit_if_uncredited(login: str, type: str) -> None:
print(f" ⚠️ Pull requests not found for {private_fork_owner}/{private_fork_repo}!")
pull_requests = []
else:
capture_exception()
sentry_sdk.capture_exception()
raise RuntimeError("Request to list pull requests failed") from None

for pull_request in pull_requests:
Expand All @@ -158,7 +149,7 @@ def credit_if_uncredited(login: str, type: str) -> None:
f" ⚠️ Reviews not found for {private_fork_owner}/{private_fork_repo}#{pull_request['number']}!"
)
continue
capture_exception()
sentry_sdk.capture_exception()
raise RuntimeError("Request to list pull requests reviews failed") from None

for review in reviews:
Expand Down Expand Up @@ -208,16 +199,15 @@ def iter_cve_refs_with_tags(cve_api: CveApi, cve_id: str, tags: set[str]) -> abc
try:
cve = cve_api.show_cve_record(cve_id)
except HTTPError as err:
if sentry_sdk.is_initialized():
sentry_sdk.logger.warning(
"{cve_id} lookup returned {status_code} {reason}",
cve_id=cve_id,
status_code=err.response.status_code,
reason=err.response.reason,
)
sentry_sdk.logger.warning(
"{cve_id} lookup returned {status_code} {reason}",
cve_id=cve_id,
status_code=err.response.status_code,
reason=err.response.reason,
)
return
except RequestException:
capture_exception()
sentry_sdk.capture_exception()
return

try:
Expand All @@ -226,7 +216,7 @@ def iter_cve_refs_with_tags(cve_api: CveApi, cve_id: str, tags: set[str]) -> abc
except KeyError, TypeError:
# Note TypeError covers tests where cve_api is a mock without a
# return value for show_cve_record.
capture_exception()
sentry_sdk.capture_exception()
return

if cve_state != "PUBLISHED":
Expand Down Expand Up @@ -267,7 +257,7 @@ def apply_to_repo(github: GitHub, owner: str, repo: str, cve_api: CveApi, *, res
except RequestFailed:
# Capture the original exception in Sentry (private)
# and emit a sanitized public exception.
capture_exception()
sentry_sdk.capture_exception()
raise RuntimeError("Request to create a private fork failed") from None

# Keep draft GHSAs in sync with CVEs:
Expand Down Expand Up @@ -308,7 +298,7 @@ def apply_to_repo(github: GitHub, owner: str, repo: str, cve_api: CveApi, *, res
except RequestFailed:
# Capture the original exception in Sentry (private)
# and emit a sanitized public exception.
capture_exception()
sentry_sdk.capture_exception()
raise RuntimeError("Request to update advisory failed") from None
print(" 💾 Updated advisory")
else:
Expand Down Expand Up @@ -377,16 +367,8 @@ def run() -> None:
def main() -> None:
# Report the cron run to Sentry so we're alerted if it fails or stops running.
init_sentry()
check_in_id = capture_checkin(MONITOR_SLUG_GHSA, STATUS_IN_PROGRESS)
start_time = time.monotonic()
try:
with cron():
run()
except Exception:
capture_checkin(
MONITOR_SLUG_GHSA, STATUS_ERROR, duration=time.monotonic() - start_time, check_in_id=check_in_id
)
raise
capture_checkin(MONITOR_SLUG_GHSA, STATUS_OK, duration=time.monotonic() - start_time, check_in_id=check_in_id)


if __name__ == "__main__":
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import pytest
import sentry_sdk


@pytest.fixture(scope="session", autouse=True)
def sentry_init():
sentry_sdk.init(dsn=None, enable_tracing=False)
18 changes: 17 additions & 1 deletion tests/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from unittest import mock

import pytest
import requests
import requests.exceptions as req_exc

from psrt_ghsa_bot import app
Expand Down Expand Up @@ -495,7 +496,6 @@ def test_only_draft_ghsa_closed_on_cve(show_cve_response, state) -> None:
"errcls",
[
req_exc.RequestException,
req_exc.HTTPError,
req_exc.ConnectionError,
req_exc.ConnectTimeout,
req_exc.ReadTimeout,
Expand All @@ -506,3 +506,19 @@ def test_show_cve_errors_ignored(errcls, cve_id):
cve_api.show_cve_record.side_effect = errcls("test error")
assert list(app.iter_cve_refs_with_tags(cve_api, cve_id, {"patch"})) == []
cve_api.show_cve_record.assert_called()


@pytest.mark.parametrize("code", ["bad_request", "internal_server_error"])
def test_show_cve_bad_responses_logged(code, cve_id):
response = requests.Response()
response.status_code = requests.codes[code]
response.reason = code.replace("_", " ").title()
cve_api = mock.Mock()
cve_api.show_cve_record.side_effect = req_exc.HTTPError(response=response)
with mock.patch("sentry_sdk.logger.warning") as logger:
assert list(app.iter_cve_refs_with_tags(cve_api, cve_id, {"patch"})) == []
cve_api.show_cve_record.assert_called()
assert logger.call_args is not None
log_kwargs = logger.call_args.kwargs
assert log_kwargs.get("status_code") == response.status_code
assert log_kwargs.get("reason") == response.reason
64 changes: 24 additions & 40 deletions tests/test_sentry_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,54 @@
from psrt_ghsa_bot import _sentry_monitoring as sm
from psrt_ghsa_bot import app

DSN = "https://public@o0.ingest.sentry.io/0"


def test_noop_without_dsn(monkeypatch) -> None:
monkeypatch.delenv("SENTRY_DSN", raising=False)
with mock.patch.object(sm.sentry_sdk, "init") as init:
sm.init_sentry()
init.assert_not_called()

with mock.patch.object(sm.crons, "capture_checkin") as capture:
result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK)
assert result is None
capture.assert_not_called()


def test_init_with_dsn(monkeypatch) -> None:
monkeypatch.setenv("SENTRY_DSN", DSN)
@pytest.mark.parametrize("dsn", [None, "https://public@o0.ingest.sentry.example/0"])
def test_init_with_dsn(monkeypatch, dsn) -> None:
Comment thread
sethmlarson marked this conversation as resolved.
Resolved
if dsn:
monkeypatch.setenv("SENTRY_DSN", dsn)
else:
monkeypatch.delenv("SENTRY_DSN", raising=False)
with mock.patch.object(sm.sentry_sdk, "init") as init:
sm.init_sentry()
init.assert_called_once()
assert init.call_args.kwargs["dsn"] == DSN


def test_capture_checkin_with_dsn(monkeypatch) -> None:
monkeypatch.setenv("SENTRY_DSN", DSN)
with mock.patch.object(sm.crons, "capture_checkin", return_value="stanstan") as capture:
result = sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK, duration=1.5, check_in_id="stanstan")
assert result == "stanstan"
capture.assert_called_once_with(
monitor_slug=sm.MONITOR_SLUG_GHSA,
status=sm.STATUS_OK,
duration=1.5,
check_in_id="stanstan",
)


def test_capture_checkin_swallow_sdk_errors(monkeypatch) -> None:
monkeypatch.setenv("SENTRY_DSN", DSN)
with mock.patch.object(sm.crons, "capture_checkin", side_effect=AttributeError):
assert sm.capture_checkin(sm.MONITOR_SLUG_GHSA, sm.STATUS_OK) is None
assert init.call_args.kwargs["dsn"] == dsn


def test_main_reports_ok_on_success() -> None:
with (
mock.patch("psrt_ghsa_bot.app.init_sentry") as init,
mock.patch("psrt_ghsa_bot.app.run") as run,
mock.patch("psrt_ghsa_bot.app.capture_checkin", return_value="cid") as capture,
mock.patch("sentry_sdk.crons.capture_checkin", return_value="cid") as capture,
):
app.main()

init.assert_called_once()
run.assert_called_once()
assert [c.args[1] for c in capture.call_args_list] == [app.STATUS_IN_PROGRESS, app.STATUS_OK]
assert [c.kwargs["status"] for c in capture.call_args_list] == [sm.STATUS_IN_PROGRESS, sm.STATUS_OK]
assert capture.call_args_list[-1].kwargs["check_in_id"] == "cid"


def test_main_reports_error_and_reraises() -> None:
with (
mock.patch("psrt_ghsa_bot.app.init_sentry"),
mock.patch("psrt_ghsa_bot.app.run", side_effect=RuntimeError("uh oh")),
mock.patch("psrt_ghsa_bot.app.capture_checkin", return_value="cid") as capture,
mock.patch("sentry_sdk.crons.capture_checkin", return_value="cid") as capture,
):
with pytest.raises(RuntimeError, match="uh oh"):
app.main()

assert [c.args[1] for c in capture.call_args_list] == [app.STATUS_IN_PROGRESS, app.STATUS_ERROR]
assert [c.kwargs["status"] for c in capture.call_args_list] == [sm.STATUS_IN_PROGRESS, sm.STATUS_ERROR]
assert capture.call_args_list[-1].kwargs["check_in_id"] == "cid"


def test_main_runs_on_checkin_error() -> None:
with (
mock.patch("psrt_ghsa_bot.app.init_sentry") as init,
mock.patch("psrt_ghsa_bot.app.run") as run,
mock.patch("sentry_sdk.crons.capture_checkin", side_effect=AttributeError) as capture,
):
app.main()

init.assert_called_once()
run.assert_called_once()
capture.assert_called_once()
Loading