ccproxy.observability.metrics¶
ccproxy.observability.metrics
¶
Prometheus metrics for operational monitoring.
This module provides direct prometheus_client integration for fast operational metrics like request counts, response times, and resource usage. These metrics are optimized for real-time monitoring and alerting.
Key features: - Thread-safe metric operations using prometheus_client - Minimal overhead for high-frequency operations - Standard Prometheus metric types (Counter, Histogram, Gauge) - Automatic label management and validation - Pushgateway integration for batch metric pushing
PrometheusMetrics
¶
Prometheus metrics collector for operational monitoring.
Provides thread-safe, high-performance metrics collection using prometheus_client. Designed for minimal overhead in request processing hot paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
namespace
|
str
|
Metric name prefix |
'ccproxy'
|
registry
|
CollectorRegistry | None
|
Custom Prometheus registry (uses default if None) |
None
|
pushgateway_client
|
Any | None
|
Optional pushgateway client for dependency injection |
None
|
Source code in ccproxy/observability/metrics.py
record_request
¶
Record a request event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
HTTP method (GET, POST, etc.) |
required |
endpoint
|
str
|
API endpoint path |
required |
model
|
str | None
|
Model name used |
None
|
status
|
str | int
|
Response status code or status string |
'unknown'
|
service_type
|
str | None
|
Service type (claude_sdk_service, proxy_service) |
None
|
Source code in ccproxy/observability/metrics.py
record_response_time
¶
Record response time.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
duration_seconds
|
float
|
Response time in seconds |
required |
model
|
str | None
|
Model name used |
None
|
endpoint
|
str
|
API endpoint |
'unknown'
|
service_type
|
str | None
|
Service type (claude_sdk_service, proxy_service) |
None
|
Source code in ccproxy/observability/metrics.py
record_tokens
¶
Record token usage.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
token_count
|
int
|
Number of tokens |
required |
token_type
|
str
|
Type of tokens (input, output, cache_read, cache_write) |
required |
model
|
str | None
|
Model name |
None
|
service_type
|
str | None
|
Service type (claude_sdk_service, proxy_service) |
None
|
Source code in ccproxy/observability/metrics.py
record_cost
¶
Record cost.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cost_usd
|
float
|
Cost in USD |
required |
model
|
str | None
|
Model name |
None
|
cost_type
|
str
|
Type of cost (input, output, cache, total) |
'total'
|
service_type
|
str | None
|
Service type (claude_sdk_service, proxy_service) |
None
|
Source code in ccproxy/observability/metrics.py
record_error
¶
Record an error event.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error_type
|
str
|
Type/name of error |
required |
endpoint
|
str
|
API endpoint where error occurred |
'unknown'
|
model
|
str | None
|
Model name if applicable |
None
|
service_type
|
str | None
|
Service type (claude_sdk_service, proxy_service) |
None
|
Source code in ccproxy/observability/metrics.py
set_active_requests
¶
Set the current number of active requests.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
count
|
int
|
Number of active requests |
required |
inc_active_requests
¶
dec_active_requests
¶
update_system_info
¶
is_enabled
¶
push_to_gateway
¶
Push current metrics to Pushgateway using official prometheus_client methods.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
method
|
str
|
Push method - "push" (replace), "pushadd" (add), or "delete" |
'push'
|
Returns:
Type | Description |
---|---|
bool
|
True if push succeeded, False otherwise |
Source code in ccproxy/observability/metrics.py
push_add_to_gateway
¶
Add current metrics to existing job/instance in Pushgateway (pushadd operation).
This is useful when you want to add metrics without replacing existing ones.
Returns:
Type | Description |
---|---|
bool
|
True if push succeeded, False otherwise |
Source code in ccproxy/observability/metrics.py
delete_from_gateway
¶
Delete all metrics for the configured job from Pushgateway.
This removes all metrics associated with the job, useful for cleanup.
Returns:
Type | Description |
---|---|
bool
|
True if delete succeeded, False otherwise |
Source code in ccproxy/observability/metrics.py
is_pushgateway_enabled
¶
Check if Pushgateway client is enabled and configured.
get_metrics
¶
Get or create global metrics instance with dependency injection.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
namespace
|
str
|
Metric namespace prefix |
'ccproxy'
|
registry
|
CollectorRegistry | None
|
Custom Prometheus registry |
None
|
pushgateway_client
|
Any | None
|
Optional pushgateway client for dependency injection |
None
|
settings
|
Any | None
|
Optional settings instance to avoid circular imports |
None
|
Returns:
Type | Description |
---|---|
PrometheusMetrics
|
PrometheusMetrics instance with full pushgateway support: |
PrometheusMetrics
|
|
PrometheusMetrics
|
|
PrometheusMetrics
|
|
Source code in ccproxy/observability/metrics.py
reset_metrics
¶
Reset global metrics instance (mainly for testing).