rust-tokio-supervisor 0.1.3

A Rust tokio supervisor with declarative task supervision, restart policy, shutdown coordination, and observability.
Documentation
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! Metrics facade and low-cardinality label validation.
//!
//! This module defines the stable metric names used by the supervisor core and
//! validates labels before a recorder receives them.

use crate::control::outcome::StaleReportHandling;
use crate::event::payload::{SupervisorEvent, What};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::sync::Arc;
use std::sync::atomic::{AtomicI64, Ordering};

/// Stable metric names emitted by the supervisor core.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SupervisorMetricName {
    /// Total child restarts.
    RestartTotal,
    /// Current child state gauge.
    ChildState,
    /// Child uptime in seconds.
    ChildUptimeSeconds,
    /// Restart backoff in seconds.
    BackoffSeconds,
    /// Health check latency in seconds.
    HealthcheckLatencySeconds,
    /// Total meltdown events.
    MeltdownTotal,
    /// Shutdown duration in seconds.
    ShutdownDurationSeconds,
    /// Total child shutdown outcomes.
    ShutdownChildOutcomesTotal,
    /// Total shutdown abort outcomes.
    ShutdownAbortTotal,
    /// Total late child shutdown reports.
    ShutdownLateReportsTotal,
    /// Total event lag.
    EventLagTotal,
    /// Current configuration version.
    ConfigVersion,
    /// Runtime control loop exit counter.
    RuntimeControlLoopExitTotal,
    /// Runtime control plane alive gauge.
    RuntimeControlPlaneAlive,
    /// Total child control commands.
    ChildControlCommandTotal,
    /// Remaining restart limit for child runtime state.
    ChildRuntimeRestartLimitRemaining,
    /// Total stale heartbeat observations.
    ChildRuntimeHeartbeatStaleTotal,
    /// Total child control operation transitions.
    ChildRuntimeOperationTransitionsTotal,
    /// Total generation fence lifecycle transitions for manual restart isolation.
    ChildRestartFenceTotal,
    /// Total stale completion reports classified outside authoritative triples.
    ChildAttemptStaleReportTotal,
    /// Observed pending restart gauge mirrored from lifecycle signals.
    ChildRestartPendingTotal,
}

impl SupervisorMetricName {
    /// Returns the wire metric name.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns the stable metric name.
    ///
    /// # Examples
    ///
    /// ```
    /// let name = rust_supervisor::observe::metrics::SupervisorMetricName::RestartTotal;
    /// assert_eq!(name.as_str(), "supervisor_restart_total");
    /// ```
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::RestartTotal => "supervisor_restart_total",
            Self::ChildState => "supervisor_child_state",
            Self::ChildUptimeSeconds => "supervisor_child_uptime_seconds",
            Self::BackoffSeconds => "supervisor_backoff_seconds",
            Self::HealthcheckLatencySeconds => "supervisor_healthcheck_latency_seconds",
            Self::MeltdownTotal => "supervisor_meltdown_total",
            Self::ShutdownDurationSeconds => "supervisor_shutdown_duration_seconds",
            Self::ShutdownChildOutcomesTotal => "supervisor_shutdown_child_outcomes_total",
            Self::ShutdownAbortTotal => "supervisor_shutdown_abort_total",
            Self::ShutdownLateReportsTotal => "supervisor_shutdown_late_reports_total",
            Self::EventLagTotal => "supervisor_event_lag_total",
            Self::ConfigVersion => "supervisor_config_version",
            Self::RuntimeControlLoopExitTotal => "supervisor_runtime_control_loop_exit_total",
            Self::RuntimeControlPlaneAlive => "supervisor_runtime_control_plane_alive",
            Self::ChildControlCommandTotal => "supervisor_child_control_command_total",
            Self::ChildRuntimeRestartLimitRemaining => {
                "supervisor_child_runtime_restart_limit_remaining"
            }
            Self::ChildRuntimeHeartbeatStaleTotal => {
                "supervisor_child_runtime_heartbeat_stale_total"
            }
            Self::ChildRuntimeOperationTransitionsTotal => {
                "supervisor_child_runtime_operation_transitions_total"
            }
            Self::ChildRestartFenceTotal => "supervisor_child_restart_fence_total",
            Self::ChildAttemptStaleReportTotal => "supervisor_child_attempt_stale_report_total",
            Self::ChildRestartPendingTotal => "supervisor_child_restart_pending_total",
        }
    }
}

/// Metric sample produced by the facade.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MetricSample {
    /// Stable metric name.
    pub name: String,
    /// Numeric metric value.
    pub value: f64,
    /// Low-cardinality labels.
    pub labels: BTreeMap<String, String>,
}

