Lines 100.00% 12 / 12
Methods 100.00% 1 / 1
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 collectMetrics 100.00% 12 / 12 100.00% 1 / 1 3
14final readonly class ConnectionMetricSource implements RuntimeMetricSource
15{
16    /** @return list<MetricFamily> */
17    public function collectMetrics(RuntimeSnapshot $snapshot): array
18    {
19        $connection = $snapshot->connection;
20        $phases = new MetricFamily('stewart_ha_connection_phase', 'Home Assistant connection phase; 1 for the current one.', MetricType::Gauge);
21
22        foreach (ConnectionPhase::cases() as $phase) {
23            $phases->recordSample(MetricLabels::fromLabel('phase', $phase->value), $phase === $connection->phase);
24        }
25
26        $lastOutage = new MetricFamily('stewart_ha_last_outage_seconds', 'Length of the last Home Assistant outage.', MetricType::Gauge);
27
28        if ($connection->lastOutage !== null) {
29            $lastOutage->recordSample(MetricLabels::none(), $connection->lastOutage->toSeconds());
30        }
31
32        return [
33            $phases,
34            MetricFamily::createWithSample('stewart_ha_reconnects_total', 'Home Assistant reconnects since the daemon started.', MetricType::Counter, MetricLabels::none(), $connection->reconnects),
35            $lastOutage,
36        ];
37    }
38}