CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-azure-core

Microsoft Azure Core Library providing foundational infrastructure for Azure SDK Python clients

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

configuration-and-settings.mddocs/

Configuration and Settings

Azure Core provides a comprehensive global configuration system that manages Azure SDK settings with a clear precedence hierarchy, environment variable support, and programmatic configuration options. The settings system enables consistent behavior across all Azure SDK components.

Global Settings Instance

from azure.core.settings import settings

# Global settings singleton
settings.log_level = logging.DEBUG
settings.tracing_enabled = True
settings.tracing_implementation = "opentelemetry"
settings.azure_cloud = AzureClouds.AZURE_US_GOVERNMENT

Configuration Priority

Settings follow a 5-level precedence system (highest to lowest priority):

  1. Immediate values: Values passed directly to setting calls
  2. User-set values: Values set programmatically via assignment
  3. Environment variables: OS environment variables
  4. System settings: System-wide configurations
  5. Default values: Built-in default values

Available Settings

Log Level

Controls logging level for Azure SDK operations.

import logging
from azure.core.settings import settings

# Programmatic configuration
settings.log_level = logging.DEBUG
settings.log_level = logging.INFO
settings.log_level = logging.WARNING
settings.log_level = logging.ERROR
settings.log_level = logging.CRITICAL

# Get current log level
current_level = settings.log_level()

# Use immediate value (highest priority)
temp_level = settings.log_level(logging.WARNING)

Environment Variable: AZURE_LOG_LEVEL Default: logging.INFO (20) Supported Values:

  • Integers: Any valid logging level
  • Strings: "CRITICAL", "ERROR", "WARNING", "INFO", "DEBUG" (case-insensitive)

Environment Variable Usage:

export AZURE_LOG_LEVEL=DEBUG
export AZURE_LOG_LEVEL=20

Tracing Enabled

Controls whether distributed tracing is enabled across Azure SDK operations.

from azure.core.settings import settings

# Enable/disable tracing
settings.tracing_enabled = True
settings.tracing_enabled = False

# Auto-enable when implementation is set
settings.tracing_implementation = "opentelemetry"
# tracing_enabled automatically becomes True

# Check current status
is_enabled = settings.tracing_enabled()

Environment Variable: AZURE_TRACING_ENABLED Default: None (auto-determined based on tracing implementation) Supported Values:

  • Boolean: True, False
  • Strings: "yes", "1", "on", "true" → True; "no", "0", "off", "false" → False

Environment Variable Usage:

export AZURE_TRACING_ENABLED=true
export AZURE_TRACING_ENABLED=1
export AZURE_TRACING_ENABLED=yes

Tracing Implementation

Specifies which distributed tracing implementation to use.

from azure.core.settings import settings
from azure.core.tracing.ext.opentelemetry_span import OpenTelemetrySpan

# String-based configuration
settings.tracing_implementation = "opentelemetry"
settings.tracing_implementation = "opencensus"

# Direct class assignment
settings.tracing_implementation = OpenTelemetrySpan

# Get current implementation
impl = settings.tracing_implementation()

Environment Variable: AZURE_SDK_TRACING_IMPLEMENTATION Default: None Supported Values:

  • None: No tracing implementation
  • String: "opentelemetry", "opencensus" (case-insensitive)
  • Type: Any AbstractSpan subclass

Environment Variable Usage:

export AZURE_SDK_TRACING_IMPLEMENTATION=opentelemetry

Azure Cloud

Specifies which Azure cloud environment to use for default endpoints and configurations.

from azure.core.settings import settings
from azure.core import AzureClouds

# Enum-based configuration
settings.azure_cloud = AzureClouds.AZURE_PUBLIC_CLOUD
settings.azure_cloud = AzureClouds.AZURE_CHINA_CLOUD
settings.azure_cloud = AzureClouds.AZURE_US_GOVERNMENT

# String-based configuration
settings.azure_cloud = "AZURE_US_GOVERNMENT"
settings.azure_cloud = "AZURE_CHINA_CLOUD"

# Get current cloud
cloud = settings.azure_cloud()

Environment Variable: AZURE_CLOUD Default: AzureClouds.AZURE_PUBLIC_CLOUD Supported Values:

  • AzureClouds.AZURE_PUBLIC_CLOUD: Public Azure cloud
  • AzureClouds.AZURE_CHINA_CLOUD: Azure China cloud
  • AzureClouds.AZURE_US_GOVERNMENT: Azure US Government cloud

Environment Variable Usage:

export AZURE_CLOUD=AZURE_US_GOVERNMENT
export AZURE_CLOUD=AZURE_CHINA_CLOUD

Configuration Snapshots

Current Configuration

Get a snapshot of all current settings with their computed values.

from azure.core.settings import settings

# Get current configuration snapshot
current_config = settings.current

# Access individual settings
print(f"Log Level: {current_config.log_level}")
print(f"Tracing Enabled: {current_config.tracing_enabled}")
print(f"Tracing Implementation: {current_config.tracing_implementation}")
print(f"Azure Cloud: {current_config.azure_cloud}")

Default Configuration

Get a snapshot of all default values without environment or user overrides.

from azure.core.settings import settings

# Get default configuration snapshot
defaults = settings.defaults

print(f"Default Log Level: {defaults.log_level}")
print(f"Default Tracing Enabled: {defaults.tracing_enabled}")
print(f"Default Azure Cloud: {defaults.azure_cloud}")

Custom Configuration

Create a configuration snapshot with specific overrides.

import logging
from azure.core.settings import settings
from azure.core import AzureClouds

