Lines 100.00% 9 / 9
Functions and Methods 100.00% 1 / 1
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
ComponentMetricSource 100.00% 9 / 9 100.00% 1 / 1 5 100.00% 1 / 1
 collectMetrics 100.00% 9 / 9 100.00% 1 / 1 5
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\ComponentState;
9use Stewart\Runtime\Metrics\Exposition\MetricFamily;
10use Stewart\Runtime\Metrics\Exposition\MetricLabels;
11use Stewart\Runtime\Metrics\Exposition\MetricType;
12use Stewart\Runtime\Metrics\RuntimeMetricSource;
13
14final readonly class ComponentMetricSource implements RuntimeMetricSource
15{
16    /** @return list<MetricFamily> */
17    public function collectMetrics(RuntimeSnapshot $snapshot): array
18    {
19        $component = $snapshot->component;
20        $info = new MetricFamily('stewart_component_info', 'Version and protocol of the stewart Home Assistant integration; always 1.', MetricType::Gauge);
21        $states = new MetricFamily('stewart_component_state', 'State of the stewart Home Assistant integration; 1 for the current one.', MetricType::Gauge);
22
23        if ($component?->version !== null && $component->protocol !== null) {
24            $info->recordSample(MetricLabels::fromLabel('version', $component->version)->withLabel('protocol', (string) $component->protocol), 1);
25        }
26
27        if ($component !== null) {
28            foreach (ComponentState::cases() as $state) {
29                $states->recordSample(MetricLabels::fromLabel('state', $state->value), $state === $component->state);
30            }
31        }
32
33        return [$info, $states];
34    }
35}