Skip to main content

Imalive 5: from Python to Go, with 7 times less RAM and nothing to change in your deployments

· 5 min read
Idriss Neumann
founder cwcloud.tech

Imalive is the OpenTelemetry and Prometheus metrics exporter we install on all our VMs and Kubernetes clusters. It does the job we would otherwise give to node exporter, and for our needs it does it better: it's also a healthcheck API, an HTTP/TCP monitor and an alerting tool. Everything is described in the documentation.

It's a sidekick: it has to be there everywhere, and nobody should notice it. Yet the Python version took more than 120 MiB of RAM to say "I'm alive". So we rewrote it in Go, and the 5.x versions need about 7 times less memory.

Why not just node exporter?​

Node exporter is the reference to expose the metrics of a Linux machine to Prometheus. But it does only that, and we needed more on each machine:

  • OpenTelemetry as well as Prometheus: imalive can be scraped on /v1/prom, and can also push its metrics, logs and traces to an OTLP collector, which is handy when the machine sits in a private network Prometheus can't reach;
  • a healthcheck API to plug directly into a load balancer;
  • HTTP and TCP monitors, declared in a YAML file, with their result and duration exported as metrics;
  • heartbits in JSON on stdout, to be collected with the rest of the logs;
  • alerts without any extra component: CPU, memory, swap and disk thresholds, Slack and Discord notifications, or a custom webhook.

With node exporter, we would need to add a blackbox exporter, an Alertmanager and a healthcheck endpoint to get there. To be fair, node exporter exposes many more low-level system metrics: if you need detailed kernel, network or filesystem statistics, it remains the right tool, and both can run side by side.

The numbers​

Container RAM and Docker image size, imalive 4.4.1 (Python) versus 5.0.1 (Go)

4.4.1 (Python)5.0.1 (Go)
RAM, idle121 MiB17 MiB
RAM after 5,000 requests125 MiB18 MiB
RAM after 10,000 requests128 MiB18 MiB
Docker image153 MB28 MB

That's around 100 MiB saved per instance. It won't change much on a big server, but it does on a 1 or 2 GB VPS or a Raspberry Pi, and it adds up over a whole fleet.

We ran both versions in Docker on the same arm64 machine, with native builds and the same .env.example. It's a comparison on a single machine, not a full benchmark: expect different absolute values on your own nodes.

A rewrite nobody should notice​

Imalive already runs on all our VMs and clusters, with dashboards and alerts built on top of it. The main constraint was therefore to make the upgrade transparent:

  • same endpoints, with and without the /v1 prefix, including /docs, /redoc and /openapi.json;
  • same JSON payloads and same log format on stdout, so the existing log pipelines keep working;
  • same environment variables and same imalive.yml file for the monitors;
  • same Prometheus metric names, so the Grafana dashboards keep working.

Two things do change: the API counters no longer have a cid label, and the python_* runtime metrics are replaced by the go_* ones.

The metric names were the trickiest part. The OpenTelemetry exporter for Prometheus collapses consecutive underscores, which renamed some of our monitors' metrics and would have silently emptied dashboard panels. Those metrics are now registered directly with the Prometheus Go client, and a unit test pins their names.

Under the hood, it's a single binary built with chi, gopsutil for the system metrics, the OpenTelemetry SDK and the Prometheus client, in an Alpine image. The OpenAPI specification is generated from the router itself, and now documents the response bodies.

What about dependencies?​

We had a surprise on the way: when we rebuilt the last Python version to compare it, it answered a 500 error to every request. Its dependencies were declared as version ranges, and a recent FastAPI release no longer worked with the Prometheus instrumentation library. We had to reinstall the exact versions of the published image to run our measurements.

The published images were fine, but a simple rebuild would have broken our healthchecks. Version ranges without a lock file are a time bomb, in any language. With Go, go.mod and go.sum pin the dependencies by default.

How to upgrade​

Change the image tag, and that's it:

image: comworkio/imalive-api:5.0.2

Keep your .env and imalive.yml files as they are. One behaviour to check if you use HTTP monitors: check_tls: false is now really taken into account.

Installation with Docker Compose, Ansible or Helm, the monitors and the OpenTelemetry configuration are described in the imalive documentation. The source code is available on our GitLab and GitHub.