impl MetricSample {
    /// Creates a metric sample.
    ///
    /// # Arguments
    ///
    /// - `name`: Stable metric name.
    /// - `value`: Numeric metric value.
    /// - `labels`: Low-cardinality labels.
    ///
    /// # Returns
    ///
    /// Returns a [`MetricSample`].
    pub fn new(name: SupervisorMetricName, value: f64, labels: BTreeMap<String, String>) -> Self {
        Self {
            name: name.as_str().to_owned(),
            value,
            labels,
        }
    }
}

/// Validation error for metric labels.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MetricLabelError {
    /// Label key that failed validation.
    pub key: String,
    /// Human-readable validation reason.
    pub reason: String,
}

/// Facade that maps lifecycle events to metrics.
pub struct MetricsFacade {
    /// Maximum accepted label value length.
    pub max_label_value_len: usize,
    /// Best-effort mirror of pending restart depth for gauge export.
    restart_pending_total: Arc<AtomicI64>,
}

impl Clone for MetricsFacade {
    /// Clones this metrics facade while sharing the pending restart gauge counter.
    fn clone(&self) -> Self {
        Self {
            max_label_value_len: self.max_label_value_len,
            restart_pending_total: Arc::clone(&self.restart_pending_total),
        }
    }
}

impl std::fmt::Debug for MetricsFacade {
    /// Renders diagnostic information for this metrics facade.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("MetricsFacade")
            .field("max_label_value_len", &self.max_label_value_len)
            .field(
                "restart_pending_total",
                &self.restart_pending_total.load(Ordering::SeqCst),
            )
            .finish()
    }
}

impl MetricsFacade {
    /// Creates a metrics facade.
    ///
    /// # Arguments
    ///
    /// This function has no arguments.
    ///
    /// # Returns
    ///
    /// Returns a [`MetricsFacade`] with conservative label validation.
    ///
    /// # Examples
    ///
    /// ```
    /// let facade = rust_supervisor::observe::metrics::MetricsFacade::new();
    /// assert!(facade.validate_label("state", "running").is_ok());
    /// ```
    pub fn new() -> Self {
        Self {
            max_label_value_len: 96,
            restart_pending_total: Arc::new(AtomicI64::new(0)),
        }
    }

    /// Validates one low-cardinality metric label.
    ///
    /// # Arguments
    ///
    /// - `key`: Metric label key.
    /// - `value`: Metric label value.
    ///
    /// # Returns
    ///
    /// Returns `Ok(())` when the label is accepted.
    pub fn validate_label(&self, key: &str, value: &str) -> Result<(), MetricLabelError> {
        if !allowed_label_key(key) {
            return Err(MetricLabelError {
                key: key.to_owned(),
                reason: "label key is not allowed".to_owned(),
            });
        }
        if value.len() > self.max_label_value_len {
            return Err(MetricLabelError {
                key: key.to_owned(),
                reason: "label value is too long".to_owned(),
            });
        }
        if value.contains('\n') {
            return Err(MetricLabelError {
                key: key.to_owned(),
                reason: "label value contains a newline".to_owned(),
            });
        }
        Ok(())
    }

    /// Applies a bounded adjustment to the mirrored pending restart gauge counter.
    ///
    /// # Arguments
    ///
    /// - `delta`: Signed delta applied to the gauge counter (never drops below zero).
    ///
    /// # Returns
    ///
    /// Returns the gauge counter after applying `delta`.
    fn adjust_restart_pending_total(&self, delta: i64) -> i64 {
        let mut next = self
            .restart_pending_total
            .load(Ordering::SeqCst)
            .saturating_add(delta);
        if next < 0 {
            next = 0;
        }
        self.restart_pending_total.store(next, Ordering::SeqCst);
        next
    }

    /// Maps a lifecycle event into metric samples.
    ///
    /// # Arguments
    ///
    /// - `event`: Lifecycle event emitted by the runtime.
    ///
    /// # Returns
    ///
    /// Returns zero or more metric samples for the event.
    pub fn samples_for_event(&self, event: &SupervisorEvent) -> Vec<MetricSample> {
        match &event.what {
            What::ChildRestarted { .. } => vec![MetricSample::new(
                SupervisorMetricName::RestartTotal,
                1.0,
                labels_for_event(event),
            )],
            What::BackoffScheduled { delay_ms } => vec![MetricSample::new(
                SupervisorMetricName::BackoffSeconds,
                *delay_ms as f64 / 1000.0,
                labels_for_event(event),
            )],
            What::Meltdown { .. } => vec![MetricSample::new(
                SupervisorMetricName::MeltdownTotal,
                1.0,
                labels_for_event(event),
            )],
            What::ShutdownCompleted {
                phase,
                result,
                duration_ms,
            } => vec![MetricSample::new(
                SupervisorMetricName::ShutdownDurationSeconds,
                *duration_ms as f64 / 1000.0,
                shutdown_completed_labels_for_event(event, phase, result),
            )],
            What::ChildShutdownGraceful { phase, .. } => vec![MetricSample::new(
                SupervisorMetricName::ShutdownChildOutcomesTotal,
                1.0,
                shutdown_child_outcome_labels_for_event(event, "graceful", phase),
            )],
            What::ChildShutdownAborted {
                phase,
                result,
                reason,
                ..
            } => shutdown_aborted_samples(event, phase, result, reason),
            What::ChildShutdownLateReport { phase, .. } => {
                shutdown_late_report_samples(event, phase)
            }
            What::SubscriberLagged { missed } => vec![MetricSample::new(
                SupervisorMetricName::EventLagTotal,
                *missed as f64,
                labels_for_event(event),
            )],
            What::RuntimeControlLoopStarted { phase, .. } => vec![MetricSample::new(
                SupervisorMetricName::RuntimeControlPlaneAlive,
                1.0,
                runtime_labels_for_event(event, "alive", phase),
            )],
            What::RuntimeControlLoopCompleted { phase, .. } => {
                runtime_terminal_samples(event, "completed", phase)
            }
            What::RuntimeControlLoopFailed { phase, .. } => {
                runtime_terminal_samples(event, "failed", phase)
            }
            What::ChildControlCommandCompleted {
                command, result, ..
            } => vec![MetricSample::new(
                SupervisorMetricName::ChildControlCommandTotal,
                1.0,
                child_control_command_labels(command, result),
            )],
            What::ChildControlOperationChanged { from, to, .. } => vec![MetricSample::new(
                SupervisorMetricName::ChildRuntimeOperationTransitionsTotal,
                1.0,
                operation_transition_labels(format!("{from:?}"), format!("{to:?}")),
            )],
            What::ChildRuntimeRestartLimitUpdated {
                child_id,
                restart_limit,
            } => vec![MetricSample::new(
                SupervisorMetricName::ChildRuntimeRestartLimitRemaining,
                restart_limit.remaining as f64,
                restart_limit_labels(child_id),
            )],
            What::ChildRestartFenceEntered { .. } => {
                let gauge = self.adjust_restart_pending_total(1);
                vec![
                    MetricSample::new(
                        SupervisorMetricName::ChildRestartFenceTotal,
                        1.0,
                        child_restart_fence_labels(event, "entered"),
                    ),
                    MetricSample::new(
                        SupervisorMetricName::ChildRestartPendingTotal,
                        gauge as f64,
                        restart_pending_gauge_labels(),
                    ),
                ]
            }
            What::ChildRestartFenceAbortRequested { .. } => vec![MetricSample::new(
                SupervisorMetricName::ChildRestartFenceTotal,
                1.0,
                child_restart_fence_labels(event, "abort_requested"),
            )],
            What::ChildRestartFenceReleased { .. } => vec![MetricSample::new(
                SupervisorMetricName::ChildRestartFenceTotal,
                1.0,
                child_restart_fence_labels(event, "released"),
            )],
            What::ChildRestartFencePendingDrained { .. } => {
                let gauge = self.adjust_restart_pending_total(-1);
                vec![MetricSample::new(
                    SupervisorMetricName::ChildRestartPendingTotal,
                    gauge as f64,
                    restart_pending_gauge_labels(),
                )]
            }
            What::ChildRestartConflict { decision, .. } => {
                let fence_bucket = if decision == "already_pending" {
                    "already_pending"
                } else {
                    "rejected"
                };
                vec![MetricSample::new(
                    SupervisorMetricName::ChildRestartFenceTotal,
                    1.0,
                    child_restart_fence_labels(event, fence_bucket),
                )]
            }
            What::ChildAttemptStaleReport { handled_as, .. } => vec![MetricSample::new(
                SupervisorMetricName::ChildAttemptStaleReportTotal,
                1.0,
                stale_report_metric_labels(*handled_as),
            )],
            What::ChildHeartbeatStale { .. } => vec![MetricSample::new(
                SupervisorMetricName::ChildRuntimeHeartbeatStaleTotal,
                1.0,
                BTreeMap::new(),
            )],
            _ => Vec::new(),
        }
    }
}

impl Default for MetricsFacade {
    /// Creates the default metrics facade.
    fn default() -> Self {
        Self::new()
    }
}

/// Checks whether a label key is part of the public low-cardinality set.
///
/// # Arguments
///
/// - `key`: Label key to validate.
///
/// # Returns
///
/// Returns `true` when the key is allowed.
fn allowed_label_key(key: &str) -> bool {
    matches!(
        key,
        "supervisor_path"
            | "child_id"
            | "state"
            | "phase"
            | "status"
            | "result"
            | "reason"
            | "decision"
            | "failure_category"
            | "command"
            | "from"
            | "to"
            | "handled_as"
    )
}

/// Builds an empty label map for pending restart gauges to avoid label cardinality explosions.
///
/// # Arguments
///
/// This function has no arguments.
///
/// # Returns
///
/// Returns an empty [`BTreeMap`] suitable for gauge exports.
fn restart_pending_gauge_labels() -> BTreeMap<String, String> {
    BTreeMap::new()
}

/// Builds metric labels for stale completion counters without embedding child identifiers.
///
/// # Arguments
///
/// - `handled_as`: Low-cardinality handling bucket copied into the label map.
///
/// # Returns
///
/// Returns a label map safe for `supervisor_child_attempt_stale_report_total`.
fn stale_report_metric_labels(handled_as: StaleReportHandling) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert("handled_as".to_owned(), format!("{handled_as:?}"));
    labels
}

/// Extends event labels with the low-cardinality classification for generation-fence metrics.
///
/// # Arguments
///
/// - `event`: Supervisor event whose identifiers should seed the label map.
/// - `result`: Stable fence outcome fragment such as `entered`, `abort_requested`, or `released`.
///
/// # Returns
///
/// Returns merged labels ready for [`SupervisorMetricName::ChildRestartFenceTotal`].
fn child_restart_fence_labels(event: &SupervisorEvent, result: &str) -> BTreeMap<String, String> {
    let mut labels = labels_for_event(event);
    labels.insert("result".to_owned(), result.to_owned());
    labels
}

/// Builds labels for child control command counters.
///
/// # Arguments
///
/// - `command`: Stable command name.
/// - `result`: Stable result classification.
///
/// # Returns
///
/// Returns labels for a child control command counter.
fn child_control_command_labels(
    command: impl Into<String>,
    result: impl Into<String>,
) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert("command".to_owned(), command.into());
    labels.insert("result".to_owned(), result.into());
    labels
}

/// Builds labels for operation transition counters.
///
/// # Arguments
///
/// - `from`: Previous operation.
/// - `to`: New operation.
///
/// # Returns
///
/// Returns labels for an operation transition counter.
fn operation_transition_labels(
    from: impl Into<String>,
    to: impl Into<String>,
) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert("from".to_owned(), from.into());
    labels.insert("to".to_owned(), to.into());
    labels
}

/// Builds labels for restart limit gauges.
///
/// # Arguments
///
/// - `child_id`: Child whose restart limit was refreshed.
///
/// # Returns
///
/// Returns labels for the restart limit gauge.
fn restart_limit_labels(child_id: &crate::id::types::ChildId) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert("child_id".to_owned(), child_id.to_string());
    labels
}

/// Builds labels that are safe to attach to event-derived metrics.
///
/// # Arguments
///
/// - `event`: Lifecycle event used as the label source.
///
/// # Returns
///
/// Returns a map of low-cardinality labels.
fn labels_for_event(event: &SupervisorEvent) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert(
        "supervisor_path".to_owned(),
        event.r#where.supervisor_path.to_string(),
    );
    if let Some(child_id) = &event.r#where.child_id {
        labels.insert("child_id".to_owned(), child_id.to_string());
    }
    if let Some(policy) = &event.policy {
        labels.insert("decision".to_owned(), policy.decision.clone());
    }
    labels.insert(
        "correlation_id".to_owned(),
        event.correlation_id.value.to_string(),
    );
    labels
}

/// Builds metric samples for an aborted shutdown child.
///
/// # Arguments
///
/// - `event`: Lifecycle event used as the label source.
/// - `phase`: Shutdown phase label.
/// - `result`: Child shutdown result label.
/// - `reason`: Abort reason supplied by the event.
///
/// # Returns
///
/// Returns child outcome and abort counter samples.
fn shutdown_aborted_samples(
    event: &SupervisorEvent,
    phase: &str,
    result: &str,
    reason: &str,
) -> Vec<MetricSample> {
    vec![
        MetricSample::new(
            SupervisorMetricName::ShutdownChildOutcomesTotal,
            1.0,
            shutdown_child_outcome_labels_for_event(event, result, phase),
        ),
        MetricSample::new(
            SupervisorMetricName::ShutdownAbortTotal,
            1.0,
            shutdown_abort_labels_for_event(event, phase, reason),
        ),
    ]
}

/// Builds metric samples for a late shutdown child report.
///
/// # Arguments
///
/// - `event`: Lifecycle event used as the label source.
/// - `phase`: Shutdown phase label.
///
/// # Returns
///
/// Returns child outcome and late-report counter samples.
fn shutdown_late_report_samples(event: &SupervisorEvent, phase: &str) -> Vec<MetricSample> {
    vec![
        MetricSample::new(
            SupervisorMetricName::ShutdownChildOutcomesTotal,
            1.0,
            shutdown_child_outcome_labels_for_event(event, "late_report", phase),
        ),
        MetricSample::new(
            SupervisorMetricName::ShutdownLateReportsTotal,
            1.0,
            shutdown_late_report_labels_for_event(event, phase),
        ),
    ]
}

/// Builds metric samples for a terminal runtime control loop event.
///
/// # Arguments
///
/// - `event`: Lifecycle event used as the label source.
/// - `state`: Terminal runtime state.
/// - `phase`: Runtime phase label.
///
/// # Returns
///
/// Returns exit counter and alive gauge samples.
fn runtime_terminal_samples(
    event: &SupervisorEvent,
    state: &str,
    phase: &str,
) -> Vec<MetricSample> {
    vec![
        MetricSample::new(
            SupervisorMetricName::RuntimeControlLoopExitTotal,
            1.0,
            runtime_labels_for_event(event, state, phase),
        ),
        MetricSample::new(
            SupervisorMetricName::RuntimeControlPlaneAlive,
            0.0,
            runtime_labels_for_event(event, state, phase),
        ),
    ]
}

/// Builds labels for runtime control plane metrics.
fn runtime_labels_for_event(
    event: &SupervisorEvent,
    state: &str,
    phase: &str,
) -> BTreeMap<String, String> {
    let mut labels = labels_for_event(event);
    labels.insert("state".to_owned(), state.to_owned());
    labels.insert("phase".to_owned(), phase.to_owned());
    labels
}

/// Builds low-cardinality labels for shutdown completion duration.
fn shutdown_completed_labels_for_event(
    event: &SupervisorEvent,
    phase: &str,
    result: &str,
) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert(
        "supervisor_path".to_owned(),
        event.r#where.supervisor_path.to_string(),
    );
    labels.insert("phase".to_owned(), phase.to_owned());
    labels.insert("result".to_owned(), result.to_owned());
    labels
}

/// Builds labels for child shutdown outcome counters.
fn shutdown_child_outcome_labels_for_event(
    event: &SupervisorEvent,
    status: &str,
    phase: &str,
) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert(
        "supervisor_path".to_owned(),
        event.r#where.supervisor_path.to_string(),
    );
    labels.insert("status".to_owned(), status.to_owned());
    labels.insert("phase".to_owned(), phase.to_owned());
    labels
}

/// Builds labels for shutdown abort counters.
fn shutdown_abort_labels_for_event(
    event: &SupervisorEvent,
    phase: &str,
    reason: &str,
) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert(
        "supervisor_path".to_owned(),
        event.r#where.supervisor_path.to_string(),
    );
    labels.insert("phase".to_owned(), phase.to_owned());
    labels.insert(
        "reason".to_owned(),
        shutdown_abort_reason(reason).to_owned(),
    );
    labels
}

/// Builds labels for late shutdown report counters.
fn shutdown_late_report_labels_for_event(
    event: &SupervisorEvent,
    phase: &str,
) -> BTreeMap<String, String> {
    let mut labels = BTreeMap::new();
    labels.insert(
        "supervisor_path".to_owned(),
        event.r#where.supervisor_path.to_string(),
    );
    labels.insert("phase".to_owned(), phase.to_owned());
    labels
}

/// Converts an abort reason into a bounded metric label value.
fn shutdown_abort_reason(reason: &str) -> &'static str {
    let reason = reason.to_ascii_lowercase();
    if reason.contains("timeout") {
        "timeout"
    } else if reason.contains("failed") {
        "abort_failed"
    } else if reason.contains("operator") {
        "operator"
    } else if reason.is_empty() {
        "unspecified"
    } else {
        "runtime"
    }
}