Catalog PyPI
PACKAGE EXPLORERView in registry

aignostics-foundry-core

PyPI

๐Ÿญ Foundational infrastructure for Foundry components.

actaignostics-foundry-corecodecovcopiercyclonedxdetect-secretsfoundryfoundry-pythonjupyternoxpip-auditpip-licensespre-commitpypipytestpython
All keywords (20)
ruffsonarcloudsonarqubeuv
Versions collected1
Direct dependencies26From release 0.18.0
Last observed21 Oct, 10:49 UTC

README

Version 0.18.0 documentation

๐Ÿญ Foundry Python Core

License CI Quality Gate Security Maintainability Technical Debt Code Smells Dependabot Renovate enabled Coverage Ruff Pyright Copier

Foundational infrastructure for Foundry components.

Prerequisites

Install mise (task runner and dev tool manager):

brew install mise

Or follow the installation guide for other methods. Then activate mise in your shell profile.

Usage

Quickstart

FoundryContext is the single source of truth for all project-specific values. One call at application startup makes everything available library-wide โ€” logging, Sentry, database settings, and more all derive from it automatically.

Initialise and boot

# main.py
from aignostics_foundry_core.foundry import FoundryContext, set_context
from aignostics_foundry_core.boot import boot

set_context(FoundryContext.from_package("myproject"))
boot()

FoundryContext.from_package("myproject") reads package metadata and environment variables to populate every field:

  • name, version, version_full โ€” from importlib.metadata
  • environment โ€” resolved from env vars in priority order (see Configuration reference below)
  • env_prefix ("MYPROJECT_") โ€” used by every settings class; all env vars for this project share this prefix
  • is_container, is_cli, is_test, is_library โ€” detected automatically

boot() initialises logging (loguru), amends the SSL trust chain (truststore + certifi), and optionally starts Sentry โ€” all in one call.

Env file search order

Settings are loaded from the environment and from env files. Highest priority first:

  1. .env.{environment}
  2. .env
  3. {MYPROJECT_ENV_FILE} (optional extra file; when the variable is set)
  4. ~/.myproject/.env.{environment}
  5. ~/.myproject/.env

Access the context from any module

from aignostics_foundry_core.foundry import get_context

ctx = get_context()
print(f"Running {ctx.name} v{ctx.version_full} in {ctx.environment}")
# โ†’ Running myproject v1.2.3+main-abc1234---run.12345---build.42 in staging

get_context() raises RuntimeError with a clear message if set_context() was never called.

Testing pattern

Never call set_context() in tests. Pass a FoundryContext directly to functions via their optional context parameter:

from aignostics_foundry_core.foundry import FoundryContext
from aignostics_foundry_core.log import logging_initialize

ctx = FoundryContext(name="myproject", version="0.0.0", version_full="0.0.0", environment="test")
logging_initialize(context=ctx)

All public library functions (logging_initialize, sentry_initialize, boot, load_modules, etc.) accept an optional context keyword argument and fall back to get_context() when it is None.


Configuration reference

All settings classes read from environment variables prefixed with {PREFIX} where {PREFIX} = MYPROJECT_ for a package named myproject.

Context & deployment environment

Read directly by FoundryContext.from_package() โ€” no settings class.

VariableDefaultDescription
{PREFIX}ENVIRONMENT"local"Deployment environment name. Highest priority.
ENVโ€”Fallback environment (lower priority than {PREFIX}ENVIRONMENT).
VERCEL_ENVโ€”Vercel deployment environment (lower priority).
RAILWAY_ENVIRONMENTโ€”Railway deployment environment (lower priority).
{PREFIX}RUNNING_IN_CONTAINERunsetSet to any non-empty value to mark is_container = True.
{PREFIX}ENV_FILEunsetPath to an additional env file, inserted between the home-dir files and the local .env.

Build metadata

Read by FoundryContext.from_package() to build version_full and version_with_vcs_ref. All optional; most useful in CI.

VariableDefaultDescription
VCS_REFread from .git/HEADBranch name or commit SHA. Falls back to reading .git/HEAD when project path is found.
COMMIT_SHA"unknown"Full commit SHA; first 7 chars used.
BUILD_DATE"unknown"Build date string.
CI_RUN_ID"unknown"CI system run ID.
CI_RUN_NUMBER"unknown"CI system build number.
BUILDER"uv"Build tool name.

When any of these variables is set, version_full gains a +โ€ฆ suffix, e.g. 1.2.3+main-abc1234---run.12345---build.42.

Logging ({PREFIX}LOG_)

Settings class: LogSettings

VariableDefaultDescription
{PREFIX}LOG_LEVELINFOLog level: CRITICAL, ERROR, WARNING, SUCCESS, INFO, DEBUG, or TRACE.
{PREFIX}LOG_STDERR_ENABLEDtrueEnable logging to stderr.
{PREFIX}LOG_FILE_ENABLEDfalseEnable logging to a file.
{PREFIX}LOG_FILE_NAMEplatform log dirPath to the log file (validated on startup when FILE_ENABLED is true).
{PREFIX}LOG_REDIRECT_LOGGINGtrueRedirect stdlib logging to loguru via InterceptHandler.

