Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 | 1x 1x 1x 24x 24x 24x 24x 24x 24x 24x 23x 24x 24x 1x 1x 1x 110x 110x 3x 3x 3x 3x 1x 1x 1x 1x 2x 2x 2x 2x 2x 1x 1x 1x 2x 58x 58x 58x 58x 58x 37x 37x 37x 37x 37x 37x 37x 1x 1x 1x 1x 95x 22x 95x 95x 95x 58x 37x 95x 95x 95x 95x 95x 109x 109x 109x 113x 113x 109x 13x 13x 11x 11x 13x 11x 11x 13x 13x 13x 5x 5x 13x 11x 11x 2x 11x 13x 1x 13x 14x 14x 14x 14x 15x 15x 15x 17x 17x 17x 17x 6x 6x 5x 5x 1x 1x 17x 2x 2x 17x 17x 11x 11x 11x 11x 1x 2x 2x 1x 1x 1x 1x 1x 1x | /**
* MetricsMiddleware - 性能指标收集 + 自优化触发机制
*
* v2.4.0 新增:
* - 任务失败率自动分析,超过阈值时触发优化建议
* - 重试策略自动调整(基于历史失败模式)
* - 指标持久化与趋势分析
*
* @module middleware/metrics-middleware
*/
const fs = require('fs');
const path = require('path');
// ─── 默认配置 ────────────────────────────────────────────
const DEFAULT_CONFIG = {
// 失败率阈值:超过此比例触发优化建议
failureRateThreshold: 0.25,
// 连续失败次数:连续失败超过此次数触发优化
consecutiveFailureThreshold: 3,
// 指标存储路径
storagePath: path.join(__dirname, '../../.metrics'),
// 优化建议最大缓存数
maxSuggestionHistory: 50,
// 重试策略配置
retry: {
baseDelay: 1000, // 基础延迟 (ms)
maxRetries: 5, // 最大重试次数
backoffMultiplier: 2, // 退避倍数
maxDelay: 30000, // 最大延迟 (ms)
},
// 触发回调
onOptimizationTriggered: null,
};
// ─── MetricsMiddleware 类 ────────────────────────────────
class MetricsMiddleware {
constructor(config = {}) {
this.config = { ...DEFAULT_CONFIG, ...config };
this.metrics = {
totalTasks: 0,
completedTasks: 0,
failedTasks: 0,
pausedTasks: 0,
cancelledTasks: 0,
// 子技能调用指标
skillCalls: {}, // { skillName: { total, success, failed, avgLatency } }
// 时间序列指标(用于趋势分析)
timeSeries: [], // [{ timestamp, failureRate, avgLatency }]
// 优化建议历史
suggestions: [],
};
this._retryStrategies = new Map(); // taskId -> retry config
this._consecutiveFailures = new Map(); // skillName -> count
this._initStorage();
}
// ── 初始化存储 ──────────────────────────────────────────
_initStorage() {
const dir = this.config.storagePath;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir, { recursive: true });
}
// 加载持久化指标
const metricsFile = path.join(dir, 'metrics.json');
if (fs.existsSync(metricsFile)) {
try {
const saved = JSON.parse(fs.readFileSync(metricsFile, 'utf-8'));
this.metrics = { ...this.metrics, ...saved };
} catch (e) {
// 忽略损坏的存储文件
}
}
}
// ── 持久化 ──────────────────────────────────────────────
save() {
const metricsFile = path.join(this.config.storagePath, 'metrics.json');
fs.writeFileSync(metricsFile, JSON.stringify(this.metrics, null, 2));
}
// ── 中间件接口:execute ─────────────────────────────────
/**
* Middleware 标准接口
* @param {object} context - 任务上下文
* @param {function} next - 下一个中间件
*/
async execute(context, next) {
const startTime = Date.now();
const skillName = context.skillName || 'unknown';
try {
const result = await next(context);
const latency = Date.now() - startTime;
// 记录成功
this.recordSuccess(context.taskId, skillName, latency);
// 重置连续失败计数
this._consecutiveFailures.set(skillName, 0);
return result;
} catch (error) {
const latency = Date.now() - startTime;
// 记录失败
this.recordFailure(context.taskId, skillName, error, latency);
// 检查是否需要触发优化
const analysis = this.analyzeAndOptimize(skillName);
// 自动调整重试策略
const retryConfig = this.getAdaptiveRetryConfig(skillName);
// 如果需要重试且有重试次数剩余
if (retryConfig.shouldRetry && context.retryCount < retryConfig.maxRetries) {
const delay = this._calculateBackoff(skillName, context.retryCount);
context.retryDelay = delay;
context.retryConfig = retryConfig;
}
throw Object.assign(error, {
metricsAnalysis: analysis,
retryConfig,
});
}
}
// ── 指标记录 ────────────────────────────────────────────
recordSuccess(taskId, skillName, latency) {
this.metrics.totalTasks++;
this.metrics.completedTasks++;
this._recordSkillCall(skillName, { success: true, latency });
this._addTimeSeriesPoint();
this.save();
}
recordFailure(taskId, skillName, error, latency) {
this.metrics.totalTasks++;
this.metrics.failedTasks++;
this._recordSkillCall(skillName, { success: false, latency, error: error?.message });
// 更新连续失败计数
const prev = this._consecutiveFailures.get(skillName) || 0;
this._consecutiveFailures.set(skillName, prev + 1);
this._addTimeSeriesPoint();
this.save();
}
recordPause(taskId) {
this.metrics.pausedTasks++;
this.save();
}
recordCancel(taskId) {
this.metrics.cancelledTasks++;
this.save();
}
// ── 内部:记录技能调用 ──────────────────────────────────
_recordSkillCall(skillName, { success, latency, error }) {
if (!this.metrics.skillCalls[skillName]) {
this.metrics.skillCalls[skillName] = {
total: 0, success: 0, failed: 0, totalLatency: 0,
};
}
const call = this.metrics.skillCalls[skillName];
call.total++;
if (success) {
call.success++;
} else {
call.failed++;
}
call.totalLatency += latency;
}
// ── 内部:添加时间序列点 ────────────────────────────────
_addTimeSeriesPoint() {
const failureRate = this.metrics.totalTasks > 0
? this.metrics.failedTasks / this.metrics.totalTasks
: 0;
const avgLatency = this._getOverallAvgLatency();
this.metrics.timeSeries.push({
timestamp: new Date().toISOString(),
failureRate: Math.round(failureRate * 1000) / 1000,
avgLatency: Math.round(avgLatency),
});
// 保留最近 100 个点
Iif (this.metrics.timeSeries.length > 100) {
this.metrics.timeSeries = this.metrics.timeSeries.slice(-100);
}
}
_getOverallAvgLatency() {
let totalLatency = 0;
let totalCalls = 0;
for (const skill of Object.values(this.metrics.skillCalls)) {
totalLatency += skill.totalLatency;
totalCalls += skill.total;
}
return totalCalls > 0 ? totalLatency / totalCalls : 0;
}
// ── 核心:分析并优化 ────────────────────────────────────
/**
* 分析当前指标,当失败率超过阈值时触发优化建议
* @param {string} skillName - 可选,只分析特定技能
* @returns {object} 分析结果
*/
analyzeAndOptimize(skillName) {
const analysis = {
overall: this._analyzeOverall(),
bySkill: skillName ? this._analyzeSkill(skillName) : null,
suggestions: [],
triggered: false,
timestamp: new Date().toISOString(),
};
// 检查总体失败率
if (analysis.overall.failureRate > this.config.failureRateThreshold) {
analysis.triggered = true;
analysis.suggestions.push({
type: 'HIGH_FAILURE_RATE',
severity: 'warning',
message: `总体失败率 ${analysis.overall.failureRatePercent}% 超过阈值 ${this.config.failureRateThreshold * 100}%`,
action: 'review_failed_tasks',
});
}
// 检查特定技能
if (analysis.bySkill && analysis.bySkill.failureRate > this.config.failureRateThreshold) {
analysis.triggered = true;
analysis.suggestions.push({
type: 'SKILL_FAILURE_RATE',
severity: 'critical',
skillName,
message: `技能 "${skillName}" 失败率 ${analysis.bySkill.failureRatePercent}% 超过阈值`,
action: 'review_skill_config',
});
}
// 检查连续失败
Eif (skillName) {
const consecutive = this._consecutiveFailures.get(skillName) || 0;
if (consecutive >= this.config.consecutiveFailureThreshold) {
analysis.triggered = true;
analysis.suggestions.push({
type: 'CONSECUTIVE_FAILURES',
severity: 'critical',
skillName,
message: `技能 "${skillName}" 连续失败 ${consecutive} 次`,
action: 'increase_retry_backoff',
});
}
}
// 缓存优化建议
if (analysis.suggestions.length > 0) {
this.metrics.suggestions.push(analysis);
if (this.metrics.suggestions.length > this.config.maxSuggestionHistory) {
this.metrics.suggestions = this.metrics.suggestions.slice(-this.config.maxSuggestionHistory);
}
this.save();
}
// 触发回调
if (analysis.triggered && this.config.onOptimizationTriggered) {
this.config.onOptimizationTriggered(analysis);
}
return analysis;
}
_analyzeOverall() {
const total = this.metrics.totalTasks;
const failed = this.metrics.failedTasks;
const failureRate = total > 0 ? failed / total : 0;
return {
totalTasks: total,
completedTasks: this.metrics.completedTasks,
failedTasks: failed,
failureRate: Math.round(failureRate * 1000) / 1000,
failureRatePercent: (failureRate * 100).toFixed(1) + '%',
avgLatency: Math.round(this._getOverallAvgLatency()),
};
}
_analyzeSkill(skillName) {
const call = this.metrics.skillCalls[skillName];
Iif (!call || call.total === 0) {
return { total: 0, failureRate: 0, failureRatePercent: '0.0%' };
}
return {
total: call.total,
success: call.success,
failed: call.failed,
failureRate: Math.round((call.failed / call.total) * 1000) / 1000,
failureRatePercent: ((call.failed / call.total) * 100).toFixed(1) + '%',
avgLatency: Math.round(call.totalLatency / call.total),
};
}
// ── 核心:自适应重试策略 ────────────────────────────────
/**
* 根据历史失败模式自动调整重试策略
* @param {string} skillName - 技能名称
* @returns {object} 调整后的重试配置
*/
getAdaptiveRetryConfig(skillName) {
const call = this.metrics.skillCalls[skillName];
const consecutive = this._consecutiveFailures.get(skillName) || 0;
// 默认配置
let config = { ...this.config.retry };
if (call && call.total > 0) {
const failureRate = call.failed / call.total;
if (failureRate > 0.5) {
// 高失败率:增加最大重试次数,延长退避
config.maxRetries = Math.min(config.maxRetries + 2, 8);
config.backoffMultiplier = Math.min(config.backoffMultiplier + 0.5, 4);
} else Iif (failureRate > 0.3) {
// 中等失败率:适度增加
config.maxRetries = Math.min(config.maxRetries + 1, 6);
} else Iif (failureRate < 0.05 && call.total > 10) {
// 低失败率且有足够样本:减少重试以节省资源
config.maxRetries = Math.max(config.maxRetries - 1, 2);
}
}
// 连续失败:激进退避
if (consecutive >= 3) {
config.backoffMultiplier = Math.min(config.backoffMultiplier + consecutive * 0.5, 4);
config.baseDelay = Math.min(config.baseDelay * consecutive, 10000);
}
// 决定是否应该重试
const shouldRetry = consecutive < config.maxRetries;
return { ...config, shouldRetry, consecutiveFailures: consecutive };
}
// ── 内部:计算退避延迟 ──────────────────────────────────
_calculateBackoff(skillName, retryCount) {
const config = this.getAdaptiveRetryConfig(skillName);
const delay = config.baseDelay * Math.pow(config.backoffMultiplier, retryCount);
// 添加随机抖动避免 thundering herd
const jitter = Math.random() * delay * 0.1;
return Math.min(delay + jitter, config.maxDelay);
}
// ── 查询接口 ────────────────────────────────────────────
/**
* 获取指标报告
* @returns {object} 完整指标报告
*/
getReport() {
return {
summary: this._analyzeOverall(),
skills: Object.keys(this.metrics.skillCalls).reduce((acc, name) => {
acc[name] = this._analyzeSkill(name);
return acc;
}, {}),
recentSuggestions: this.metrics.suggestions.slice(-5),
timestamp: new Date().toISOString(),
};
}
/**
* 获取趋势数据
* @param {number} limit - 返回最近 N 个点
* @returns {Array} 时间序列数据
*/
getTrend(limit = 20) {
return this.metrics.timeSeries.slice(-limit);
}
/**
* 重置指标(用于测试或新版本部署)
*/
reset() {
this.metrics = {
totalTasks: 0, completedTasks: 0, failedTasks: 0,
pausedTasks: 0, cancelledTasks: 0,
skillCalls: {}, timeSeries: [], suggestions: [],
};
this._consecutiveFailures.clear();
this._retryStrategies.clear();
this.save();
}
}
module.exports = { MetricsMiddleware, DEFAULT_CONFIG };
|