How to create a simple Prometheus exporter?

There are often scenarios when you need to build something custom on Prometheus. An example on a simple exporter(plugin) for Prometheus.

The simplest way is just to create an API with the /metrics route that will serve your metrics to Prometheus in a typical way. Your API get a response should look like this:

# HELP metric_name metric_help
# TYPE metric_name counter
metric_name 10

Where # HELP is your metric name and some brief description of your metric, # TYPE it is the type of the metric(the list of supported types), and the last row is the value of your metric.

To configure your Prometheus to read from this API(let's imagine it runs locally on a 3000 port), add the next configuration to your Prometheus server:

scrape_configs:
  - job_name: prometheus
    metrics_path: /metrics
    scrape_interval: 30s
    static_configs:
    - targets:
      - localhost:3000

You can also use already existing libraries and frameworks for this purpose. A nice example of a library like this is prom-client for NodeJS.

Last updated