diff --git a/Dockerfile b/Dockerfile index 80f6d6e..6c76228 100644 --- a/Dockerfile +++ b/Dockerfile @@ -4,6 +4,8 @@ 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 @@ -11,4 +13,6 @@ 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"] diff --git a/README.md b/README.md index ee9ca90..29e32a3 100644 --- a/README.md +++ b/README.md @@ -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= -p 5000:5000 sentry-tunnel ``` Below is the original README: diff --git a/app.py b/app.py index b3a4647..186a7fa 100644 --- a/app.py +++ b/app.py @@ -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()]) @@ -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 {