Skip to content

Commit

Permalink
Use capture_checkin instead of the decorator for Sentry healthchecks
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-z committed Apr 25, 2024
1 parent 1a1e361 commit c914341
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
from aiohttp import web
from sentry_sdk.integrations.asyncio import AsyncioIntegration
from sentry_sdk.integrations.logging import LoggingIntegration
from sentry_sdk.crons import monitor
from sentry_sdk.crons import capture_checkin
from sentry_sdk.crons.consts import MonitorStatus
from time import perf_counter, sleep, time


Expand Down Expand Up @@ -122,25 +123,22 @@ async def health_endpoint(_request):
# Ping Sentry at least every minute. Using a 30s buffer to be safe.
if IS_SENTRY_ENABLED and current_time - state["sentry_cron_last_ping_time"] > 30:
state["sentry_cron_last_ping_time"] = current_time
ping_sentry(success)
capture_checkin(
monitor_slug='discord-provisioner-bot',
status=MonitorStatus.OK if success else MonitorStatus.ERROR,
monitor_config={
"schedule": { "type": "interval", "value": 1, "unit": "minute" },
"checkin_margin": 5, # minutes
"max_runtime": 1, # minutes
"failure_issue_threshold": 1,
"recovery_threshold": 2,
}
)
logger.info(f"Pinged Sentry CRON with status {'OK' if success else 'ERROR'}")

if success:
return web.Response(text='OK')
else:
return web.Response(text='Client is closed!', status=500)

# Sentry CRON docs: https://docs.sentry.io/platforms/python/crons/
@monitor(monitor_slug='discord-provisioner-bot', monitor_config={
"schedule": { "type": "interval", "value": 1, "unit": "minute" },
"checkin_margin": 5, # minutes
"max_runtime": 1, # minutes
"failure_issue_threshold": 1,
"recovery_threshold": 2,
})
def ping_sentry(success):
if success:
logger.info("Pinged Sentry CRON")
else:
raise Exception(f"ping_sentry: Healthcheck failed!")

client.run(os.environ['DISCORD_TOKEN'])

0 comments on commit c914341

Please sign in to comment.