Observabilityο
Observability and metrics for Confii.
This module provides metrics collection and observability features for monitoring configuration usage, access patterns, and performance.
- class confii.observability.ConfigAccessMetric(key: str, access_count: int = 0, first_access: float | None = None, last_access: float | None = None, avg_access_time: float = 0.0, total_access_time: float = 0.0)[source]ο
Bases:
objectRepresents a configuration access metric.
This dataclass tracks metrics for a single configuration key, including access frequency and timing information.
- class confii.observability.ConfigEventEmitter[source]ο
Bases:
objectEvent emitter for configuration events.
This class provides a simple event emitter pattern for configuration events, allowing listeners to subscribe to configuration changes, reloads, and other events. Supports multiple listeners per event type.
- _listenersο
Dictionary mapping event names to lists of callback functions
Example
>>> from confii import Config >>> config = Config(loaders=[YamlLoader("config.yaml")]) >>> emitter = config.enable_events() >>> >>> @emitter.on("reload") ... def handle_reload(new_config, duration): ... print(f"Config reloaded in {duration:.3f}s") >>> >>> @emitter.on("change") ... def handle_change(old_config, new_config): ... print("Configuration changed")
- __init__() None[source]ο
Initialize the event emitter.
Creates a new event emitter with an empty listener registry.
- emit(event: str, *args: Any, **kwargs: Any) None[source]ο
Emit an event to all listeners.
- Parameters:
event β Event name
*args β Positional arguments for listeners
**kwargs β Keyword arguments for listeners
Example
>>> emitter.emit("reload", config_dict)
- off(event: str, callback: Callable) None[source]ο
Unregister an event listener.
- Parameters:
event β Event name
callback β Callback function to remove
- on(event: str, callback: Callable | None = None) Callable[source]ο
Register an event listener.
Can be used as a decorator or called directly:
# As decorator @emitter.on(βreloadβ) def handle_reload():
print(βConfiguration reloadedβ)
# Direct call emitter.on(βreloadβ, handle_reload)
- Parameters:
event β Event name (e.g., βreloadβ, βchangeβ, βaccessβ)
callback β Callback function. If None, returns a decorator.
- Returns:
The callback function (for decorator chaining)
- class confii.observability.ConfigMetrics(total_keys: int = 0, accessed_keys: int = 0, reload_count: int = 0, last_reload: float | None = None, reload_durations: List[float] = <factory>, access_metrics: Dict[str, ~confii.observability.ConfigAccessMetric]=<factory>, change_count: int = 0, last_change: float | None = None)[source]ο
Bases:
objectConfiguration metrics and statistics.
This dataclass aggregates metrics for configuration usage, including access patterns, reload frequency, and change tracking.
- reload_durationsο
List of reload durations (in seconds) for performance tracking
- Type:
List[float]
- access_metricsο
Dictionary mapping key paths to ConfigAccessMetric instances
- Type:
- MAX_RELOAD_DURATIONS = 1000ο
- __init__(total_keys: int = 0, accessed_keys: int = 0, reload_count: int = 0, last_reload: float | None = None, reload_durations: List[float] = <factory>, access_metrics: Dict[str, ~confii.observability.ConfigAccessMetric]=<factory>, change_count: int = 0, last_change: float | None = None) Noneο
- get_statistics() Dict[str, Any][source]ο
Get metrics statistics.
- Returns:
Dictionary with metrics statistics
- class confii.observability.ConfigObserver[source]ο
Bases:
objectObserver for configuration access and changes.
This class tracks configuration usage patterns, access frequencies, and performance metrics for observability purposes. It can be enabled on a Config instance to automatically collect metrics during normal usage.
- metricsο
ConfigMetrics instance containing collected metrics
- _enabledο
Whether metrics collection is currently enabled
Example
>>> from confii import Config >>> config = Config(loaders=[YamlLoader("config.yaml")]) >>> observer = config.enable_observability() >>> # Use config normally - metrics are collected automatically >>> host = config.database.host >>> metrics = observer.get_statistics() >>> print(f"Config accessed {metrics['accessed_keys']} times")
- __init__() None[source]ο
Initialize the config observer.
Creates a new observer instance with metrics collection enabled by default.
- disable() None[source]ο
Disable metrics collection.
When disabled, the observer stops tracking metrics but retains previously collected data.
- enable() None[source]ο
Enable metrics collection.
When enabled, the observer will track all configuration access, reloads, and changes.
- get_metrics() ConfigMetrics[source]ο
Get current metrics.
- Returns:
ConfigMetrics instance
- get_statistics() Dict[str, Any][source]ο
Get metrics statistics.
Returns a dictionary containing aggregated statistics about configuration usage, including access patterns, reload performance, and top accessed keys.
- Returns:
total_keys: Total number of configuration keys
accessed_keys: Number of unique keys accessed
access_rate: Ratio of accessed keys to total keys
reload_count: Number of times config was reloaded
avg_reload_time: Average reload duration in seconds
change_count: Number of configuration changes
top_accessed_keys: List of most frequently accessed keys
- Return type:
Dictionary with metrics statistics including
Example
>>> stats = observer.get_statistics() >>> print(f"Top accessed key: {stats['top_accessed_keys'][0]['key']}") >>> print(f"Average reload time: {stats['avg_reload_time']:.3f}s")
- record_key_access(key: str, access_time: float = 0.0) None[source]ο
Record a configuration key access.
- Parameters:
key β Configuration key accessed
access_time β Time taken to access (in seconds)