ccproxy.observability.context¶
ccproxy.observability.context
¶
Request context management with timing and correlation.
This module provides context managers and utilities for tracking request lifecycle, timing measurements, and correlation across async operations. Uses structlog for rich business event logging.
Key features: - Accurate timing measurement using time.perf_counter() - Request correlation with unique IDs - Structured logging integration - Async-safe context management - Exception handling and error tracking
RequestContext
dataclass
¶
RequestContext(
request_id,
start_time,
logger,
metadata=dict(),
storage=None,
)
ContextTracker
¶
Thread-safe tracker for managing active request contexts.
Useful for tracking concurrent requests and their states, especially for metrics like active request counts.
Source code in ccproxy/observability/context.py
add_context
async
¶
remove_context
async
¶
get_context
async
¶
get_active_count
async
¶
get_all_contexts
async
¶
cleanup_stale_contexts
async
¶
Remove contexts older than max_age_seconds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
max_age_seconds
|
float
|
Maximum age in seconds before considering stale |
300
|
Returns:
Type | Description |
---|---|
int
|
Number of contexts removed |
Source code in ccproxy/observability/context.py
request_context
async
¶
Context manager for tracking complete request lifecycle with timing.
Automatically logs request start/success/error events with accurate timing. Provides structured logging with request correlation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str | None
|
Unique request identifier (generated if not provided) |
None
|
storage
|
Any | None
|
Optional storage backend for access logs |
None
|
metrics
|
Any | None
|
Optional PrometheusMetrics instance for active request tracking |
None
|
**initial_context
|
Any
|
Initial context to include in all log events |
{}
|
Yields:
Name | Type | Description |
---|---|---|
RequestContext |
AsyncGenerator[RequestContext, None]
|
Context object with timing and logging capabilities |
Example
async with request_context(method="POST", path="/v1/messages") as ctx: ctx.add_metadata(model="claude-3-5-sonnet") # Process request ctx.log_event("request_processed", tokens=150) # Context automatically logs success with timing
Source code in ccproxy/observability/context.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
|
timed_operation
async
¶
Context manager for timing individual operations within a request.
Useful for measuring specific parts of request processing like API calls, database queries, or data processing steps.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
operation_name
|
str
|
Name of the operation being timed |
required |
request_id
|
str | None
|
Associated request ID for correlation |
None
|
**context
|
Any
|
Additional context for logging |
{}
|
Yields:
Type | Description |
---|---|
AsyncGenerator[dict[str, Any], None]
|
Dict with timing information and logger |
Example
async with timed_operation("claude_api_call", request_id=ctx.request_id) as op: response = await api_client.call() op["response_size"] = len(response) # Automatically logs operation timing
Source code in ccproxy/observability/context.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
|
get_context_tracker
¶
Get or create global context tracker.
tracked_request_context
async
¶
Request context manager that also tracks active requests globally.
Combines request_context() with automatic tracking in the global context tracker for monitoring active request counts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
request_id
|
str | None
|
Unique request identifier |
None
|
**initial_context
|
Any
|
Initial context to include in log events |
{}
|
Yields:
Name | Type | Description |
---|---|---|
RequestContext |
AsyncGenerator[RequestContext, None]
|
Context object with timing and logging |