Profiling
provide.foundation.profiling
¶
TODO: Add module docstring.
Classes¶
ExporterError
¶
ExporterError(
message: str,
*,
exporter_name: str | None = None,
endpoint: str | None = None,
retry_count: int | None = None,
**kwargs: Any
)
Bases: ProfilingError
Raised when metric export operations fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Export error message. |
required |
exporter_name
|
str | None
|
Optional name of the exporter that failed. |
None
|
endpoint
|
str | None
|
Optional endpoint URL that failed. |
None
|
retry_count
|
int | None
|
Optional number of retries attempted. |
None
|
**kwargs
|
Any
|
Additional context passed to ProfilingError. |
{}
|
Examples:
>>> raise ExporterError("Failed to connect to Prometheus")
>>> raise ExporterError("Export timeout", exporter_name="datadog", retry_count=3)
Source code in provide/foundation/errors/profiling.py
MetricsError
¶
MetricsError(
message: str,
*,
metric_name: str | None = None,
metric_value: Any = None,
**kwargs: Any
)
Bases: ProfilingError
Raised when metrics collection operations fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Metrics error message. |
required |
metric_name
|
str | None
|
Optional name of the metric that failed. |
None
|
metric_value
|
Any
|
Optional value that caused the error. |
None
|
**kwargs
|
Any
|
Additional context passed to ProfilingError. |
{}
|
Examples:
>>> raise MetricsError("Invalid metric value")
>>> raise MetricsError("Metric overflow", metric_name="latency_ms")
Source code in provide/foundation/errors/profiling.py
ProfileMetrics
¶
Thread-safe metrics collection for profiling Foundation performance.
Tracks message processing performance, emoji overhead, and throughput metrics for Foundation's logging infrastructure.
Example
metrics = ProfileMetrics() metrics.record_message(duration_ns=1500000, has_emoji=True, field_count=5) print(f"Avg latency: {metrics.avg_latency_ms:.2f}ms") print(f"Throughput: {metrics.messages_per_second:.0f} msg/sec")
Initialize metrics with zero values and current timestamp.
Source code in provide/foundation/profiling/metrics.py
Attributes¶
avg_fields_per_message
property
¶
Calculate average number of fields per message.
avg_latency_ms
property
¶
Calculate average processing latency in milliseconds.
emoji_overhead_percent
property
¶
Calculate percentage of messages with emoji processing.
messages_per_second
property
¶
Calculate messages per second since start time.
Functions¶
record_message
¶
Record a processed message with timing and metadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration_ns
|
int
|
Processing duration in nanoseconds |
required |
has_emoji
|
bool
|
Whether the message contained emoji processing |
required |
field_count
|
int
|
Number of fields in the log event |
required |
Source code in provide/foundation/profiling/metrics.py
reset
¶
Reset all metrics to initial values with new start time.
Source code in provide/foundation/profiling/metrics.py
to_dict
¶
Serialize metrics to dictionary for JSON output.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary containing all current metrics |
Source code in provide/foundation/profiling/metrics.py
ProfilingComponent
¶
Hub component for managing Foundation performance profiling.
Integrates profiling functionality into Foundation's Hub architecture, providing centralized configuration and lifecycle management for performance monitoring.
Example
from provide.foundation.hub import Hub from provide.foundation.profiling import register_profiling
hub = Hub() register_profiling(hub) profiler = hub.get_component("profiler") profiler.enable()
Metrics are automatically collected¶
metrics = profiler.get_metrics() print(f"Throughput: {metrics.messages_per_second:.0f} msg/sec")
Initialize profiling component.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_rate
|
float
|
Fraction of messages to sample for metrics |
DEFAULT_PROFILING_SAMPLE_RATE
|
Source code in provide/foundation/profiling/component.py
Functions¶
__repr__
¶
String representation for debugging.
Source code in provide/foundation/profiling/component.py
disable
¶
Disable profiling metrics collection.
Source code in provide/foundation/profiling/component.py
enable
¶
Enable profiling metrics collection.
This method is safe to call multiple times - it will not re-enable if already enabled.
Source code in provide/foundation/profiling/component.py
get_metrics
¶
Get current profiling metrics.
Returns:
| Type | Description |
|---|---|
ProfileMetrics
|
Current ProfileMetrics instance with collected data |
reset
¶
Reset profiling metrics to initial values.
Useful for testing and periodic metric collection.
Source code in provide/foundation/profiling/component.py
ProfilingError
¶
ProfilingError(
message: str,
*,
component: str | None = None,
sample_rate: float | None = None,
**kwargs: Any
)
Bases: FoundationError
Raised when profiling operations fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Error message describing the profiling issue. |
required |
component
|
str | None
|
Optional profiling component that caused the error. |
None
|
sample_rate
|
float | None
|
Optional sample rate when the error occurred. |
None
|
**kwargs
|
Any
|
Additional context passed to FoundationError. |
{}
|
Examples:
>>> raise ProfilingError("Profiling initialization failed")
>>> raise ProfilingError("Invalid sample rate", sample_rate=1.5)
Source code in provide/foundation/errors/profiling.py
ProfilingProcessor
¶
Structlog processor that collects performance metrics via sampling.
This processor integrates into Foundation's existing structlog pipeline to collect metrics about message processing performance, emoji overhead, and throughput with configurable sampling to minimize performance impact.
Example
processor = ProfilingProcessor(sample_rate=0.01) # 1% sampling
Add to structlog processor chain¶
processors.append(processor)
Later, get metrics¶
metrics = processor.get_metrics() print(f"Processing {metrics.messages_per_second:.0f} msg/sec")
Initialize profiling processor with sampling configuration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample_rate
|
float
|
Fraction of messages to sample (0.0 to 1.0) 0.01 = 1% sampling for minimal overhead |
DEFAULT_PROFILING_SAMPLE_RATE
|
Source code in provide/foundation/profiling/processor.py
Functions¶
__call__
¶
Process log event and optionally collect metrics.
This is the main entry point called by structlog for each log message. Uses sampling to minimize performance overhead.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Any
|
The logger instance (unused) |
required |
method_name
|
str
|
The logging method name (unused) |
required |
event_dict
|
EventDict
|
The event dictionary to process |
required |
Returns:
| Type | Description |
|---|---|
EventDict
|
The event_dict unchanged (pass-through processor) |
Source code in provide/foundation/profiling/processor.py
disable
¶
enable
¶
get_metrics
¶
SamplingError
¶
SamplingError(
message: str,
*,
sample_rate: float | None = None,
samples_processed: int | None = None,
**kwargs: Any
)
Bases: ProfilingError
Raised when sampling operations fail.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Sampling error message. |
required |
sample_rate
|
float | None
|
The sample rate that caused the error. |
None
|
samples_processed
|
int | None
|
Optional number of samples processed. |
None
|
**kwargs
|
Any
|
Additional context passed to ProfilingError. |
{}
|
Examples:
>>> raise SamplingError("Invalid sample rate", sample_rate=1.5)
>>> raise SamplingError("Sampling buffer overflow", samples_processed=1000)
Source code in provide/foundation/errors/profiling.py
Functions¶
register_profiling
¶
Register profiling component with Hub and add CLI command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hub
|
Hub
|
The Hub instance to register with |
required |
sample_rate
|
float
|
Sampling rate for metrics collection |
DEFAULT_PROFILING_SAMPLE_RATE
|
Example
from provide.foundation.hub import Hub from provide.foundation.profiling.component import register_profiling
hub = Hub() register_profiling(hub)