proxywhirl.metrics_collector¶
Prometheus metrics collector for ProxyWhirl.
This module provides centralized Prometheus metrics collection for monitoring proxy pool health, request performance, and circuit breaker states.
Metrics exposed: - proxywhirl_requests_total: Counter for total requests - proxywhirl_request_duration_seconds: Histogram for request latency - proxywhirl_proxy_health_status: Gauge for proxy health (1=healthy, 0=unhealthy) - proxywhirl_active_proxies: Gauge for number of active proxies - proxywhirl_circuit_breaker_state: Gauge for circuit breaker states
- Usage:
from proxywhirl.metrics_collector import MetricsCollector
collector = MetricsCollector() collector.record_request(“success”, duration=1.5, proxy_id=”proxy-1”) collector.update_proxy_health(“proxy-1”, is_healthy=True)
Classes¶
Centralized Prometheus metrics collector for ProxyWhirl. |
Functions¶
|
Clear all metrics for a specific proxy. |
Get the global MetricsCollector singleton. |
|
|
Record a request made through a proxy. |
Reset the global MetricsCollector singleton. |
|
|
Update the count of active proxies in the pool. |
|
Update circuit breaker state for a specific proxy. |
|
Update health status for a specific proxy. |
Module Contents¶
- class proxywhirl.metrics_collector.MetricsCollector(registry=None)[source]¶
Centralized Prometheus metrics collector for ProxyWhirl.
This class provides a clean API for recording metrics throughout the application. All metrics follow Prometheus naming conventions and best practices.
Initialize Prometheus metrics collectors.
- Parameters:
registry (prometheus_client.CollectorRegistry | None) – Optional custom registry. If None, uses the default REGISTRY. This allows for isolated testing without metric collisions.
- clear_proxy_metrics(proxy_id)[source]¶
Clear all metrics for a specific proxy.
This should be called when a proxy is removed from the pool to prevent stale metrics from persisting.
- Parameters:
proxy_id (str) – Identifier of the proxy to clear
- Return type:
None
- update_active_proxies(count)[source]¶
Update the count of active proxies in the pool.
- Parameters:
count (int) – Number of active proxies
- Return type:
None
- proxywhirl.metrics_collector.clear_proxy_metrics(proxy_id)[source]¶
Clear all metrics for a specific proxy.
Convenience function that uses the global collector.
- Parameters:
proxy_id (str) – Identifier of the proxy to clear
- Return type:
None
- proxywhirl.metrics_collector.get_metrics_collector()[source]¶
Get the global MetricsCollector singleton.
- Returns:
The global metrics collector instance
- Return type:
- proxywhirl.metrics_collector.record_request(status, duration, proxy_id)[source]¶
Record a request made through a proxy.
Convenience function that uses the global collector.
- proxywhirl.metrics_collector.reset_metrics_collector()[source]¶
Reset the global MetricsCollector singleton.
This is primarily used for testing to ensure test isolation. Calling this function clears the global collector reference, allowing a new instance to be created on next access.
- Return type:
None
- proxywhirl.metrics_collector.update_active_proxies(count)[source]¶
Update the count of active proxies in the pool.
Convenience function that uses the global collector.
- Parameters:
count (int) – Number of active proxies
- Return type:
None
- proxywhirl.metrics_collector.update_circuit_breaker_state(proxy_id, state)[source]¶
Update circuit breaker state for a specific proxy.
Convenience function that uses the global collector.
- Parameters:
proxy_id (str) – Identifier of the proxy
state (Literal['closed', 'open', 'half-open']) – Circuit breaker state (closed=0, open=1, half-open=2)
- Return type:
None