metrics
¶
Metrics layer - analytics, monitoring, and reports.
Analytics
¶
Tracks function performance, success rates, and execution patterns.
Initialize the Analytics instance.
Source code in src/metrics/analytics.py
track
¶
execute_async_wrapped_call
async
¶
Execute an async wrapped function call with analytics tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Async function to execute |
required |
event_type
|
str
|
Type of event for tracking |
required |
*args
|
Any
|
Function arguments |
()
|
**kwargs
|
Any
|
Function keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Function result |
Source code in src/metrics/analytics.py
execute_sync_wrapped_call
¶
Execute a sync wrapped function call with analytics tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Sync function to execute |
required |
event_type
|
str
|
Type of event for tracking |
required |
*args
|
Any
|
Function arguments |
()
|
**kwargs
|
Any
|
Function keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Function result |
Note
Cannot be called from within an active event loop. If you need to call a sync function from async context, call it directly or use an async wrapper.
Source code in src/metrics/analytics.py
execute_wrapped_call
async
¶
Execute a wrapped function call with analytics tracking.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
Function to execute |
required |
event_type
|
str
|
Type of event for tracking |
required |
is_async
|
bool
|
Whether the function is async |
required |
*args
|
Any
|
Function arguments |
()
|
**kwargs
|
Any
|
Function keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Function result |
Source code in src/metrics/analytics.py
batch_mode
async
¶
Context manager that suppresses console logging and shows a spinner instead.
Use this when executing many tracked operations in a loop to avoid flooding the console with individual timing logs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str
|
Message to display next to the spinner |
'Processing...'
|
console
|
Console | None
|
Optional Rich Console instance |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[Status]
|
Rich Status object that can be updated with progress info |
Example
async with analytics.batch_mode("Fetching tracks...") as status: for i, batch in enumerate(batches): status.update(f"[cyan]Fetching batch {i+1}/{len(batches)}...[/cyan]") await fetch_batch(batch)
Source code in src/metrics/analytics.py
get_stats
¶
Get statistics for analytics data.
Source code in src/metrics/analytics.py
log_summary
¶
Log a summary of analytics data.
Source code in src/metrics/analytics.py
clear_old_events
¶
Clear old events from the analytics log.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
days
|
int
|
Number of days to keep (only applies when compact_time=False) |
7
|
Returns:
| Type | Description |
|---|---|
int
|
Number of events removed |
Note
When compact_time=True, uses count-based pruning (removes oldest half or 1000 events) instead of age-based pruning, since timestamps don't include dates.
Source code in src/metrics/analytics.py
merge_with
¶
Merge analytics data from another Analytics instance.
Source code in src/metrics/analytics.py
generate_reports
¶
Generate analytics reports.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
force_mode
|
bool
|
Force report generation even if criteria not met |
False
|
Note
Garbage collection is triggered after report generation if enabled via config (analytics.enable_gc_collect) and event count exceeds threshold.