diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 8014b8f..4f0b450 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -29,6 +29,7 @@ jobs: tests.test_hotfolder \ tests.test_migrate \ tests.test_label \ + tests.test_observability \ tests.test_secrets -v sh -n infra/lock_dependencies.sh @@ -99,7 +100,7 @@ jobs: # the generator's geometry. The provider suites use a fake transport: they # prove the documented contract, not the integration. - name: Print-file geometry and provider adapters - run: $COMPOSE exec -T api python -m unittest tests.test_printfile tests.test_printpng tests.test_mercadopago tests.test_tiny tests.test_cnpj tests.test_payment_messages tests.test_jadlog tests.test_correios tests.test_quote_review tests.test_moves tests.test_large_files tests.test_grade_check tests.test_schema tests.test_customer_document tests.test_mail_html tests.test_layout_rules tests.test_proof_cut tests.test_proof_render tests.test_prumo tests.test_speed tests.test_printpng_validate tests.test_machine_log tests.test_machine_stats tests.test_label tests.test_worker_drain -v + run: $COMPOSE exec -T api python -m unittest tests.test_printfile tests.test_printpng tests.test_mercadopago tests.test_tiny tests.test_cnpj tests.test_payment_messages tests.test_jadlog tests.test_correios tests.test_quote_review tests.test_moves tests.test_large_files tests.test_grade_check tests.test_schema tests.test_customer_document tests.test_mail_html tests.test_layout_rules tests.test_proof_cut tests.test_proof_render tests.test_prumo tests.test_speed tests.test_printpng_validate tests.test_machine_log tests.test_machine_stats tests.test_label tests.test_observability tests.test_worker_drain -v - name: Runtime and retention regressions run: | diff --git a/app/api/health.py b/app/api/health.py index d31bd70..3ef97bc 100644 --- a/app/api/health.py +++ b/app/api/health.py @@ -63,6 +63,10 @@ def session(request: Request, response: Response): 'unit_cents': band['unit_cents']} for band in products[mode]['bands']]} for mode in catalog.MODES}, + # 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', ''), # 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 e0c3b93..8a52e42 100644 --- a/app/api/users.py +++ b/app/api/users.py @@ -5,6 +5,7 @@ changes it in "Minha senha". The superadmin is the system's own account, set fro administrators manage everyone else. Nobody can remove, disable or demote themselves, so the last administrator cannot lock the team out by accident. """ +import os import re from decimal import Decimal from typing import Literal @@ -18,7 +19,7 @@ from ..core import db from ..core.auth import (CAPABILITIES, MACHINE_QUESTION, audit, can, may_move, may_reorder, password_hash, password_matches, signed_in) from ..core.models import StrictModel -from ..runtime import BACK, STATES, TRANSITIONS +from ..runtime import BACK, ENVIRONMENT, STATES, TRANSITIONS router = APIRouter() @@ -68,6 +69,10 @@ def me(request: Request): moves = [[a, b] for a, nexts in TRANSITIONS.items() for b in nexts if may_move(role, a, b)] moves += [[a, b] for a, b in BACK.items() if may_move(role, a, b, back=True)] return {'email': account['email'], 'name': account['name'], 'role': role, + # 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', ''), # 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/app.py b/app/app.py index 31e8b87..93d1c2b 100644 --- a/app/app.py +++ b/app/app.py @@ -10,11 +10,14 @@ from fastapi import FastAPI from fastapi.responses import JSONResponse from starlette.middleware.trustedhost import TrustedHostMiddleware +from . import observability from .core import db from .core.auth import audit, client_ip from .runtime import ALLOWED_HOSTS, ALLOWED_ORIGINS, storage from .api import artwork, customer, health, machine_log, operator, orders, payments, quotes, reviews, site_admin, site_videos, stamps, uploads, users +observability.init('api') + @asynccontextmanager async def lifespan(app): diff --git a/app/observability.py b/app/observability.py new file mode 100644 index 0000000..bc16dd7 --- /dev/null +++ b/app/observability.py @@ -0,0 +1,139 @@ +"""Sentry for the API and the worker, off until a DSN arrives (note Sentry). + +`init(component)` reads `SENTRY_DSN_API` and returns without importing the SDK +when it is empty, so local and CI runs carry no Sentry dependency or network +cost. When it is set, `scrub` keeps cookies, credentials, request bodies and +Brazilian personal data out of the events (LGPD). +""" +import os +import re +from urllib.parse import parse_qsl, urlencode, urlsplit, urlunsplit + +EMAIL = re.compile(r'[^\s@]+@[^\s@]+\.[A-Za-z]{2,}') +# CPF (11 digits) or CNPJ (14), with or without punctuation. +DOCUMENT = re.compile(r'(?{ + window.dtfEnvironment=session.sentry_environment||session.environment; + window.dtfRelease=session.sentry_release||undefined; + window.dtfObservability?.start?.(session.sentry_dsn_web,'site'); window.dtfUploadMaxBytes=session.max_upload_bytes; const limit=document.getElementById('zLimite'); if(limit)limit.textContent='Até '+(session.max_upload_bytes/1073741824).toFixed(0)+' GB por arquivo.'; diff --git a/web/index.html b/web/index.html index c40bb6c..105b43f 100644 --- a/web/index.html +++ b/web/index.html @@ -641,6 +641,8 @@ h2 em{font-style:normal;color:var(--laranja)} @media(prefers-reduced-motion:reduce){*{transition:none!important;scroll-behavior:auto}} + +
diff --git a/web/kanban.html b/web/kanban.html index 49f90b2..a5bc674 100644 --- a/web/kanban.html +++ b/web/kanban.html @@ -297,4 +297,4 @@ -