Prometheus Integration - screenly/balena-prometheus-exporter

The balena-prometheus-exporter project is a Python application that exposes metrics in a format consumable by Prometheus. It uses the prometheus_client library to define and expose the metrics. The metrics are served on a /metrics endpoint, which can be scraped by Prometheus.

Here’s an example of how the metrics are exposed in the balena-prometheus-exporter:

from prometheus_client import Gauge, CollectorRegistry, GENERATE_Latest

# Create a gauge metric
g = Gauge('balena_system_load', 'System load', registry=registry)

# Update the gauge metric
g.set(0.5)

# Expose the metrics
print(registry.generate_latest())

Prometheus can scrape the metrics by using a scrape configuration in its configuration file. Here’s an example of a scrape configuration for the balena-prometheus-exporter:

scrape_configs:
- job_name: 'balena-exporter'
static_configs:
- targets: ['exporter:9323']

In this example, Prometheus will scrape the metrics from the balena-prometheus-exporter running on host exporter on port 9323.

The balena-prometheus-exporter can also be run in a Docker container. In this case, the metrics can be exposed as a sidecar container or as a separate container. Here’s an example of a Docker Compose file that runs the balena-prometheus-exporter as a sidecar container:

version: '3.7'

services:
app:
image: my-app:latest
ports:
- '8000:8000'

exporter:
image: balena-prometheus-exporter:latest
command: -balena.apikey=$BALENA_API_KEY
ports:
- '9323:9323'
depends_on:
- app

In this example, the balena-prometheus-exporter is running as a sidecar container to the app container. The exporter container is exposing the metrics on port 9323, which can be scraped by Prometheus.

The balena-prometheus-exporter also supports multi-target exporter pattern, which allows scraping metrics from multiple targets. Here’s an example of a scrape configuration for a multi-target exporter:

scrape_configs:
- job_name: 'balena-exporter'
static_configs:
- targets: ['exporter:9323']
metrics_path: '/multi'
params:
'targets[]': ['http://localhost:8000', 'http://localhost:8001']

In this example, the balena-prometheus-exporter is configured to scrape metrics from two targets: http://localhost:8000 and http://localhost:8001. The metrics are exposed on the /multi endpoint, and the targets are passed as a parameter.

Sources: