Monitoring¶
novelentitymatcher.monitoring.metrics
¶
Metrics schema and utilities for library-centric observability.
Provides MetricEvent dataclass and helper functions for emitting metrics through user-provided callbacks. Works without external dependencies and is suitable for library usage patterns.
Classes¶
MetricEvent(name, value, unit, labels, timestamp)
dataclass
¶
Standardized metric event structure.
Can be emitted from any component (matcher, stages, detectors) and sent to user-provided callbacks for custom handling.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Metric name (e.g., "match_latency", "novelty_rate") |
value |
float
|
Numeric value of the metric |
unit |
str
|
Unit of measurement (e.g., "seconds", "count", "ratio") |
labels |
dict[str, str]
|
Key-value pairs for categorization (e.g., {"stage": "ood"}) |
timestamp |
datetime
|
When the metric was recorded |
Functions¶
create_metric(name, value, unit, labels=None)
¶
Helper to create MetricEvent with current timestamp.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Metric name |
required |
value
|
float
|
Numeric value |
required |
unit
|
str
|
Unit of measurement |
required |
labels
|
dict[str, str] | None
|
Optional labels dictionary |
None
|
Returns:
| Type | Description |
|---|---|
MetricEvent
|
MetricEvent with current timestamp |
Source code in src/novelentitymatcher/monitoring/metrics.py
get_metric_summary(events)
¶
Summarize metric events by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
events
|
list[MetricEvent]
|
List of MetricEvent instances |
required |
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, float]]
|
Dictionary mapping metric names to summary statistics |
dict[str, dict[str, float]]
|
{metric_name: {"count": N, "sum": X, "mean": Y, ...}} |
Source code in src/novelentitymatcher/monitoring/metrics.py
novelentitymatcher.monitoring.performance
¶
Performance monitoring utilities for semantic matchers.
Classes¶
PerformanceMonitor()
¶
Simple performance tracking for matchers and other components.
Provides detailed metrics for different operations.
Example
monitor = PerformanceMonitor()
with monitor.track("match_operation"): result = matcher.match(query)
logger.info(monitor.summary())
Source code in src/novelentitymatcher/monitoring/performance.py
Functions¶
record(operation, duration)
¶
Record a timing for an operation.
track(operation)
¶
Context manager for tracking operation timing.
summary()
¶
Return summary statistics for all tracked operations.
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, float]]
|
Dict mapping operation names to statistics: - count: Number of recordings - total: Total time - mean: Average time - min: Minimum time - max: Maximum time |
Source code in src/novelentitymatcher/monitoring/performance.py
reset()
¶
get_operation_metrics(operation)
¶
Get metrics for a specific operation.
to_dict()
¶
export_json(filepath)
¶
Export all metrics to JSON file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to output JSON file |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to saved file |
Source code in src/novelentitymatcher/monitoring/performance.py
export_csv(filepath)
¶
Export metrics to CSV file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filepath
|
str
|
Path to output CSV file |
required |
Returns:
| Type | Description |
|---|---|
Path
|
Path to saved file |
Source code in src/novelentitymatcher/monitoring/performance.py
Functions¶
track_performance(func)
¶
Decorator to track method performance metrics.
Tracks
- Number of calls
- Total time
- Average time per call
- Last call duration
Usage
@track_performance def match(self, query, top_k=5): ...
Access metrics¶
matcher._metrics # {'calls': 10, 'total_time': 1.5, 'avg_time': 0.15}