> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nerves-hub.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Metrics and Alarms extensions

> What the standalone Metrics and Alarms extensions are, which device clients implement them, and how Nerves devices send the same data through Health.

NervesHub has two extensions that carry part of what the [Health extension](https://docs.nerves-hub.org/extensions/health.md) carries, each at its own pace:

* **Metrics** sends metric readings in batches, each with the device's own timestamp.
* **Alarms** sends each alarm at the moment it is raised or cleared.

> [!WARNING]
>
> `nerves_hub_link` 2.14.0 implements neither extension. Turning **Metrics** or **Alarms** on for a product does nothing for a Nerves device. Nerves devices send metrics and alarms inside the [Health extension](https://docs.nerves-hub.org/extensions/health.md)'s report, and NervesHub stores them in the same place.

## Which clients implement them

| Client | `metrics` | `alarms` |
| - | - | - |
| `nerves_hub_link` 2.14.0 | No. Use Health. | No. Use Health. |
| `nerves_hub_link_atomvm_esp32` | No | Yes, on its main branch. Not in a tagged release as of 0.2.0. |
| `nerves-hub-link-agent` ([Linux agent](https://docs.nerves-hub.org/integrations/linux-agent.md)) | No | No |
| `nerves-hub-link-esp32` | No | No |

Both extensions have a written contract: [`docs/metrics.md`](https://github.com/nerves-hub/nerves_hub_web/blob/main/docs/metrics.md) and [`docs/alarms.md`](https://github.com/nerves-hub/nerves_hub_web/blob/main/docs/alarms.md) in `nerves_hub_web`. The [extensions protocol reference](https://docs.nerves-hub.org/device-api/extensions.md) summarizes the messages.

## Requirements

* NervesHub 2.5.0 or later.
* **Metrics** needs ClickHouse on the instance. Without it, NervesHub does not offer the extension, and devices report metrics through Health. See [Feature prerequisites](https://docs.nerves-hub.org/hosting/feature-prerequisites.md#clickhouse).
* **Alarms** works without ClickHouse. The current alarms are stored either way. Alarm history needs ClickHouse.
* A device client that implements the extension.

## Why they exist

Health sends metrics, metadata and alarms in one report, on a timer NervesHub controls. That timer is hourly while nobody is looking at the device.

* For metrics, the report repeats metadata that only changes when the device is updated. The Metrics extension lets a device sample often, batch readings with their own timestamps, and keep readings taken while offline.
* For alarms, an alarm raised just after a report waits up to an hour to be seen, and one raised and cleared between two reports is never seen. The Alarms extension sends the event when it happens.

Health keeps accepting metrics and alarms.

## Turn them on

**1. Use a client that implements the extension**

See the table above. For AtomVM on ESP32, see [AtomVM on ESP32](https://docs.nerves-hub.org/integrations/atomvm-esp32.md).

**2. Turn it on for the product**

In **Product settings**, on the **General** tab, switch on **Metrics** or **Alarms** in the **Device extensions** card. A product created in the web UI starts with every extension on. A product created through the API or `nh` starts with every extension off.

**3. Check the device setting**

On the device's **Settings** tab, **Metrics** and **Alarms** in the **Extensions** card are on by default.

## Metrics extension behaviour

* NervesHub asks for a report every 15 minutes while nobody has the device's page open, and every 60 seconds while somebody does. A device may also report without being asked.
* A message carries at most 60 reports. Each report has a `timestamp` and a `metrics` map of names to numbers.
* A report whose timestamp is more than 24 hours from the server's clock is skipped. A client with no clock should use Health until its clock is set.
* The rate limit is one message per second per device, with a burst of five.
* The name limits are the same as for Health: 64 bytes per name, 20 names per report by default, numbers only.
* History is kept for 30 days.

## Alarms extension behaviour

* When the extension attaches, NervesHub sends `alarms:sync`. The device answers with `alarms:snapshot`, the full set of alarms raised right now. NervesHub replaces what it has stored with that set.
* After that, the device sends `alarms:raised` and `alarms:cleared` as alarms change.
* A device that attaches Alarms should leave the `alarms` key out of its health reports. A health report with an empty `alarms` map clears every stored alarm.
* NervesHub uses the device's `raised_at` and `cleared_at` when they are no more than 1 hour ahead of its clock and no more than 72 hours in the past. Otherwise it uses the arrival time.
* The rate limit is one message per second per device, with a burst of ten. If NervesHub drops a message, it sends `alarms:sync` ten seconds later to correct its state.
* An alarm name longer than 255 characters is ignored. A leading `Elixir.` is stripped.

## Related pages

* [Extensions overview](https://docs.nerves-hub.org/extensions/overview.md)
* [Health extension](https://docs.nerves-hub.org/extensions/health.md)
* [Health monitoring](https://docs.nerves-hub.org/management/health-monitoring.md)
* [Extensions protocol](https://docs.nerves-hub.org/device-api/extensions.md)
* [ClickHouse for a self-hosted instance](https://docs.nerves-hub.org/hosting/clickhouse.md)


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.