---
id: I0052
title: "Prometheus WebSocket delivery lag"
url: https://carbium.sh/library/integrate/prometheus-websocket-delivery-lag
collection: Integrations
category: Search and time series
revision: 1
updated: 2026-11-22
author: Carbium
code: MIT (LICENSE in the zip); text: all rights reserved, Carbium Corporation
---

# Prometheus WebSocket delivery lag

A 20-second sampler opens one Carbium WebSocket, subscribes to slots and roots, and prints a gap histogram, a counter of missing slot numbers and a root-lag gauge as Prometheus text, so you can set alerts from your own baseline after the October 2026 changes.

## What changed in October

The Alpenglow switch reached mainnet on 2026-10-21 and changed three numbers that notification alerts were tuned around. A slot is now about 200 ms. Finalized sits 0 to 1 slots behind processed, where it used to sit about 32 behind. Mainnet blocks carry no vote transactions, so per-block transaction counts from before the switch do not compare with counts after it.

Limits written before the switch usually assume a longer slot and a finalized slot far behind the head, so they sit far from where a problem shows up today. New limits need numbers from your own connection, because the path between your process and the socket adds its own timing to every notification.

Three things are worth charting from the client side: how regularly slot notifications arrive, whether slot numbers go missing between them, and how far root trails the newest slot. The sampler below measures all three over one [WebSocket](https://carbium.sh/docs#websocket) connection and prints them as Prometheus text.

## The three series and why each

| Series | Type | What it shows |
|---|---|---|
| `carbium_slot_gap_ms` | histogram | Milliseconds between consecutive slot notifications, taken when each one arrives. A stalled socket or a congested network path pushes counts into the higher buckets. |
| `carbium_slot_jumps_total` | counter | Slot numbers between two consecutive notifications that never arrived. A leader that never produced its slot sends nothing, so a few are normal. A rise over your baseline means more skipped slots or lost notifications. |
| `carbium_slot_root_lag` | gauge | The newest slot minus the newest root from `rootSubscribe`, in slots. |

The gap is a histogram because an average hides a handful of long stalls among many regular intervals, and buckets keep them. The bounds in the code cluster around 200 ms and run out to 2000, so a shift of 50 ms moves counts between neighbouring buckets and a real stall lands in a bucket of its own.

The counter adds the number of missing slot numbers each time, so a gap of five slots adds five. Root lag is the gauge to read against the 0 to 1 slot figure for finalized: it is where that figure shows up on your side of the socket.

## The sampler

The sampler needs no packages, and Node 24 strips the types on load. Set `CARBIUM_KEY`. `CARBIUM_WS_URL` and `WINDOW_MS` (default 20000) are optional, and the code never prints the key.

`main.ts` (run: `node main.ts`)

```ts
// I0052 · Prometheus WebSocket delivery lag
// https://carbium.sh/library/integrate/prometheus-websocket-delivery-lag
// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.
// SPDX-License-Identifier: MIT

const key = process.env.CARBIUM_KEY;
if (!key) {
  console.error("Set CARBIUM_KEY to your Carbium key.");
  process.exit(1);
}
const url = new URL(process.env.CARBIUM_WS_URL ?? "wss://services.carbium.sh");
url.searchParams.set("api-key", key);
const windowMs = Number(process.env.WINDOW_MS ?? 20_000);

const bounds = [50, 100, 150, 200, 250, 300, 400, 600, 1000, 2000]; // gap buckets in ms
const buckets = bounds.map(() => 0);
let count = 0;
let sum = 0;
let jumps = 0;
let lastSlot = 0;
let lastAt = 0;
let lag = Number.NaN;

function onSlot(slot: number, root: number | null): void {
  const now = performance.now();
  if (slot <= lastSlot) return; // repeated or out of order
  if (lastSlot > 0) {
    jumps += slot - lastSlot - 1;
    const ms = now - lastAt;
    count++;
    sum += ms;
    bounds.forEach((b, i) => {
      if (ms <= b) buckets[i]++;
    });
  }
  lastSlot = slot;
  lastAt = now;
  if (root !== null) lag = slot - root;
}

function render(): string {
  return [
    "# HELP carbium_slot_gap_ms Milliseconds between slot notifications as they arrive.",
    "# TYPE carbium_slot_gap_ms histogram",
    ...bounds.map((b, i) => `carbium_slot_gap_ms_bucket{le="${b}"} ${buckets[i]}`),
    `carbium_slot_gap_ms_bucket{le="+Inf"} ${count}`,
    `carbium_slot_gap_ms_sum ${sum.toFixed(1)}`,
    `carbium_slot_gap_ms_count ${count}`,
    "# HELP carbium_slot_jumps_total Slot numbers between two notifications that never arrived.",
    "# TYPE carbium_slot_jumps_total counter",
    `carbium_slot_jumps_total ${jumps}`,
    "# HELP carbium_slot_root_lag Newest slot minus newest root, in slots.",
    "# TYPE carbium_slot_root_lag gauge",
    `carbium_slot_root_lag ${lag}`,
    "",
  ].join("\n");
}

let done = false;
let root: number | null = null;
const ws = new WebSocket(url.href);
function finish(): void {
  if (done) return;
  done = true;
  ws.close();
  if (count === 0) console.error("no slot intervals were measured");
  process.stdout.write(render(), () => process.exit(count > 0 ? 0 : 1));
}

ws.onopen = () => {
  ["slotSubscribe", "rootSubscribe"].forEach((method, i) =>
    ws.send(JSON.stringify({ jsonrpc: "2.0", id: i + 1, method, params: [] })),
  );
  setTimeout(finish, windowMs);
};
ws.onmessage = (ev) => {
  const m = JSON.parse(String(ev.data));
  if (m.error) console.error(`subscribe failed: ${m.error.message}`);
  else if (m.method === "rootNotification") {
    root = m.params.result;
    if (lastSlot > 0 && root !== null) lag = lastSlot - root;
  } else if (m.method === "slotNotification") onSlot(m.params.result.slot, root);
};
ws.onerror = () => console.error("socket error");
ws.onclose = finish;
```

Run it with `node main.ts`. Diagnostics go to stderr and the exposition text goes to stdout, so a redirect captures clean output. The exit code is 0 when at least one interval was measured and 1 when none arrived.

The first slot notification only sets the reference, because an interval needs two arrivals. Intervals therefore start once the connection is open and the subscription answers, and a run holds fewer intervals than the window has slots. At about 0.2 s a slot, a 20 second window covers up to 100 slots.

Opening the slot and root subscriptions costs 5 credits each, then [20 credits per MiB sent](https://carbium.sh/docs#limits), and a 20 second run sends little. Both subscriptions share one socket.

## Take a baseline before alerting

Run the sampler a few dozen times from the host that will run it, across quiet and busy hours, and keep every output. The spread of those runs sets your limits.

- Gap: work out the 95th percentile of each run from its buckets, and set the alert above the worst run you consider healthy.
- Jumps: record the count per run. Skipped slots are part of normal operation, so expect a small positive number on many runs and put the alert above the top of that range.
- Root lag: record the value at exit. Finalized now sits 0 to 1 slots behind processed, so expect small values, and take the exact range from your runs.

A run holds fewer than 100 intervals, so one run gives a coarse percentile. Compare many runs with each other, let a single odd run pass, and alert on a condition that holds across consecutive runs.

Redo the baseline whenever the sampler moves to another host or network. The path adds its own timing to every gap, and the numbers you keep belong to that path.

## Scrape it and alert

The sampler runs once and exits, which fits node_exporter's textfile collector. Run `node main.ts > carbium_ws.tmp && mv carbium_ws.tmp /var/lib/node_exporter/textfile/carbium_ws.prom` from cron or a systemd timer once a minute, with the collector pointed at that directory. The rename keeps a scrape from reading a half-written file, and a failed run exits 1 and leaves the previous file in place.

Each file replaces the last, so the series are a snapshot of the most recent run. Query them without `rate()`.

- `histogram_quantile(0.95, sum by (le) (carbium_slot_gap_ms_bucket)) > 400` fires when the 95th percentile gap passes 400 ms.
- `carbium_slot_jumps_total > 10` fires when a run misses more than 10 slot numbers.
- `carbium_slot_root_lag > 4` fires when root trails the newest slot by more than 4 slots.
- `time() - node_textfile_mtime_seconds{file=~".*carbium_ws.prom"} > 300` fires when the sampler stopped producing output, which covers a refused key and a socket that never delivered.

The numbers 400, 10 and 4 stand in for the limits from your baseline. Give each rule `for: 5m` so one bad run does not page anyone.

## Where it ends

- Arrival times include your network path, TLS and the server. One socket in one place cannot separate them, so a shift in the histogram tells you to look, and locating the hop takes a second sampler on another network.
- A missing slot number looks the same whether the leader skipped the slot or a notification was lost. A slot notified at processed can also end up skipped later when its fork is dropped, so a notification is no proof the slot finalized.
- Each run is a 20 second snapshot with counters that restart at 0, and the gauge keeps only its last value. A spike between two runs, or inside one, goes unseen.
- A socket that closes early ends the run with fewer intervals and possibly an empty histogram. A sampler that stays connected needs the reconnect loop from [WebSocket keepalive and reconnect](https://carbium.sh/library/howsolana/websocket-keepalive-and-reconnect).
- The sampler holds one socket and two subscriptions for its window, and both count against the plan's per-account cap.

## When it goes wrong

| What goes wrong | What to do |
|---|---|
| Exit code 1 with `socket error` on stderr and an empty histogram. | Send the whole key in `CARBIUM_KEY`. An unknown key gets [401 at the door](https://carbium.sh/docs#errors) and the socket never opens. Also check that `CARBIUM_WS_URL` is unset or holds only the endpoint. |
| stderr shows `subscribe failed` and the root-lag gauge prints NaN. | The server refused the subscription, often because the plan's [cap on connections and subscriptions](https://carbium.sh/docs#websocket) is full. Close other clients on the account and run again. A refused subscription costs no credits. NaN also means no root notification arrived in the window. |
| The gap percentile and the jump count climb on every run after the sampler moves to a new host. | The network path changed, so the old baseline no longer applies. Take a fresh set of runs from the new host before you set limits. |
| `carbium_slot_jumps_total` is above 0 on most runs. | Slots whose leader produced nothing send no notification, so some jumps are normal. Use the range from your baseline and alert above it. |
| node_exporter shows no `carbium_slot` series. | Start node_exporter with `--collector.textfile.directory` set to the directory the file lands in, keep the `.prom` ending, and check that the sampler exited 0 so the rename ran. |

## Questions

### Why subscribe instead of polling getSlot?

A poll sees the slot only when you ask, so the gaps between its answers follow your timer. Push notifications arrive when the server sends them, which is what the gap histogram needs.

### Why these bucket bounds?

They cluster around the 200 ms slot with a tail to 2000, so a stall separates from normal traffic. Edit the `bounds` list if your baseline sits somewhere else.

### Can I raise WINDOW_MS?

Yes. A longer window holds more intervals and tightens the percentile. Keep the schedule interval longer than the window so runs do not overlap.

### What does each run cost in credits?

Two subscriptions at 5 credits each to open, so 10, plus [20 credits per MiB sent](https://carbium.sh/docs#limits). A once-a-minute schedule opens them again on every run, and unspent credits stay in your balance.

## Related

- [Slack slot-lag alerts](https://carbium.sh/library/integrate/slack-slot-lag-alerts) (Integrations, I0001)
- [WebSocket keepalive and reconnect](https://carbium.sh/library/howsolana/websocket-keepalive-and-reconnect) (How to use Solana, HS0036)
- [carbium.sh/docs#websocket](https://carbium.sh/docs#websocket)
- [carbium.sh/docs#limits](https://carbium.sh/docs#limits)
- [carbium.sh/docs#errors](https://carbium.sh/docs#errors)
- [carbium.sh/docs#rpc](https://carbium.sh/docs#rpc)
