Skip to content

Commit

Permalink
Simplify Sentry CRON using capture_checkin
Browse files Browse the repository at this point in the history
Use the `capture_checkin` function instead of the `monitor` decorator
to reduce the number of checkins sent to Sentry. This is because a lot
of the Sentry healthchecks are timing out.
  • Loading branch information
ben-z committed Apr 25, 2024
1 parent 23a0f10 commit c7764fa
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@ FROM python:3.11.5-slim
ARG DOCKER_METADATA_OUTPUT_JSON='{}'
ENV DOCKER_METADATA_OUTPUT_JSON=${DOCKER_METADATA_OUTPUT_JSON}

RUN apt-get update && apt-get install -y curl

COPY requirements.txt /app/
WORKDIR /app

RUN pip install -r requirements.txt

COPY . /app

HEALTHCHECK --interval=10s --timeout=5s --retries=3 CMD curl --fail http://localhost:5000/health || exit 1

CMD ["python", "/app/app.py"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This repo is derived from the [official example](https://github.com/getsentry/ex

```bash
docker build -t sentry-tunnel .
docker run --rm -it --name sentry-tunnel -v $(pwd):/app -e ALLOWED_SENTRY_HOSTS=o123123123.ingest.sentry.io -e ALLOWED_SENTRY_PROJECT_IDS=456456456 -e PORT=5001 -p 5001:5001 sentry-tunnel
docker run --rm -it --name sentry-tunnel -v $(pwd):/app -e ALLOWED_SENTRY_HOSTS=o123123123.ingest.sentry.io -e ALLOWED_SENTRY_PROJECT_IDS=456456456 -e DEPLOYMENT_ENVIRONMENT=dev -e SENTRY_DSN=<optional> -p 5000:5000 sentry-tunnel
```

Below is the original README:
Expand Down
33 changes: 17 additions & 16 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import requests
import sentry_sdk
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

ALLOWED_SENTRY_HOSTS = set([s.strip() for s in os.environ.get("ALLOWED_SENTRY_HOSTS", "").split(",") if s.strip()])
ALLOWED_SENTRY_PROJECT_IDS = set([s.strip() for s in os.environ.get("ALLOWED_SENTRY_PROJECT_IDS", "").split(",") if s.strip()])
Expand Down Expand Up @@ -151,24 +151,25 @@ def build_info():
@app.route("/health")
def health():
current_time = time.time()
# 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:
success = True
# Assuming the /health endpoint is called every 10 seconds, ping Sentry at least every minute.
if IS_SENTRY_ENABLED and current_time - state["sentry_cron_last_ping_time"] > 50:
state["sentry_cron_last_ping_time"] = current_time
ping_sentry()
capture_checkin(
monitor_slug='sentry-tunnel',
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,
}
)
logging.info(f"Pinged Sentry CRON with status {'OK' if success else 'ERROR'}")

return "OK"

# Sentry CRON docs: https://docs.sentry.io/platforms/python/crons/
@monitor(monitor_slug='sentry-tunnel', 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():
logging.info("Pinged Sentry CRON")

@app.get("/runtime-info")
def read_runtime_info():
return {
Expand Down

0 comments on commit c7764fa

Please sign in to comment.