:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(ingestion-consumer): export worker health state as state-set gauge (#63018)
José Sequeira2d87a043aa61
diff --git a/nodejs/tests/service-e2e/rust-ingestion-consumer.test.ts b/nodejs/tests/service-e2e/rust-ingestion-consumer.test.tsindex c3b5f7518c39..5b5dba1e05b2 100644--- a/nodejs/tests/service-e2e/rust-ingestion-consumer.test.ts+++ b/nodejs/tests/service-e2e/rust-ingestion-consumer.test.ts@@ -290,7 +290,7 @@ describe('Rust ingestion consumer with Node ingestion API workers', () => {expect(worker1RoutedAfterHealthyBatch).toBeGreaterThan(0)await workers[0].service.stop()- await waitForWorkerHealthState(rustConsumer, rustMetricsPort, workers[0].url, 2)+ await waitForWorkerHealthState(rustConsumer, rustMetricsPort, workers[0].url, 'unhealthy')const failoverEvents = buildSingleBatchEvents(team,'rust node e2e failover event',@@ -333,7 +333,7 @@ describe('Rust ingestion consumer with Node ingestion API workers', () => {})workers[0] = await restartNodeIngestionApiWorker(services, workers[0], 'node-worker-1-recovered')- await waitForWorkerHealthState(rustConsumer, rustMetricsPort, workers[0].url, 0)+ await waitForWorkerHealthState(rustConsumer, rustMetricsPort, workers[0].url, 'healthy')const recoveryEvents = buildSingleBatchEvents(team,@@ -383,7 +383,9 @@ describe('Rust ingestion consumer with Node ingestion API workers', () => {try {await Promise.all(workers.map((worker) => worker.service.stop()))await Promise.all(- workers.map((worker) => waitForWorkerHealthState(rustConsumer, rustMetricsPort, worker.url, 2))+ workers.map((worker) =>+ waitForWorkerHealthState(rustConsumer, rustMetricsPort, worker.url, 'unhealthy')+ ))const events = buildSingleBatchEvents(@@ -1010,16 +1012,16 @@ async function waitForWorkerHealthState(rustConsumer: ServiceProcess,rustMetricsPort: number,worker: string,- state: number+ state: 'healthy' | 'degraded' | 'unhealthy'): Promise<void> {await waitForExpect(async () => {const metrics = await fetchMetrics(rustConsumer, rustMetricsPort)expect(getPrometheusSample(metrics, {name: 'ingestion_consumer_worker_health_state',- labels: { worker },+ labels: { worker, state },})- ).toBe(state)+ ).toBe(1)}, 30_000)}diff --git a/rust/ingestion-consumer/src/worker_registry.rs b/rust/ingestion-consumer/src/worker_registry.rsindex f8f052ca3c81..11a6a06f6326 100644--- a/rust/ingestion-consumer/src/worker_registry.rs+++ b/rust/ingestion-consumer/src/worker_registry.rs@@ -18,6 +18,12 @@ pub enum WorkerState {}impl WorkerState {+ const ALL: [WorkerState; 3] = [+ WorkerState::Healthy,+ WorkerState::Degraded,+ WorkerState::Unhealthy,+ ];+pub fn as_str(self) -> &'static str {match self {WorkerState::Healthy => "healthy",@@ -25,13 +31,20 @@ impl WorkerState {WorkerState::Unhealthy => "unhealthy",}}+}- fn as_gauge_value(self) -> f64 {- match self {- WorkerState::Healthy => 0.0,- WorkerState::Degraded => 1.0,- WorkerState::Unhealthy => 2.0,- }+/// Emit the worker health state as a state-set gauge: one series per state+/// labeled `state`, with the current state at 1 and all others at 0 (same+/// pattern as `kube_pod_status_phase`). Operators can alert on a state by+/// name without decoding magic numbers.+fn set_state_gauge(worker_url: &str, current: WorkerState) {+ for state in WorkerState::ALL {+ gauge!(+ "ingestion_consumer_worker_health_state",+ "worker" => worker_url.to_string(),+ "state" => state.as_str(),+ )+ .set(if state == current { 1.0 } else { 0.0 });}}@@ -106,14 +119,7 @@ impl WorkerHealth {"to" => new_state.as_str(),).increment(1);- // Numeric gauge so operators can alert directly on state without- // reconstructing it from cumulative transition counters.- // 0 = Healthy, 1 = Degraded, 2 = Unhealthy.- gauge!(- "ingestion_consumer_worker_health_state",- "worker" => worker_url.to_string(),- )- .set(new_state.as_gauge_value());+ set_state_gauge(worker_url, new_state);true}@@ -207,11 +213,17 @@ impl WorkerRegistry {.build().expect("failed to create probe HTTP client");- let workers = worker_urls+ let workers: Vec<(String, Mutex<WorkerHealth>)> = worker_urls.iter().map(|url| (url.clone(), Mutex::new(WorkerHealth::new()))).collect();+ // Emit the initial state for every worker so the gauge exists from+ // startup rather than only after the first transition.+ for (url, _) in &workers {+ set_state_gauge(url, WorkerState::Healthy);+ }+Self {workers,config,