# Create custom configuration
custom_config = settings.config(
    log_level=logging.ERROR,
    tracing_enabled=False,
    azure_cloud=AzureClouds.AZURE_CHINA_CLOUD
)

# Use custom configuration values
print(f"Custom Log Level: {custom_config.log_level}")

Practical Usage Examples

Development Environment Setup

import logging
from azure.core.settings import settings

# Development configuration
settings.log_level = logging.DEBUG
settings.tracing_enabled = True
settings.tracing_implementation = "opentelemetry"

print(f"Development config - Tracing: {settings.tracing_enabled()}")
print(f"Development config - Log Level: {settings.log_level()}")

Production Environment Setup

# Production environment variables
export AZURE_LOG_LEVEL=WARNING
export AZURE_TRACING_ENABLED=true
export AZURE_SDK_TRACING_IMPLEMENTATION=opentelemetry
export AZURE_CLOUD=AZURE_PUBLIC_CLOUD

Configuration Validation

from azure.core.settings import settings
import logging

def validate_configuration():
    """Validate current Azure SDK configuration"""
    config = settings.current
    
    # Check logging configuration
    if config.log_level < logging.WARNING:
        print("Warning: Verbose logging enabled - may impact performance")
    
    # Check tracing configuration
    if config.tracing_enabled and not config.tracing_implementation:
        print("Error: Tracing enabled but no implementation specified")
        return False
    
    # Check cloud configuration
    print(f"Using Azure Cloud: {config.azure_cloud}")
    
    return True

# Validate before starting application
if validate_configuration():
    print("Configuration is valid")

Temporary Configuration Override

from azure.core.settings import settings
import logging

def debug_operation():
    """Temporarily enable debug logging for this operation"""
    # Save current log level
    original_level = settings.log_level()
    
    try:
        # Temporarily set debug level
        settings.log_level = logging.DEBUG
        
        # Perform operation with debug logging
        perform_complex_operation()
        
    finally:
        # Restore original log level
        settings.log_level = original_level

def immediate_override_example():
    """Use immediate value without changing global setting"""
    # Use debug level just for this call without changing global setting
    current_level = settings.log_level(logging.DEBUG)
    
    # Global setting remains unchanged
    global_level = settings.log_level()
    print(f"Immediate: {current_level}, Global: {global_level}")

Settings Reset and Management

from azure.core.settings import settings

def reset_to_defaults():
    """Reset all settings to their default values"""
    # Reset individual settings
    settings.log_level.unset_value()
    settings.tracing_enabled.unset_value()
    settings.tracing_implementation.unset_value()
    settings.azure_cloud.unset_value()

def show_configuration_sources():
    """Show where each setting value comes from"""
    # This would show the precedence for each setting
    print("Configuration Sources:")
    print(f"Log Level - Current: {settings.log_level()}")
    print(f"Log Level - Default: {settings.defaults.log_level}")
    print(f"Log Level - Environment: {settings.log_level.env_var}")

Integration with Application Configuration

import os
from azure.core.settings import settings
from azure.core import AzureClouds

def configure_from_app_config(app_config: dict):
    """Configure Azure SDK from application configuration"""
    
    # Map application config to Azure settings
    log_level_map = {
        'debug': logging.DEBUG,
        'info': logging.INFO,
        'warning': logging.WARNING,
        'error': logging.ERROR
    }
    
    # Configure logging
    if 'log_level' in app_config:
        settings.log_level = log_level_map.get(
            app_config['log_level'].lower(), 
            logging.INFO
        )
    
    # Configure tracing
    if app_config.get('enable_tracing', False):
        settings.tracing_enabled = True
        settings.tracing_implementation = app_config.get(
            'tracing_provider', 
            'opentelemetry'
        )
    
    # Configure cloud environment
    cloud_map = {
        'public': AzureClouds.AZURE_PUBLIC_CLOUD,
        'china': AzureClouds.AZURE_CHINA_CLOUD,
        'government': AzureClouds.AZURE_US_GOVERNMENT
    }
    
    if 'azure_cloud' in app_config:
        settings.azure_cloud = cloud_map.get(
            app_config['azure_cloud'].lower(),
            AzureClouds.AZURE_PUBLIC_CLOUD
        )

# Example usage
app_config = {
    'log_level': 'debug',
    'enable_tracing': True,
    'tracing_provider': 'opentelemetry',
    'azure_cloud': 'government'
}

configure_from_app_config(app_config)

Key Features

Hierarchical Precedence: Clear 5-level priority system ensures predictable configuration behavior.

Environment Variable Support: All settings can be configured via environment variables for deployment flexibility.

Type Safety: Automatic type conversion and validation for all configuration values.

Configuration Snapshots: Capture configuration state for debugging and validation.

Global Consistency: Single source of truth for Azure SDK configuration across all components.

Runtime Changes: Settings can be modified at runtime and take effect immediately.

Default Restoration: Settings can be reset to defaults while preserving environment variable behavior.

The configuration system provides a robust foundation for managing Azure SDK behavior consistently across different deployment environments while supporting both programmatic and environment-based configuration approaches.

Install with Tessl CLI

npx tessl i tessl/pypi-azure-core

docs

async-programming-patterns.md

authentication-and-credentials.md

configuration-and-settings.md

distributed-tracing-and-diagnostics.md

error-handling-and-exceptions.md

http-pipeline-and-policies.md

index.md

paging-and-result-iteration.md

polling-and-long-running-operations.md

rest-api-abstraction.md

transport-and-networking.md

utilities-and-helpers.md

tile.json