Тикер офлайн-детекции (internal/healthcheck) переехал из device-control-service в новый самостоятельный Go-сервис — как и планировалось с самого начала (см. изначальный README-заглушку). Детекция online остаётся в device-control-service: она происходит как побочный эффект уже имеющейся там MQTT-подписки на devices/+/telemetry и devices/+/ack, заводить для этого отдельный сервис с дублирующей MQTT-подпиской избыточно. health-check-service — свой Go-модуль (без зависимости от proto/, сборка из собственного контекста), читает только узкое read+offline-подмножество Device Shadow keyspace в Redis (devices:known, status, last_seen) — полный Shadow API с записью desired/reported state остаётся только в device-control-service. Метрика device_control_health_transitions_total переименована в device_health_transitions_total и теперь публикуется с этим именем из ДВУХ сервисов (device-control-service — direction=online, health-check-service — direction=offline): Prometheus агрегирует одноимённые метрики с разных таргетов прозрачно, поэтому Grafana- дашборд адаптирован простой сменой имени метрики в запросе, без переделки панели. Проверено вживую через docker compose: реальный online→offline переход (публикация тестовой телеметрии + ожидание таймаута) корректно долетает до Redis и RabbitMQ (device.status_changed), Prometheus видит новый scrape-таргет как up.
39 lines
1.7 KiB
Go
39 lines
1.7 KiB
Go
// Package metrics defines device-control-service's Prometheus metrics.
|
|
// Registered automatically (via promauto) into the default registry on
|
|
// import; served alongside the command HTTP API's /metrics route.
|
|
package metrics
|
|
|
|
import (
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
)
|
|
|
|
var (
|
|
CommandsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
|
|
Name: "device_control_commands_total",
|
|
Help: "Total number of TurnOn/TurnOff/SetLevel commands dispatched, by action and outcome.",
|
|
}, []string{"action", "outcome"})
|
|
|
|
CommandDuration = promauto.NewHistogramVec(prometheus.HistogramOpts{
|
|
Name: "device_control_command_duration_seconds",
|
|
Help: "Duration of a command dispatch (Redis desired_state patch + MQTT publish), by action.",
|
|
Buckets: prometheus.DefBuckets,
|
|
}, []string{"action"})
|
|
|
|
MQTTEventsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
|
|
Name: "device_control_mqtt_events_total",
|
|
Help: "Total number of telemetry/ack messages received, by event type and outcome.",
|
|
}, []string{"event_type", "outcome"})
|
|
|
|
// Name matches health-check-service's own metric of the same purpose —
|
|
// this service only ever emits direction="online" (detected via MQTT
|
|
// telemetry/ack), health-check-service only ever emits "offline".
|
|
// Prometheus aggregates same-named metrics across scrape targets, so
|
|
// Grafana's `sum by (direction) (...)` panel works across both without
|
|
// a query change.
|
|
HealthTransitionsTotal = promauto.NewCounterVec(prometheus.CounterOpts{
|
|
Name: "device_health_transitions_total",
|
|
Help: "Total number of device online/offline transitions detected.",
|
|
}, []string{"direction"})
|
|
)
|