diff --git a/app/api/health.py b/app/api/health.py index 3ef97bc..6e505a2 100644 --- a/app/api/health.py +++ b/app/api/health.py @@ -6,7 +6,7 @@ import httpx from fastapi import APIRouter, HTTPException, Request, Response from ..core import db -from .. import catalog, grade_rules, mail +from .. import catalog, grade_rules, mail, observability from ..core.auth import client_ip, owner, new_session, rate_limit from ..core.limits import upload_limit_bytes from ..core.pricing import manual_extra_cents @@ -66,7 +66,7 @@ def session(request: Request, response: Response): # Public by design: the browser initialises Sentry with these; empty is off. 'sentry_dsn_web': os.environ.get('SENTRY_DSN_WEB', '').strip(), 'sentry_environment': os.environ.get('SENTRY_ENVIRONMENT') or ENVIRONMENT, - 'sentry_release': os.environ.get('IMAGE_TAG', ''), + 'sentry_release': observability.release() or '', # Public by design: Mercado Pago's card form needs it in the browser. 'payment_public_key': os.environ.get('MP_PUBLIC_KEY', '') if payment.name == 'mercadopago' else '', # The delivery options the cart quotes, or none: pickup only. diff --git a/app/api/users.py b/app/api/users.py index 8a52e42..1a9e41b 100644 --- a/app/api/users.py +++ b/app/api/users.py @@ -14,7 +14,7 @@ from uuid import UUID, uuid4 from fastapi import APIRouter, Depends, HTTPException, Request from pydantic import Field, field_validator -from .. import machines, mail +from .. import machines, mail, observability from ..core import db from ..core.auth import (CAPABILITIES, MACHINE_QUESTION, audit, can, may_move, may_reorder, password_hash, password_matches, signed_in) @@ -72,7 +72,7 @@ def me(request: Request): # Public by design: the Kanban browser initialises Sentry with these. 'sentry_dsn_web': os.environ.get('SENTRY_DSN_WEB', '').strip(), 'sentry_environment': os.environ.get('SENTRY_ENVIRONMENT') or ENVIRONMENT, - 'sentry_release': os.environ.get('IMAGE_TAG', ''), + 'sentry_release': observability.release() or '', # The machine this session is on, and whether to ask (item 12). 'machine': account['machine'], 'machine_question': MACHINE_QUESTION.get(role), 'machine_answered': account['machine_answered'], diff --git a/app/observability.py b/app/observability.py index bc16dd7..1eab87e 100644 --- a/app/observability.py +++ b/app/observability.py @@ -117,6 +117,13 @@ def _traces_rate(): return 0.0 +def release(): + """The deploy this process runs: IMAGE_TAG when the stack sets one, else the + commit the image was built from (RELEASE, from deploy/Dockerfile.api).""" + value = os.environ.get('IMAGE_TAG') or os.environ.get('RELEASE') or '' + return None if value in ('', 'unknown', 'latest') else value + + def init(component): """Start Sentry for this process; False (and no imports) without a DSN.""" dsn = os.environ.get('SENTRY_DSN_API', '').strip() @@ -130,7 +137,7 @@ def init(component): sentry_sdk.init( dsn=dsn, environment=environment, - release=os.environ.get('IMAGE_TAG') or None, + release=release(), send_default_pii=False, traces_sample_rate=_traces_rate(), before_send=scrub, diff --git a/deploy/Dockerfile.api b/deploy/Dockerfile.api index 591d62d..8b7bb66 100644 --- a/deploy/Dockerfile.api +++ b/deploy/Dockerfile.api @@ -6,6 +6,8 @@ ARG PYTHON_BASE_IMAGE=python:3.12-slim@sha256:2f17fc044b579bab302c2e8054d3a686e2 FROM ${PYTHON_BASE_IMAGE} ARG VCS_REF=unknown +# The commit this image was built from, as the Sentry release (app/observability.py). +ENV RELEASE=$VCS_REF LABEL org.opencontainers.image.title="DTF Portal/API" \ org.opencontainers.image.revision="$VCS_REF" \ org.opencontainers.image.source="DTF System repository"