Monitoring¶
Metrics collection and exporter integrations.
Usage¶
from symphra_cache import CacheManager, CacheMonitor
from symphra_cache.monitoring.prometheus import PrometheusExporter
from symphra_cache.monitoring.statsd import StatsDExporter
# Create cache and monitor
cache = CacheManager.from_config({"backend": "memory"})
monitor = CacheMonitor(cache)
# Do some operations
cache.set("user:1", {"name": "Alice"})
cache.get("user:1")
# Unified metrics interface
metrics = monitor.metrics
print(metrics.get_latency_stats("get")) # {"min": ..., "max": ..., "avg": ...}
# Prometheus exporter (text format)
prom = PrometheusExporter(monitor, namespace="myapp", subsystem="cache")
print(prom.generate_metrics())
# StatsD exporter (send to server)
# Note: requires a reachable StatsD server if you call send_metrics()
statsd = StatsDExporter(monitor, prefix="myapp.cache")
# await statsd.send_metrics() # in an async context
Metrics Interface¶
CacheMonitor.is_enabled()toggles monitoring overhead on/off.CacheMonitor.metricsprovides adapter fields:get_count,set_count,delete_count,hit_count,miss_count.get_hit_rate()andget_total_operations()return hit rate and total operations.get_average_latency(operation)returns average latency in milliseconds forget/set.get_latency_stats(operation)returns{min, max, avg}in milliseconds forget/set.
缓存监控器
提供缓存统计、监控和健康检查功能。 线程安全。
使用示例: >>> cache = CacheManager.from_config({"backend": "memory"}) >>> monitor = CacheMonitor(cache) >>> >>> # 执行缓存操作 >>> cache.set("key", "value") >>> cache.get("key") >>> >>> # 查看统计 >>> stats = monitor.get_stats() >>> print(f"命中率: {stats.hit_rate:.2%}")
Source code in src/symphra_cache/monitor.py
169 170 171 172 173 174 175 176 177 178 179 180 181 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
metrics
property
¶
提供与导出器兼容的指标接口
__init__(cache_manager, *, enabled=True)
¶
初始化监控器
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_manager
|
CacheManager
|
缓存管理器实例 |
required |
enabled
|
bool
|
是否启用监控(禁用时性能开销为零) |
True
|
Source code in src/symphra_cache/monitor.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
__repr__()
¶
字符串表示
Source code in src/symphra_cache/monitor.py
401 402 403 404 405 406 407 408 409 | |
check_health()
¶
执行健康检查
Returns:
| Type | Description |
|---|---|
dict
|
健康检查结果字典 |
示例: >>> health = monitor.check_health() >>> if health["healthy"]: ... print("缓存健康")
Source code in src/symphra_cache/monitor.py
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 | |
get_stats()
¶
获取统计信息
Returns:
| Type | Description |
|---|---|
CacheStats
|
CacheStats 对象(副本) |
示例: >>> stats = monitor.get_stats() >>> print(f"命中率: {stats.hit_rate:.2%}") >>> print(f"平均响应时间: {stats.avg_get_time:.2f}ms")
Source code in src/symphra_cache/monitor.py
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
get_summary()
¶
获取监控摘要
返回统计信息和健康状态的汇总。
Returns:
| Type | Description |
|---|---|
dict
|
摘要字典 |
示例: >>> summary = monitor.get_summary() >>> print(summary)
Source code in src/symphra_cache/monitor.py
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 | |
is_enabled()
¶
是否启用监控(为导出器兼容提供)
Source code in src/symphra_cache/monitor.py
213 214 215 | |
reset_stats()
¶
重置统计信息
保留 start_time,更新 last_reset。
示例
monitor.reset_stats() # 重新开始统计
Source code in src/symphra_cache/monitor.py
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 | |
缓存统计信息
所有计数器都是累积值,可通过 reset() 重置。
Source code in src/symphra_cache/monitor.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 | |
avg_get_time
property
¶
平均 get 操作耗时(毫秒)
avg_set_time
property
¶
平均 set 操作耗时(毫秒)
hit_rate
property
¶
命中率(0-1)
miss_rate
property
¶
未命中率(0-1)
uptime
property
¶
运行时间(秒)
to_dict()
¶
转换为字典
Source code in src/symphra_cache/monitor.py
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 | |
Prometheus 指标导出器
将缓存监控指标转换为 Prometheus 格式。
支持的指标类型: - Counter: 累积计数器(操作次数) - Gauge: 瞬时值(缓存大小、命中率) - Histogram: 分布统计(延迟分布)
使用示例: >>> exporter = PrometheusExporter(monitor) >>> metrics_text = exporter.generate_metrics()
Source code in src/symphra_cache/monitoring/prometheus.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 180 181 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
__init__(monitor, namespace='symphra_cache', subsystem='cache', labels=None)
¶
初始化 Prometheus 导出器
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor
|
CacheMonitor
|
缓存监控器 |
required |
namespace
|
str
|
指标命名空间 |
'symphra_cache'
|
subsystem
|
str
|
子系统名称 |
'cache'
|
labels
|
dict[str, str] | None
|
全局标签 |
None
|
Source code in src/symphra_cache/monitoring/prometheus.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
create_pushgateway_client(gateway_url, job_name, instance='')
¶
创建 Pushgateway 客户端
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
gateway_url
|
str
|
Pushgateway URL |
required |
job_name
|
str
|
作业名称 |
required |
instance
|
str
|
实例标识符 |
''
|
Returns:
| Type | Description |
|---|---|
PrometheusPushgatewayClient
|
Pushgateway 客户端 |
Source code in src/symphra_cache/monitoring/prometheus.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | |
generate_metrics()
¶
生成 Prometheus 格式的指标文本
Returns:
| Type | Description |
|---|---|
str
|
Prometheus 指标文本 |
Source code in src/symphra_cache/monitoring/prometheus.py
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
get_metrics_handler()
¶
获取指标处理器函数
Returns:
| Type | Description |
|---|---|
Callable[[], str]
|
返回指标文本的函数 |
Source code in src/symphra_cache/monitoring/prometheus.py
286 287 288 289 290 291 292 293 | |
update_labels(labels)
¶
更新全局标签
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
labels
|
dict[str, str]
|
新的标签字典 |
required |
Source code in src/symphra_cache/monitoring/prometheus.py
333 334 335 336 337 338 339 340 | |
StatsD 指标导出器
将缓存监控指标转换为 StatsD 格式并通过 UDP 发送。
支持的指标类型: - Counter: 计数器(操作次数) - Timer: 计时器(延迟) - Gauge: 瞬时值(缓存大小、命中率)
使用示例: >>> exporter = StatsDExporter(monitor, host="localhost", port=8125) >>> await exporter.send_metrics()
Source code in src/symphra_cache/monitoring/statsd.py
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 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 180 181 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 | |
__aenter__()
async
¶
异步上下文管理器入口
Source code in src/symphra_cache/monitoring/statsd.py
405 406 407 408 | |
__aexit__(exc_type, exc_val, exc_tb)
async
¶
异步上下文管理器出口
Source code in src/symphra_cache/monitoring/statsd.py
410 411 412 413 414 415 416 417 | |
__del__()
¶
析构函数
Source code in src/symphra_cache/monitoring/statsd.py
419 420 421 422 | |
__init__(monitor, host='localhost', port=8125, prefix='symphra.cache', sample_rate=1.0, protocol='udp', batch_size=10)
¶
初始化 StatsD 导出器
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monitor
|
CacheMonitor
|
缓存监控器 |
required |
host
|
str
|
StatsD 服务器主机 |
'localhost'
|
port
|
int
|
StatsD 服务器端口 |
8125
|
prefix
|
str
|
指标前缀 |
'symphra.cache'
|
sample_rate
|
float
|
采样率 (0.0-1.0) |
1.0
|
protocol
|
str
|
传输协议 ("udp", "tcp") |
'udp'
|
batch_size
|
int
|
批量发送大小 |
10
|
Source code in src/symphra_cache/monitoring/statsd.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
add_custom_metric(name, value, metric_type='g')
¶
添加自定义指标
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
指标名称 |
required |
value
|
float
|
指标值 |
required |
metric_type
|
str
|
指标类型 ("c", "g", "ms") |
'g'
|
Source code in src/symphra_cache/monitoring/statsd.py
362 363 364 365 366 367 368 369 370 371 372 | |
connect()
async
¶
建立到 StatsD 服务器的连接
Source code in src/symphra_cache/monitoring/statsd.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
disconnect()
async
¶
断开连接
Source code in src/symphra_cache/monitoring/statsd.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
flush_pending_metrics()
async
¶
刷新待发送的指标
Returns:
| Type | Description |
|---|---|
bool
|
刷新是否成功 |
Source code in src/symphra_cache/monitoring/statsd.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | |
generate_all_metrics()
¶
生成所有指标
Returns:
| Type | Description |
|---|---|
list[str]
|
指标行列表 |
Source code in src/symphra_cache/monitoring/statsd.py
333 334 335 336 337 338 339 340 341 342 343 344 | |
get_connection_status()
¶
获取连接状态
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
连接状态信息 |
Source code in src/symphra_cache/monitoring/statsd.py
390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
schedule_periodic_send(interval=30.0)
async
¶
安排周期性发送指标
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
发送间隔(秒) |
30.0
|
Source code in src/symphra_cache/monitoring/statsd.py
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | |
send_metrics(metric_lines=None)
async
¶
发送指标到 StatsD 服务器
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metric_lines
|
list[str] | None
|
要发送的指标行列表,None 表示发送所有指标 |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
发送是否成功 |
Source code in src/symphra_cache/monitoring/statsd.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |