Lines 97.77% 44 / 45
Functions and Methods 0.00% 0 / 1
Classes and Traits 0.00% 0 / 1
Name Lines Functions and Methods CRAP Classes and Traits
WorkerMetricSource 97.77% 44 / 45 0.00% 0 / 1 8 0.00% 0 / 1
 collectMetrics 97.77% 44 / 45 0.00% 0 / 1 8
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Runtime\Metrics\Source;
6
7use Stewart\Runtime\Control\Protocol\Status\RuntimeSnapshot;
8use Stewart\Runtime\Lifecycle\WorkerPhase;
9use Stewart\Runtime\Metrics\Exposition\MetricFamily;
10use Stewart\Runtime\Metrics\Exposition\MetricLabels;
11use Stewart\Runtime\Metrics\Exposition\MetricType;
12use Stewart\Runtime\Metrics\Exposition\PrometheusNumber;
13use Stewart\Runtime\Metrics\RuntimeMetricSource;
14
15final readonly class WorkerMetricSource implements RuntimeMetricSource
16{
17    /** @return list<MetricFamily> */
18    public function collectMetrics(RuntimeSnapshot $snapshot): array
19    {
20        $workerLabels = ['worker'];
21        $phases = new MetricFamily('stewart_worker_phase', 'Worker lifecycle phase; 1 for the current one.', MetricType::Gauge, [...$workerLabels, 'phase']);
22        $ready = new MetricFamily('stewart_worker_ready', 'Whether the worker finished booting its apps; 1 or 0.', MetricType::Gauge, $workerLabels);
23        $missedProbes = new MetricFamily('stewart_worker_missed_probes', 'Liveness probes the worker has not answered in a row.', MetricType::Gauge, $workerLabels);
24        $loopLag = new MetricFamily('stewart_worker_loop_lag_seconds', 'Event loop lag the worker last reported.', MetricType::Gauge, $workerLabels);
25        $memory = new MetricFamily('stewart_worker_memory_bytes', 'Resident memory (RSS) the worker process last reported.', MetricType::Gauge, $workerLabels);
26        $lastPong = new MetricFamily('stewart_worker_last_pong_timestamp_seconds', 'Unix time of the last liveness answer.', MetricType::Gauge, $workerLabels);
27        $restartsInWindow = new MetricFamily('stewart_worker_restarts_in_window', 'Restarts inside the current restart budget window.', MetricType::Gauge, $workerLabels);
28        $restarts = new MetricFamily('stewart_worker_restarts_total', 'Restarts scheduled since the daemon started.', MetricType::Counter, $workerLabels);
29        $quarantines = new MetricFamily('stewart_worker_quarantines_total', 'Times the worker was quarantined after using up its restart budget.', MetricType::Counter, $workerLabels);
30        $restartDue = new MetricFamily('stewart_worker_restart_due_timestamp_seconds', 'Unix time of the next scheduled restart.', MetricType::Gauge, $workerLabels);
31        $inFlight = new MetricFamily('stewart_worker_ha_calls_in_flight', 'Home Assistant calls of this worker awaiting an answer.', MetricType::Gauge, $workerLabels);
32        $queued = new MetricFamily('stewart_worker_outbox_queued', 'Messages waiting in the outbox to the worker.', MetricType::Gauge, $workerLabels);
33        $largestBatch = new MetricFamily('stewart_worker_outbox_largest_state_batch', 'Largest state change batch sent to the worker.', MetricType::Gauge, $workerLabels);
34        $dropped = new MetricFamily('stewart_worker_outbox_dropped_total', 'Messages dropped because the worker outbox was full.', MetricType::Counter, $workerLabels);
35        $coalesced = new MetricFamily('stewart_worker_outbox_coalesced_state_changes_total', 'State changes merged into a newer one before delivery.', MetricType::Counter, $workerLabels);
36        $batchesSent = new MetricFamily('stewart_worker_outbox_state_batches_sent_total', 'State change batches sent to the worker.', MetricType::Counter, $workerLabels);
37
38        foreach ($snapshot->workers as $worker) {
39            $labels = MetricLabels::fromLabel('worker', (string) $worker->workerId);
40
41            foreach (WorkerPhase::cases() as $phase) {
42                $phases->recordSample($labels->withLabel('phase', $phase->value), $phase === $worker->phase);
43            }
44
45            $ready->recordSample($labels, $worker->ready);
46            $missedProbes->recordSample($labels, $worker->missedProbes);
47            $restartsInWindow->recordSample($labels, $worker->restartsInWindow);
48            $restarts->recordSample($labels, $worker->restarts);
49            $quarantines->recordSample($labels, $worker->quarantines);
50            $inFlight->recordSample($labels, $worker->inFlightHaCalls);
51
52            if ($worker->loopLag !== null) {
53                $loopLag->recordSample($labels, $worker->loopLag->toSeconds());
54            }
55
56            if ($worker->memoryBytes !== null) {
57                $memory->recordSample($labels, $worker->memoryBytes);
58            }
59
60            if ($worker->lastPongAt !== null) {
61                $lastPong->recordSample($labels, PrometheusNumber::convertToEpochSeconds($worker->lastPongAt));
62            }
63
64            if ($worker->restartDueAt !== null) {
65                $restartDue->recordSample($labels, PrometheusNumber::convertToEpochSeconds($worker->restartDueAt));
66            }
67
68            if ($worker->outbox !== null) {
69                $queued->recordSample($labels, $worker->outbox->queued);
70                $largestBatch->recordSample($labels, $worker->outbox->largestStateBatch);
71                $dropped->recordSample($labels, $worker->outbox->dropped);
72                $coalesced->recordSample($labels, $worker->outbox->coalescedStateChanges);
73                $batchesSent->recordSample($labels, $worker->outbox->stateBatchesSent);
74            }
75        }
76
77        return [
78            $phases, $ready, $missedProbes, $loopLag, $memory, $lastPong, $restartsInWindow, $restarts, $quarantines, $restartDue, $inFlight,
79            $queued, $largestBatch, $dropped, $coalesced, $batchesSent,
80        ];
81    }
82}