logging_initialize() sets the stdlib loggers httpx, httpx2 and urllib3 to WARNING. These HTTP clients log each request URL at INFO, and the URL can contain credentials in its query string (for example, the signature of a signed URL). At WARNING, these request lines do not get to the log sinks or to Sentry. The psycopg and psycopg.pool loggers are also at WARNING.

Sentry ({PREFIX}SENTRY_)

Settings class: SentrySettings. Sentry is only initialised when ENABLED=true and DSN is set.

VariableDefaultDescription
{PREFIX}SENTRY_ENABLEDfalseEnable Sentry error and performance monitoring.
{PREFIX}SENTRY_DSNunsetSentry DSN (must be an HTTPS URL with a valid ingest.*.sentry.io domain).
{PREFIX}SENTRY_DEBUGfalseEnable Sentry SDK debug mode.
{PREFIX}SENTRY_SEND_DEFAULT_PIIfalseInclude personally-identifiable information in events.
{PREFIX}SENTRY_INCLUDE_LOCAL_VARIABLESfalseInclude the local variables of each stack frame in error events. Locals can contain credentials and personal data.
{PREFIX}SENTRY_MAX_REQUEST_BODY_SIZE"never"Maximum size of HTTP request bodies in error events: "never", "small" (up to 1 KB), "medium" (up to 10 KB) or "always". Request bodies can contain credentials and personal data. The FastAPI integration sends JSON bodies even when SEND_DEFAULT_PII is false.
{PREFIX}SENTRY_TRACE_PROPAGATION_TARGETS[]JSON list of regexes. Sentry adds the sentry-trace and baggage headers only to outbound requests whose URL matches one of them. The baggage header contains the release, the environment and the public key of the DSN. With the default, no outbound request gets these headers. Example: {PREFIX}SENTRY_TRACE_PROPAGATION_TARGETS='["internal\\.example\\.com"]'.
{PREFIX}SENTRY_MAX_BREADCRUMBS50Maximum breadcrumbs stored per event.
{PREFIX}SENTRY_SAMPLE_RATE1.0Error event sample rate (0.0โ€“1.0).
{PREFIX}SENTRY_TRACES_SAMPLE_RATE0.1Transaction/trace sample rate.
{PREFIX}SENTRY_PROFILES_SAMPLE_RATE0.1Profiler sample rate.
{PREFIX}SENTRY_PROFILE_SESSION_SAMPLE_RATE0.1Profile session sample rate.
{PREFIX}SENTRY_PROFILE_LIFECYCLE"trace"Profile lifecycle mode: "trace" or "manual".

Log records do not go to Sentry Logs, because they can contain credentials and personal data. To send them, pass LoggingIntegration(capture_sentry_logs=True) or LoguruIntegration(capture_sentry_logs=True) to boot(sentry_integrations=...). foundry-core requires sentry-sdk 2.68.0 or later, and it has no {PREFIX}SENTRY_ENABLE_LOGS setting.

OpenTelemetry ({PREFIX}OTEL_)

Settings class: OTelSettings. OpenTelemetry is only initialised when ENABLED=true and the standard OTEL_EXPORTER_OTLP_ENDPOINT is set. Each signal is then independently toggleable โ€” traces and metrics default on, logs off (their volume/cost profile differs). Telemetry is exported via OTLP/gRPC, e.g. to the internal OTel gateway backing the Grafana stack (Tempo/Loki/Prometheus).

VariableDefaultDescription
{PREFIX}OTEL_ENABLEDfalseMaster switch for OpenTelemetry export via OTLP.
{PREFIX}OTEL_TRACES_ENABLEDtrueExport traces (once ENABLED).
{PREFIX}OTEL_METRICS_ENABLEDtrueExport metrics (once ENABLED).
{PREFIX}OTEL_LOGS_ENABLEDfalseBridge loguru records into OTLP log export (once ENABLED).

The OTLP log sink uses the same {PREFIX}LOG_LEVEL as the stderr and file sinks. It also uses the log_filter that the service passes to boot(). Records below the log level, and records that the filter rejects, do not go to the OTLP endpoint.

Endpoint, service name, and all other exporter behaviour come from the standard, unprefixed OpenTelemetry environment variables the SDK reads itself โ€” not project-prefixed settings:

VariableDefaultDescription
OTEL_EXPORTER_OTLP_ENDPOINTunsetOTLP/gRPC collector endpoint. Required โ€” export is skipped if unset.
OTEL_SERVICE_NAMEproject nameService name attached to all telemetry. Defaults to the FoundryContext name.
OTEL_EXPORTER_OTLP_CERTIFICATEOS CA bundle, else certifi'sCA file for the exporter's TLS. The Foundry Cloud Run Dockerfile installs the fleet's internal CA into the OS trust store, so this defaults to that bundle (falls back to certifi's public-roots-only bundle if it isn't present, e.g. running locally); set explicitly to override.
OTEL_RESOURCE_ATTRIBUTESunsetExtra resource attributes, comma-separated key=value pairs.
OTEL_SEMCONV_STABILITY_OPT_INhttpOpts HTTP instrumentation into the stable semantic conventions (low-cardinality route-template span names) instead of the old, experimental ones.

