PackagesPackages
Shared Python utilities (common)
Shared Python 3.13 utilities used across services and jobs
common is the shared Python 3.13 library used by backend services and jobs for Spanner access,
structured logging, secrets, and shared models.
Package profile
| Field | Value |
|---|---|
| Location | packages/python/common/ |
| Runtime | Python 3.13 |
| Install | uv sync --all-packages |
| Import | common.* |
| Type | Library only (no runtime entrypoint) |
What lives here
common.spanner: Async Spanner wrapper, session pooling, transaction utilities, and index cache.common.spanner.models: Pydantic models for Spanner tables and mutation helpers.common.models: Cross-service request/response models (TaskInfo, RawEvent, PIMS/clinic/org, invoice webhook payloads).common.secrets: Secret Manager helpers with caching and CRC32c integrity checks.common.logging_utils: JSON logging formatter and request context helpers.common.cloudtasks: Async Cloud Tasks helper for queue inspection and de-duplication.common.security: Constant-time secret verification.common.util: Datetime normalization, gRPC timestamp helpers, and project ID resolution.
Spanner primitives
Spanner: Async wrapper overSpannerAsyncClientwith anAsyncBurstyPoolsession pool.TransactionManager: Async context manager that guarantees commit or rollback.SpannerModel: Base Pydantic model for Spanner rows withupsert_mutationandbulk_upserthelpers that handle commit timestamps and large batches.SpannerIndexCache: Caches secondary index counts for mutation sizing.SpannerModel.iter_models(): Useful for warming caches across all models.
Data model coverage
- Spanner row models for appointments, clinics, patients, organizations, invoices, pipelines, households, and Bubble mirror tables.
- Pydantic request/response models for webhook payloads and pipeline task metadata.
Logging and telemetry
setup_loggingconfigures JSON logs for Cloud Logging withJsonFormatter.request_contextextracts HTTP metadata (method, path, task headers, trace id) for structured logs.- Use
extra={"extra_fields": {...}}to attach service-specific metadata without string formatting.
Secrets and security
get_secretfetches from Secret Manager with a per-secret cache and CRC32c validation.get_service_sentry_dsn,get_service_api_key,get_raven_bubble_api_tokenwrap common lookups.Security.securely_verify_secretprovides constant-time comparisons.
Cloud Tasks helpers
CloudTasksHelper.list_tasks_for_queuelists tasks in a queue.CloudTasksHelper.is_task_scheduledchecks for an existing TaskInfo-based task id.
Local development
- Install dependencies with
uv sync --all-packagesfrom repo root. - No standalone runtime; validate changes by running the owning service tests.
- Keep imports absolute (
import common...); the package ships withpy.typedfor type checking.
Configuration
- Requires ADC when accessing Spanner or Secret Manager.
GCP_PROJECT_IDis used byresolve_project_idwhen running outside GCP.
Ownership
- Shared platform library; changes should be coordinated with service owners.
References
- Related: Spanner emulator setup
Last updated on