#if false using System.Diagnostics.Metrics; using OrpaonVision.Core.Common; using OrpaonVision.Core.Results; namespace OrpaonVision.SiteApp.Runtime.Metrics; /// /// 状态机性能指标收集器。 /// 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); } /// /// 状态机性能指标收集器实现。 /// public sealed class StateMachineMetrics : IStateMachineMetrics { private readonly Counter _stateTransitionCounter; private readonly Histogram _stateTransitionDuration; private readonly Counter _stateGuardCheckCounter; private readonly Counter _manualInterventionCounter; private readonly Histogram _manualInterventionDuration; private readonly ILogger _logger; public StateMachineMetrics(IMeterFactory meterFactory, ILogger logger) { var meter = meterFactory.Create("OrpaonVision.StateMachine"); _stateTransitionCounter = meter.CreateCounter( "orpaonvision_state_machine_transitions_total", description: "状态转换总次数"); _stateTransitionDuration = meter.CreateHistogram( "orpaonvision_state_machine_transition_duration_seconds", description: "状态转换耗时(秒)"); _stateGuardCheckCounter = meter.CreateCounter( "orpaonvision_state_machine_guard_checks_total", description: "状态守卫检查总次数"); _manualInterventionCounter = meter.CreateCounter( "orpaonvision_state_machine_manual_interventions_total", description: "人工干预总次数"); _manualInterventionDuration = meter.CreateHistogram( "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[] { new("from_state", fromState.ToString()), new("to_state", toState.ToString()), new("trigger", trigger.ToString()) }); // 记录状态转换耗时 _stateTransitionDuration.Record(elapsedSeconds, new KeyValuePair[] { 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[] { 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[] { new("intervention_type", interventionType), new("success", success ? "true" : "false") }); // 记录人工干预耗时 _manualInterventionDuration.Record(elapsedSeconds, new KeyValuePair[] { 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