services
azure_bootstrap.services
¶
Service implementations for Azure bootstrap library.
This module contains bootstrap services for application initialization.
Modules:
| Name | Description |
|---|---|
application_bootstrap |
Application bootstrap orchestrator that handles the complete startup flow. |
bootstrap_logging |
Bootstrap logging configuration for handling initialization before full telemetry setup. |
interfaces |
Service interfaces for Azure bootstrap library. |
telemetry |
|
Classes:
| Name | Description |
|---|---|
ApplicationBootstrap |
Orchestrates the complete application startup sequence with proper logging flow. |
BootstrapLogger |
Bootstrap logging manager that provides safe logging before full configuration. |
TelemetryManager |
Manages Application Insights telemetry and structured logging |
Functions:
| Name | Description |
|---|---|
create_enhanced_config_repository |
Factory function to create an enhanced configuration repository. |
initialize_application |
Convenience function to perform complete application bootstrap. |
get_bootstrap_logger |
Get a logger that works during bootstrap phase. |
ApplicationBootstrap
¶
ApplicationBootstrap(secrets_repository: SecretsRepositoryInterface | None = None)
Bases: ApplicationBootstrapInterface
Orchestrates the complete application startup sequence with proper logging flow.
This class handles the complex bootstrap process that requires careful ordering to avoid circular dependencies between logging, configuration, and secrets.
Bootstrap Flow
- Initialize console logging (safe fallback that always works)
- Try App Insights from environment variables if available
- Create and load enhanced configuration from App Config/Key Vault
- Attempt to upgrade logging to App Insights if connection string is now available
- Load all configuration to os.environ for transparent application access
- Log completion and provide access to configured components
Key Features
- Eliminates circular dependencies between logging and configuration
- Provides working logging throughout the entire bootstrap process
- Graceful fallbacks at every step ensure robustness
- Comprehensive logging of bootstrap progress and decisions
- Transparent configuration access via os.environ after completion
Usage Example
Simple bootstrap¶
bootstrap = ApplicationBootstrap() config_repo = bootstrap.initialize()
Bootstrap with secrets repository¶
bootstrap = ApplicationBootstrap(secrets_repository=my_secrets_repo) config_repo = bootstrap.initialize()
After bootstrap, all configs are in os.environ¶
app_insights_key = os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING") database_host = os.getenv("DATABASE_HOST")
Initialize the application bootstrap orchestrator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secrets_repository
|
SecretsRepositoryInterface | None
|
Optional secrets repository for Key Vault integration |
None
|
Methods:
| Name | Description |
|---|---|
initialize |
Execute the complete bootstrap sequence with proper logging flow. |
get_config_repository |
Get the configured repository after bootstrap. |
is_bootstrap_completed |
Check if bootstrap process has completed successfully. |
Source code in azure_bootstrap/services/application_bootstrap.py
initialize
¶
initialize() -> EnhancedConfigRepositoryInterface
Execute the complete bootstrap sequence with proper logging flow.
This method orchestrates the entire application startup process following the correct sequence to avoid circular dependencies while ensuring working logging throughout.
Returns:
| Type | Description |
|---|---|
EnhancedConfigRepositoryInterface
|
Configured enhanced configuration repository with all configs loaded |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If bootstrap fails in an unrecoverable way |
Source code in azure_bootstrap/services/application_bootstrap.py
get_config_repository
¶
get_config_repository() -> EnhancedConfigRepositoryInterface | None
Get the configured repository after bootstrap.
Returns:
| Type | Description |
|---|---|
EnhancedConfigRepositoryInterface | None
|
The enhanced configuration repository if bootstrap is complete, None otherwise |
Source code in azure_bootstrap/services/application_bootstrap.py
BootstrapLogger
¶
Bases: BootstrapLoggerInterface
Bootstrap logging manager that provides safe logging before full configuration.
This class handles the chicken-and-egg problem where: 1. Configuration loading needs logging 2. Logging (App Insights) needs configuration
Solution: Start with basic logging, then upgrade to full telemetry later.
Methods:
| Name | Description |
|---|---|
configure_bootstrap_logging |
Configure basic logging that works before full configuration is loaded. |
is_bootstrap_configured |
Check if bootstrap logging is configured. |
create_logger |
Create a logger with bootstrap configuration if not already done. |
configure_bootstrap_logging
classmethod
¶
configure_bootstrap_logging(level: str | None = None) -> None
Configure basic logging that works before full configuration is loaded.
This provides immediate, safe logging during application bootstrap. Later, telemetry_manager.configure() will enhance it with App Insights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str | None
|
Logging level (DEBUG, INFO, WARNING, ERROR). If not provided, reads from LOG_LEVEL environment variable, defaulting to INFO. |
None
|
Source code in azure_bootstrap/services/bootstrap_logging.py
create_logger
classmethod
¶
Create a logger with bootstrap configuration if not already done.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logger name (typically name) |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
Configured logger ready for use |
Source code in azure_bootstrap/services/bootstrap_logging.py
TelemetryManager
¶
Bases: TelemetryManagerInterface
Manages Application Insights telemetry and structured logging
Methods:
| Name | Description |
|---|---|
configure |
Configure Application Insights telemetry with support for bootstrap flow reconfiguration |
try_upgrade_from_config |
Attempt to upgrade from basic logging to App Insights after config is loaded |
get_tracer |
Get the configured tracer |
create_span |
Create a new trace span |
log_email_processing_start |
Log email processing start with structured data |
log_email_processing_success |
Log successful email processing |
log_email_processing_error |
Log email processing error |
log_queue_message_received |
Log queue message processing |
log_storage_operation |
Log storage operations |
Source code in azure_bootstrap/services/telemetry.py
configure
¶
Configure Application Insights telemetry with support for bootstrap flow reconfiguration
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
connection_string
|
str | None
|
App Insights connection string |
None
|
allow_reconfigure
|
bool
|
If True, allows reconfiguration even if already configured |
False
|
Source code in azure_bootstrap/services/telemetry.py
try_upgrade_from_config
¶
try_upgrade_from_config(config_repository: EnhancedConfigRepositoryInterface) -> bool
Attempt to upgrade from basic logging to App Insights after config is loaded
This method is called after configuration loading to check if an App Insights connection string is now available from App Config/Key Vault that wasn't available during initial bootstrap.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config_repository
|
EnhancedConfigRepositoryInterface
|
Enhanced config repository to check for connection string |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if upgrade was attempted (success or failure), False if no upgrade needed |
Source code in azure_bootstrap/services/telemetry.py
create_span
¶
Create a new trace span
Source code in azure_bootstrap/services/telemetry.py
log_email_processing_start
¶
Log email processing start with structured data
Source code in azure_bootstrap/services/telemetry.py
log_email_processing_success
¶
Log successful email processing
Source code in azure_bootstrap/services/telemetry.py
log_email_processing_error
¶
log_email_processing_error(error: str, message_id: str | None = None, user_email: str | None = None) -> None
Log email processing error
Source code in azure_bootstrap/services/telemetry.py
log_queue_message_received
¶
Log queue message processing
Source code in azure_bootstrap/services/telemetry.py
log_storage_operation
¶
Log storage operations
Source code in azure_bootstrap/services/telemetry.py
create_enhanced_config_repository
¶
create_enhanced_config_repository(app_config_connection_string: str | None = None, secrets_repository: SecretsRepositoryInterface | None = None, auto_load_to_environ: bool = False) -> EnhancedConfigRepositoryInterface
Factory function to create an enhanced configuration repository.
This factory function provides a convenient way to create a properly configured EnhancedConfigRepository instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
app_config_connection_string
|
str | None
|
Azure App Configuration connection string |
None
|
secrets_repository
|
SecretsRepositoryInterface | None
|
Optional secrets repository for Key Vault integration |
None
|
auto_load_to_environ
|
bool
|
If True, automatically load configs to os.environ on init |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
EnhancedConfigRepositoryInterface |
EnhancedConfigRepositoryInterface
|
Configured repository instance |
Usage
Simple usage with environment variables only¶
config_repo = create_enhanced_config_repository()
With App Config¶
config_repo = create_enhanced_config_repository( app_config_connection_string="Endpoint=...", auto_load_to_environ=True )
With App Config and Key Vault¶
secrets_repo = SecretsRepository(vault_url="...") config_repo = create_enhanced_config_repository( app_config_connection_string="Endpoint=...", secrets_repository=secrets_repo, auto_load_to_environ=True )
Source code in azure_bootstrap/repositories/enhanced_config_repository.py
initialize_application
¶
initialize_application(secrets_repository: SecretsRepositoryInterface | None = None) -> EnhancedConfigRepositoryInterface
Convenience function to perform complete application bootstrap.
This is the main entry point for application initialization. It handles the complete bootstrap sequence and returns the configured repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
secrets_repository
|
SecretsRepositoryInterface | None
|
Optional secrets repository for Key Vault integration |
None
|
Returns:
| Type | Description |
|---|---|
EnhancedConfigRepositoryInterface
|
Configured enhanced configuration repository with all configs loaded |
Example
Simple initialization¶
config_repo = initialize_application()
With secrets repository¶
from src.repositories.secrets_repository import create_secrets_repository secrets_repo = create_secrets_repository() config_repo = initialize_application(secrets_repository=secrets_repo)
After initialization, use standard os.environ access¶
app_insights_key = os.environ.get("APPLICATIONINSIGHTS_CONNECTION_STRING")
Source code in azure_bootstrap/services/application_bootstrap.py
get_bootstrap_logger
¶
Get a logger that works during bootstrap phase.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Logger name (typically name) |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
Logger configured for bootstrap use |
Example
from src.infrastructure.bootstrap_logging import get_bootstrap_logger
logger = get_bootstrap_logger(name) logger.info("This works before full telemetry setup!")