Lines
100.00%
55 / 55
Functions and Methods
100.00%
1 / 1
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| AppMetricSource | 100.00% | 55 / 55 | 100.00% | 1 / 1 | 11 | 100.00% | 1 / 1 | |||
| collectMetrics | 100.00% | 55 / 55 | 100.00% | 1 / 1 | 11 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Stewart\Runtime\Metrics\Source; | |
| 6 | ||
| 7 | use Stewart\Runtime\Control\Protocol\Status\RuntimeSnapshot; | |
| 8 | use Stewart\Runtime\Lifecycle\AppState; | |
| 9 | use Stewart\Runtime\Metrics\Exposition\Histogram; | |
| 10 | use Stewart\Runtime\Metrics\Exposition\MetricFamily; | |
| 11 | use Stewart\Runtime\Metrics\Exposition\MetricLabels; | |
| 12 | use Stewart\Runtime\Metrics\Exposition\MetricType; | |
| 13 | use Stewart\Runtime\Metrics\Exposition\PrometheusNumber; | |
| 14 | use Stewart\Runtime\Metrics\RuntimeMetricSource; | |
| 15 | use Stewart\Runtime\Model\ResourceScope; | |
| 16 | ||
| 17 | final readonly class AppMetricSource implements RuntimeMetricSource | |
| 18 | { | |
| 19 | /** @return list<MetricFamily> */ | |
| 20 | public function collectMetrics(RuntimeSnapshot $snapshot): array | |
| 21 | { | |
| 22 | $appLabels = ['app', 'worker']; | |
| 23 | $outcomeLabels = [...$appLabels, 'outcome']; | |
| 24 | $info = new MetricFamily('stewart_app_info', 'App class per app; always 1.', MetricType::Gauge, [...$appLabels, 'class']); | |
| 25 | $states = new MetricFamily('stewart_app_state', 'App lifecycle state; 1 for the current one.', MetricType::Gauge, [...$appLabels, 'state']); | |
| 26 | $paused = new MetricFamily('stewart_app_paused', 'Whether the app is paused; 1 or 0.', MetricType::Gauge, $appLabels); | |
| 27 | $subscriptions = new MetricFamily('stewart_app_subscriptions', 'Subscriptions the app holds.', MetricType::Gauge, $appLabels); | |
| 28 | $schedules = new MetricFamily('stewart_app_schedules', 'Schedules the app holds.', MetricType::Gauge, $appLabels); | |
| 29 | $exposedEntities = new MetricFamily('stewart_app_exposed_entities', 'Home Assistant entities the app exposes, unavailable ones included.', MetricType::Gauge, $appLabels); | |
| 30 | $delivered = new MetricFamily('stewart_app_events_delivered_total', 'Events delivered to the app handlers.', MetricType::Counter, $appLabels); | |
| 31 | $handled = new MetricFamily('stewart_app_events_handled_total', 'Events that got through the operators and reached the app\'s own handler.', MetricType::Counter, $appLabels); | |
| 32 | $lastHandled = new MetricFamily('stewart_app_last_handled_timestamp_seconds', 'Unix time the app\'s own handler last ran for an event.', MetricType::Gauge, $appLabels); | |
| 33 | $nextRun = new MetricFamily('stewart_app_next_run_timestamp_seconds', 'Unix time the app\'s next schedule or timer is due.', MetricType::Gauge, $appLabels); | |
| 34 | $subscriptionDropped = new MetricFamily('stewart_app_subscription_dropped_total', 'Events dropped because a subscription queue was full.', MetricType::Counter, $appLabels); | |
| 35 | $scheduleRuns = new MetricFamily('stewart_app_schedule_runs_total', 'Scheduled handler runs.', MetricType::Counter, $appLabels); | |
| 36 | $publishes = new MetricFamily('stewart_app_publishes_total', 'Messages the app published.', MetricType::Counter, $appLabels); | |
| 37 | $failures = new MetricFamily('stewart_app_failures_total', 'Handler failures of the app.', MetricType::Counter, $appLabels); | |
| 38 | $suppressed = new MetricFamily('stewart_app_suppressed_total', 'Work skipped while the app was paused.', MetricType::Counter, $appLabels); | |
| 39 | $lastFailure = new MetricFamily('stewart_app_last_failure_timestamp_seconds', 'Unix time of the last app failure.', MetricType::Gauge, $appLabels); | |
| 40 | $haCalls = new MetricFamily('stewart_app_ha_calls_total', 'Home Assistant calls of the app by outcome.', MetricType::Counter, $outcomeLabels); | |
| 41 | $exposedCommands = new MetricFamily('stewart_app_exposed_commands_total', 'Home Assistant commands on the app\'s exposed entities by outcome.', MetricType::Counter, $outcomeLabels); | |
| 42 | $haCallDuration = new MetricFamily('stewart_app_ha_call_duration_seconds', 'Home Assistant call latency of the app by outcome.', MetricType::Histogram, $outcomeLabels); | |
| 43 | ||
| 44 | foreach ($snapshot->apps as $app) { | |
| 45 | $labels = MetricLabels::fromLabel('app', $app->id)->withLabel('worker', $app->workerId === null ? '' : (string) $app->workerId); | |
| 46 | ||
| 47 | if (ResourceScope::tryFromWireValue($app->id)?->isShared() !== true) { | |
| 48 | $info->recordSample($labels->withLabel('class', $app->class), 1); | |
| 49 | ||
| 50 | foreach (AppState::cases() as $state) { | |
| 51 | $states->recordSample($labels->withLabel('state', $state->value), $state === $app->state); | |
| 52 | } | |
| 53 | ||
| 54 | $paused->recordSample($labels, $app->pause !== null); | |
| 55 | } | |
| 56 | ||
| 57 | $subscriptions->recordSample($labels, $app->subscriptions); | |
| 58 | $schedules->recordSample($labels, $app->schedules); | |
| 59 | $exposedEntities->recordSample($labels, $app->exposedEntities); | |
| 60 | $delivered->recordSample($labels, $app->counters->delivered); | |
| 61 | $handled->recordSample($labels, $app->counters->handled); | |
| 62 | $subscriptionDropped->recordSample($labels, $app->counters->subscriptionDropped); | |
| 63 | $scheduleRuns->recordSample($labels, $app->counters->scheduleRuns); | |
| 64 | $publishes->recordSample($labels, $app->counters->publishes); | |
| 65 | $failures->recordSample($labels, $app->counters->failures); | |
| 66 | $suppressed->recordSample($labels, $app->counters->suppressed); | |
| 67 | ||
| 68 | if ($app->lastHandledAt !== null) { | |
| 69 | $lastHandled->recordSample($labels, PrometheusNumber::convertToEpochSeconds($app->lastHandledAt)); | |
| 70 | } | |
| 71 | ||
| 72 | if ($app->nextRunAt !== null) { | |
| 73 | $nextRun->recordSample($labels, PrometheusNumber::convertToEpochSeconds($app->nextRunAt)); | |
| 74 | } | |
| 75 | ||
| 76 | if ($app->lastFailure !== null) { | |
| 77 | $lastFailure->recordSample($labels, PrometheusNumber::convertToEpochSeconds($app->lastFailure->at)); | |
| 78 | } | |
| 79 | ||
| 80 | foreach ($app->exposedCommands as $commands) { | |
| 81 | $exposedCommands->recordSample($labels->withLabel('outcome', $commands->outcome->value), $commands->count); | |
| 82 | } | |
| 83 | ||
| 84 | foreach ($app->haCalls as $calls) { | |
| 85 | $outcomeLabels = $labels->withLabel('outcome', $calls->outcome->value); | |
| 86 | $haCalls->recordSample($outcomeLabels, $calls->count); | |
| 87 | ||
| 88 | if ($calls->latency !== null) { | |
| 89 | $haCallDuration->recordHistogram($outcomeLabels, Histogram::fromLatencyHistogram($calls->latency)); | |
| 90 | } | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| 94 | return [ | |
| 95 | $info, $states, $paused, $subscriptions, $schedules, $exposedEntities, $delivered, $handled, $lastHandled, $nextRun, $subscriptionDropped, | |
| 96 | $scheduleRuns, $publishes, $failures, $suppressed, $lastFailure, $haCalls, $haCallDuration, $exposedCommands, | |
| 97 | ]; | |
| 98 | } | |
| 99 | } |