Process-level tracing/metrics/logs are set up by boot(). boot() also applies default auto-instrumentors (HTTPX, SQLAlchemy) when traces are enabled โ€” override via boot(otel_instrumentors=[...]), or pass [] to opt out. Request-level FastAPI spans are instrumented automatically too: init_api() applies instrument_fastapi() to the app it builds (and to every versioned sub-app) โ€” no explicit call needed. A project that constructs its FastAPI instance some other way can call instrument_fastapi(app) directly, once, after construction.

Database ({PREFIX}DB_)

Settings class: DatabaseSettings. Database configuration is only activated when {PREFIX}DB_URL is present (in the environment or in an env file).

VariableRequiredDefaultDescription
{PREFIX}DB_URLto activateโ€”Full async database connection URL (e.g. postgresql+asyncpg://user:pass@host/db). Always access via DatabaseSettings.get_url().
{PREFIX}DB_POOL_SIZEno10SQLAlchemy connection pool size.
{PREFIX}DB_POOL_MAX_OVERFLOWno10Max connections above pool size.
{PREFIX}DB_POOL_TIMEOUTno30.0Seconds to wait for a pool connection.
{PREFIX}DB_NAMEnounsetOverride the database name in the URL path at runtime.

Once a context is configured via set_context(), all database functions work with no arguments โ€” the URL and pool settings are read from the context:

from aignostics_foundry_core.database import init_engine, cli_run_with_db, with_engine

# Zero-arg engine init โ€” reads MYPROJECT_DB_URL, _DB_POOL_SIZE, etc. from env
init_engine()

# CLI helper โ€” initialises engine, runs coroutine, disposes engine
cli_run_with_db(my_async_func)


# Background job decorator โ€” engine initialised before each invocation
@with_engine
async def my_job(): ...


# Override for a secondary database
@with_engine(db_url="postgresql+asyncpg://user:pass@host/secondary")
async def my_other_job(): ...

In tests, construct DatabaseSettings directly instead of setting env vars:

from aignostics_foundry_core.database import DatabaseSettings
from tests.conftest import make_context

ctx = make_context(database=DatabaseSettings(_env_prefix="TEST_DB_", url="sqlite+aiosqlite:///test.db"))

Authentication ({PREFIX}AUTH_)

Settings class: AuthSettings. All fields are optional with defaults unless enabled=True, which activates several cross-field requirements. Only needed when using aignostics_foundry_core.api.auth dependencies.

VariableRequiredDefaultDescription
{PREFIX}AUTH_ENABLEDnofalseEnable Auth0 authentication. When true, several other fields become required.
{PREFIX}AUTH_SESSION_SECRETwhen enabled""Secret to sign session cookies. Required when AUTH_ENABLED=true.
{PREFIX}AUTH_SESSION_EXPIRATIONno86400Session cookie expiration in seconds (range: 61โ€“31536000).
{PREFIX}AUTH_DOMAINwhen enabled""Auth0 domain (e.g. myapp.eu.auth0.com). Required when AUTH_ENABLED=true.
{PREFIX}AUTH_CLIENT_IDwhen enabled""Auth0 client ID (max 32 chars). Required when AUTH_ENABLED=true.
{PREFIX}AUTH_CLIENT_SECRETwhen enabled""Auth0 client secret (64 chars). Required when AUTH_ENABLED=true.
{PREFIX}AUTH_INTERNAL_ORG_IDwhen enabled""Auth0 organization ID identifying the internal org (used by require_internal). Required when AUTH_ENABLED=true.
{PREFIX}AUTH_ROLE_CLAIMwhen enabled""JWT claim name containing the user's role (e.g. https://myapp.example.com/roles). Required when AUTH_ENABLED=true.

Console

Read directly from the environment โ€” no settings class.

VariableDefaultDescription
{PREFIX}CONSOLE_WIDTHauto-detectOverride Rich console width (integer, characters). Defaults to terminal width or 80 in non-TTY environments.

Further Reading

Downloads & popularity

Awaiting observations

No metric observations yet

Download counts and popularity will appear here as the collector receives them.

Versions 1

Collected releases
VersionPublishedDirect dependenciesOpen version
0.18.028 Sept 202626

Package advisories

0
No matching advisories collected

This describes our collected data; it is not a security assessment.

Repository

PENDING
aignostics/foundry-python-core

Shared repository data ยท GitHub

License detected in repository
Not collected

Metadata history

Meaningful changes
Current collected state Current

Observed since 21 Oct, 10:49 UTC

No superseded metadata states collected. Unchanged observations do not create new revisions.

Record identity

package:d49bb1b5389dc7c2a4fdbf6ec6ca71d6ed314d138cb43993798555814548dcac