Files
OrpaonVision/OrpaonVision.SiteApp/Runtime/Metrics/StateMachineMetrics.cs
2026-04-12 22:34:46 +08:00

141 lines
5.5 KiB
C#

#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