- 新增六层UI自动化架构:从Backend到Capabilities的完整分层实现 - 添加WeChat 4.0分辨率适配Profile与图像模板资源 - 实现幂等缓存、熔断器、重试策略、链路追踪与监控指标 - 新增头像下载安全校验、发布朋友圈路径白名单防护 - 优化密钥缓存、DB校验逻辑与初始化流程 - 补充完整错误码体系与启动清场机制
113 lines
2.7 KiB
Python
113 lines
2.7 KiB
Python
"""Prometheus 指标定义:发送计数/耗时/失败/图像匹配/DB校验/队列/运行态/熔断/缓存/自适应等待。
|
||
|
||
所有指标用 prometheus_client 定义,通过 /metrics 端点暴露(app.py 挂载)。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
import time
|
||
|
||
from prometheus_client import (
|
||
Counter,
|
||
Gauge,
|
||
Histogram,
|
||
Info,
|
||
)
|
||
|
||
logger = logging.getLogger("woc-bridge")
|
||
|
||
# 发送总数(按 flow_name / result 标签)
|
||
send_total = Counter(
|
||
"woc_send_total",
|
||
"Total send operations",
|
||
["flow_name", "result"], # result: success / failed / skipped
|
||
)
|
||
|
||
# 发送耗时分布
|
||
send_duration = Histogram(
|
||
"woc_send_duration_seconds",
|
||
"Send operation duration in seconds",
|
||
["flow_name"],
|
||
buckets=(0.5, 1.0, 2.0, 5.0, 10.0, 30.0, 60.0),
|
||
)
|
||
|
||
# 按失败状态分类的失败计数
|
||
send_failed_by_state = Counter(
|
||
"woc_send_failed_by_state_total",
|
||
"Send failures by flow state",
|
||
["flow_name", "state"],
|
||
)
|
||
|
||
# 图像匹配置信度分布
|
||
image_match_confidence = Histogram(
|
||
"woc_image_match_confidence",
|
||
"OpenCV template match confidence",
|
||
["kind"],
|
||
buckets=(0.5, 0.7, 0.8, 0.85, 0.9, 0.95, 1.0),
|
||
)
|
||
|
||
# DB 校验耗时
|
||
db_verify_duration = Histogram(
|
||
"woc_db_verify_duration_seconds",
|
||
"DB verify duration in seconds",
|
||
buckets=(0.1, 0.2, 0.5, 1.0, 2.0, 3.0, 5.0),
|
||
)
|
||
|
||
# 发送队列待处理数
|
||
send_queue_pending = Gauge(
|
||
"woc_send_queue_pending",
|
||
"Send queue pending count",
|
||
)
|
||
|
||
# WeChat 运行态(1=running, 0=not running)
|
||
wechat_running = Gauge(
|
||
"woc_wechat_running",
|
||
"WeChat process running state (1=running, 0=not)",
|
||
)
|
||
|
||
# 熔断器状态(0=closed, 1=open, 2=half_open)
|
||
circuit_state = Gauge(
|
||
"woc_circuit_state",
|
||
"Circuit breaker state (0=closed, 1=open, 2=half_open)",
|
||
["name"],
|
||
)
|
||
|
||
# 会话缓存命中
|
||
session_cache_hit = Counter(
|
||
"woc_session_cache_hit_total",
|
||
"Session cache hit count",
|
||
["result"], # hit / miss
|
||
)
|
||
|
||
# 自适应等待超时计数
|
||
adaptive_wait_timeout = Counter(
|
||
"woc_adaptive_wait_timeout_total",
|
||
"Adaptive wait timeout count",
|
||
["kind"],
|
||
)
|
||
|
||
# 首次发送耗时(无缓存)
|
||
send_duration_first = Histogram(
|
||
"woc_send_duration_first_seconds",
|
||
"First send duration (no session cache) in seconds",
|
||
buckets=(1.0, 3.0, 5.0, 10.0, 30.0, 60.0),
|
||
)
|
||
|
||
# 缓存命中后的发送耗时
|
||
send_duration_cached = Histogram(
|
||
"woc_send_duration_cached_seconds",
|
||
"Cached send duration (session cache hit) in seconds",
|
||
buckets=(0.5, 1.0, 2.0, 5.0, 10.0, 30.0),
|
||
)
|
||
|
||
|
||
def set_circuit_state(name: str, state_int: int) -> None:
|
||
"""更新熔断器状态指标。
|
||
|
||
Args:
|
||
name: 熔断器名称
|
||
state_int: 0=closed, 1=open, 2=half_open
|
||
"""
|
||
circuit_state.labels(name=name).set(state_int)
|