Skip to content

Commit

Permalink
Add Sentry CRON and /runtime-info endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ben-z committed Apr 24, 2024
1 parent b54d89f commit 223cd82
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
46 changes: 42 additions & 4 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,22 @@
# It looks like:
# {"tags":["ghcr.io/watonomous/repo-ingestion:main"],"labels":{"org.opencontainers.image.title":"repo-ingestion","org.opencontainers.image.description":"Simple server to receive file changes and open GitHub pull requests","org.opencontainers.image.url":"https://github.com/WATonomous/repo-ingestion","org.opencontainers.image.source":"https://github.com/WATonomous/repo-ingestion","org.opencontainers.image.version":"main","org.opencontainers.image.created":"2024-01-20T16:10:39.421Z","org.opencontainers.image.revision":"1d55b62b15c78251e0560af9e97927591e260a98","org.opencontainers.image.licenses":""}}
BUILD_INFO=json.loads(os.getenv("DOCKER_METADATA_OUTPUT_JSON", "{}"))

IS_SENTRY_ENABLED = os.getenv("SENTRY_DSN") is not None

# Set up Sentry
if os.getenv("SENTRY_DSN"):
if IS_SENTRY_ENABLED:
build_labels = BUILD_INFO.get("labels", {})
image_title = build_labels.get("org.opencontainers.image.title", "unknown_image")
image_version = build_labels.get("org.opencontainers.image.version", "unknown_version")
image_rev = build_labels.get("org.opencontainers.image.revision", "unknown_rev")

sentry_config = {
"dsn": os.getenv("SENTRY_DSN"),
"environment": os.getenv("SENTRY_ENVIRONMENT", "unknown"),
"dsn": os.environ["SENTRY_DSN"],
"environment": os.getenv("DEPLOYMENT_ENVIRONMENT", "unknown"),
"release": os.getenv("SENTRY_RELEASE", f'{image_title}:{image_version}@{image_rev}'),
}

logging.info(f"Sentry SDK version: {sentry_sdk.VERSION}")
logging.info(f"Sentry DSN found. Setting up Sentry with config: {sentry_config}")

sentry_logging = LoggingIntegration(
Expand Down Expand Up @@ -92,9 +93,16 @@ def split_dsn(dsn):

app = flask.Flask(__name__)
CORS(app)
state = {
"sentry_cron_last_ping_time": 0,
"num_tunnel_requests_received": 0,
"num_tunnel_requests_success": 0,
}

@app.route("/tunnel", methods=["POST"])
def tunnel():
state["num_tunnel_requests_received"] += 1

try:
logging.debug(f"Request headers: {request.headers}")
logging.debug(f"Remote addr: {request.remote_addr}")
Expand Down Expand Up @@ -129,6 +137,8 @@ def tunnel():
# handle exception in your preferred style,
# e.g. by logging or forwarding to Sentry
logging.exception(e)
else:
state["num_tunnel_requests_success"] += 1

return {}

Expand All @@ -138,8 +148,36 @@ 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:
state["sentry_cron_last_ping_time"] = current_time
ping_sentry()

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():
logger.info("Pinged Sentry CRON")

@app.get("/runtime-info")
def read_runtime_info():
return {
"sentry_enabled": IS_SENTRY_ENABLED,
"sentry_sdk_version": sentry_sdk.VERSION,
"deployment_environment": os.getenv("DEPLOYMENT_ENVIRONMENT", "unknown"),
"sentry_cron_last_ping_time": state["sentry_cron_last_ping_time"],
"num_tunnel_requests_received": state["num_tunnel_requests_received"],
"num_tunnel_requests_success": state["num_tunnel_requests_success"],
}

if __name__ == "__main__":
from waitress import serve
serve(app, host=HOST, port=PORT)
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ flask>=3.0.0
flask-cors>=3.0.10
requests>=2.26.0
waitress>=2.1.2
sentry-sdk[flask]>=1.32.0
sentry-sdk[flask]>=1.45.0

0 comments on commit 223cd82

Please sign in to comment.