>_ IzzyLab

Hands-on task

This task is step 1 of the course “Monitoring and metrics”

Metric exporter

easy~7 min

Prometheus scrapes metrics over HTTP — from the server's /metrics page. Write /usr/local/bin/export-metric that prints a metric in Prometheus format: the line app_up 1. Run it and save the output to /var/www/html/metrics — nginx is already running and serves that file as http://localhost/metrics.

The script is handy to write in the Editor tab (the /usr/local/bin directory shows up in the file tree), and you can see the result in the Site tab: the home page has a /metrics link — after writing the file, click "Refresh".

What you'll practise:
  • prometheus
  • metrics
  • shell
  • chmod
  • redirect

Topic breakdown

The Prometheus text exposition format is extremely simple: one metric — one line metric_name value, separated by a space; optionally preceded by # HELP and # TYPE metadata lines. An exporter in this model is just a program (here a shell script with a shebang) that prints these lines to stdout, and collecting the data comes down to redirecting the output into a file. Don't forget two things: the file must be made executable via chmod +x, and the redirect should use > (overwrite) rather than >>, so the lines don't get duplicated on repeated runs. Prometheus itself comes for these lines over HTTP — usually at the /metrics path, so the exporter's output is served by a web server or by an HTTP handler built into the application.

What's checked

  1. Script is executable
  2. Script prints the metric
  3. /metrics serves the metric

What it looks like