保存
This commit is contained in:
134
OrpaonVision.SiteApp/Runtime/Metrics/RuleEngineMetrics.cs
Normal file
134
OrpaonVision.SiteApp/Runtime/Metrics/RuleEngineMetrics.cs
Normal file
@@ -0,0 +1,134 @@
|
||||
#if false
|
||||
using System.Diagnostics.Metrics;
|
||||
using OrpaonVision.Core.Results;
|
||||
using OrpaonVision.Core.RuleEngine;
|
||||
|
||||
namespace OrpaonVision.SiteApp.Runtime.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// 规则引擎性能指标收集器。
|
||||
/// </summary>
|
||||
public interface IRuleEngineMetrics
|
||||
{
|
||||
void RecordRuleEvaluation(string ruleType, TimeSpan elapsed, RuleResult result);
|
||||
void RecordRuleLoad(int ruleCount, TimeSpan elapsed);
|
||||
void RecordRuleValidation(bool isValid, string ruleType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 规则引擎性能指标收集器实现。
|
||||
/// </summary>
|
||||
public sealed class RuleEngineMetrics : IRuleEngineMetrics
|
||||
{
|
||||
private readonly Counter<int> _ruleEvaluationCounter;
|
||||
private readonly Histogram<double> _ruleEvaluationDuration;
|
||||
private readonly Counter<int> _ruleLoadCounter;
|
||||
private readonly Histogram<double> _ruleLoadDuration;
|
||||
private readonly Counter<int> _ruleValidationCounter;
|
||||
private readonly ILogger<RuleEngineMetrics> _logger;
|
||||
|
||||
public RuleEngineMetrics(IMeterFactory meterFactory, ILogger<RuleEngineMetrics> logger)
|
||||
{
|
||||
var meter = meterFactory.Create("OrpaonVision.RuleEngine");
|
||||
|
||||
_ruleEvaluationCounter = meter.CreateCounter<int>(
|
||||
"orpaonvision_rule_engine_evaluations_total",
|
||||
description: "规则评估总次数");
|
||||
|
||||
_ruleEvaluationDuration = meter.CreateHistogram<double>(
|
||||
"orpaonvision_rule_engine_evaluation_duration_seconds",
|
||||
description: "规则评估耗时(秒)");
|
||||
|
||||
_ruleLoadCounter = meter.CreateCounter<int>(
|
||||
"orpaonvision_rule_engine_loads_total",
|
||||
description: "规则加载总次数");
|
||||
|
||||
_ruleLoadDuration = meter.CreateHistogram<double>(
|
||||
"orpaonvision_rule_engine_load_duration_seconds",
|
||||
description: "规则加载耗时(秒)");
|
||||
|
||||
_ruleValidationCounter = meter.CreateCounter<int>(
|
||||
"orpaonvision_rule_engine_validations_total",
|
||||
description: "规则验证总次数");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void RecordRuleEvaluation(string ruleType, TimeSpan elapsed, RuleResult result)
|
||||
{
|
||||
try
|
||||
{
|
||||
var elapsedSeconds = elapsed.TotalSeconds;
|
||||
|
||||
// 记录评估次数
|
||||
_ruleEvaluationCounter.Add(1, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("rule_type", ruleType),
|
||||
new("result", result.ToString()),
|
||||
new("success", result == RuleResult.Pass ? "true" : "false")
|
||||
});
|
||||
|
||||
// 记录评估耗时
|
||||
_ruleEvaluationDuration.Record(elapsedSeconds, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("rule_type", ruleType),
|
||||
new("result", result.ToString())
|
||||
});
|
||||
|
||||
_logger.LogDebug("记录规则评估指标:类型={RuleType},结果={Result},耗时={ElapsedMs}ms",
|
||||
ruleType, result, elapsed.TotalMilliseconds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "记录规则评估指标失败:类型={RuleType}", ruleType);
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordRuleLoad(int ruleCount, TimeSpan elapsed)
|
||||
{
|
||||
try
|
||||
{
|
||||
var elapsedSeconds = elapsed.TotalSeconds;
|
||||
|
||||
// 记录加载次数
|
||||
_ruleLoadCounter.Add(1, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("rule_count", ruleCount)
|
||||
});
|
||||
|
||||
// 记录加载耗时
|
||||
_ruleLoadDuration.Record(elapsedSeconds, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("rule_count", ruleCount)
|
||||
});
|
||||
|
||||
_logger.LogDebug("记录规则加载指标:数量={RuleCount},耗时={ElapsedMs}ms",
|
||||
ruleCount, elapsed.TotalMilliseconds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "记录规则加载指标失败:数量={RuleCount}", ruleCount);
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordRuleValidation(bool isValid, string ruleType)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 记录验证次数
|
||||
_ruleValidationCounter.Add(1, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("rule_type", ruleType),
|
||||
new("valid", isValid ? "true" : "false")
|
||||
});
|
||||
|
||||
_logger.LogDebug("记录规则验证指标:类型={RuleType},有效={IsValid}", ruleType, isValid);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "记录规则验证指标失败:类型={RuleType}", ruleType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
140
OrpaonVision.SiteApp/Runtime/Metrics/StateMachineMetrics.cs
Normal file
140
OrpaonVision.SiteApp/Runtime/Metrics/StateMachineMetrics.cs
Normal file
@@ -0,0 +1,140 @@
|
||||
#if false
|
||||
using System.Diagnostics.Metrics;
|
||||
using OrpaonVision.Core.Common;
|
||||
using OrpaonVision.Core.Results;
|
||||
|
||||
namespace OrpaonVision.SiteApp.Runtime.Metrics;
|
||||
|
||||
/// <summary>
|
||||
/// 状态机性能指标收集器。
|
||||
/// </summary>
|
||||
public interface IStateMachineMetrics
|
||||
{
|
||||
void RecordStateTransition(RuntimeState fromState, RuntimeState toState, StateTrigger trigger, TimeSpan elapsed);
|
||||
void RecordStateGuardCheck(StateTrigger trigger, RuntimeState currentState, bool allowed);
|
||||
void RecordManualIntervention(string interventionType, TimeSpan elapsed, bool success);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 状态机性能指标收集器实现。
|
||||
/// </summary>
|
||||
public sealed class StateMachineMetrics : IStateMachineMetrics
|
||||
{
|
||||
private readonly Counter<int> _stateTransitionCounter;
|
||||
private readonly Histogram<double> _stateTransitionDuration;
|
||||
private readonly Counter<int> _stateGuardCheckCounter;
|
||||
private readonly Counter<int> _manualInterventionCounter;
|
||||
private readonly Histogram<double> _manualInterventionDuration;
|
||||
private readonly ILogger<StateMachineMetrics> _logger;
|
||||
|
||||
public StateMachineMetrics(IMeterFactory meterFactory, ILogger<StateMachineMetrics> logger)
|
||||
{
|
||||
var meter = meterFactory.Create("OrpaonVision.StateMachine");
|
||||
|
||||
_stateTransitionCounter = meter.CreateCounter<int>(
|
||||
"orpaonvision_state_machine_transitions_total",
|
||||
description: "状态转换总次数");
|
||||
|
||||
_stateTransitionDuration = meter.CreateHistogram<double>(
|
||||
"orpaonvision_state_machine_transition_duration_seconds",
|
||||
description: "状态转换耗时(秒)");
|
||||
|
||||
_stateGuardCheckCounter = meter.CreateCounter<int>(
|
||||
"orpaonvision_state_machine_guard_checks_total",
|
||||
description: "状态守卫检查总次数");
|
||||
|
||||
_manualInterventionCounter = meter.CreateCounter<int>(
|
||||
"orpaonvision_state_machine_manual_interventions_total",
|
||||
description: "人工干预总次数");
|
||||
|
||||
_manualInterventionDuration = meter.CreateHistogram<double>(
|
||||
"orpaonvision_state_machine_manual_intervention_duration_seconds",
|
||||
description: "人工干预耗时(秒)");
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public void RecordStateTransition(RuntimeState fromState, RuntimeState toState, StateTrigger trigger, TimeSpan elapsed)
|
||||
{
|
||||
try
|
||||
{
|
||||
var elapsedSeconds = elapsed.TotalSeconds;
|
||||
|
||||
// 记录状态转换次数
|
||||
_stateTransitionCounter.Add(1, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("from_state", fromState.ToString()),
|
||||
new("to_state", toState.ToString()),
|
||||
new("trigger", trigger.ToString())
|
||||
});
|
||||
|
||||
// 记录状态转换耗时
|
||||
_stateTransitionDuration.Record(elapsedSeconds, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("from_state", fromState.ToString()),
|
||||
new("to_state", toState.ToString()),
|
||||
new("trigger", trigger.ToString())
|
||||
});
|
||||
|
||||
_logger.LogDebug("记录状态转换指标:{FromState} -> {ToState},触发器={Trigger},耗时={ElapsedMs}ms",
|
||||
fromState, toState, trigger, elapsed.TotalMilliseconds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "记录状态转换指标失败:{FromState} -> {ToState}", fromState, toState);
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordStateGuardCheck(StateTrigger trigger, RuntimeState currentState, bool allowed)
|
||||
{
|
||||
try
|
||||
{
|
||||
// 记录守卫检查次数
|
||||
_stateGuardCheckCounter.Add(1, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("trigger", trigger.ToString()),
|
||||
new("current_state", currentState.ToString()),
|
||||
new("allowed", allowed ? "true" : "false")
|
||||
});
|
||||
|
||||
_logger.LogDebug("记录状态守卫检查指标:触发器={Trigger},当前状态={CurrentState},允许={Allowed}",
|
||||
trigger, currentState, allowed);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "记录状态守卫检查指标失败:触发器={Trigger},当前状态={CurrentState}",
|
||||
trigger, currentState);
|
||||
}
|
||||
}
|
||||
|
||||
public void RecordManualIntervention(string interventionType, TimeSpan elapsed, bool success)
|
||||
{
|
||||
try
|
||||
{
|
||||
var elapsedSeconds = elapsed.TotalSeconds;
|
||||
|
||||
// 记录人工干预次数
|
||||
_manualInterventionCounter.Add(1, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("intervention_type", interventionType),
|
||||
new("success", success ? "true" : "false")
|
||||
});
|
||||
|
||||
// 记录人工干预耗时
|
||||
_manualInterventionDuration.Record(elapsedSeconds, new KeyValuePair<string, object>[]
|
||||
{
|
||||
new("intervention_type", interventionType),
|
||||
new("success", success ? "true" : "false")
|
||||
});
|
||||
|
||||
_logger.LogDebug("记录人工干预指标:类型={InterventionType},成功={Success},耗时={ElapsedMs}ms",
|
||||
interventionType, success, elapsed.TotalMilliseconds);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "记录人工干预指标失败:类型={InterventionType}", interventionType);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user