🎓 A1Learner
🔍
0 of 50 lessons completed

Prometheus: Scrape Your App

A scrape target is an address Prometheus visits to collect metrics. Adding ShopSentry as a target starts its numbers flowing into Prometheus.

The ShopSentry target

Three lines in prometheus.yml add the shop as a scrape target.

- job_name: shopsentry
  static_configs:
    - targets: ['localhost:18102']

Check the config file

promtool checks the file before Prometheus loads it.

promtool check config prometheus.yml

List target health

The targets API reports every job and whether it answers.

curl -s "http://localhost:19102/api/v1/targets" | python3 -c "import json,sys; d=json.load(sys.stdin); print([(t['labels']['job'], t['health']) for t in d['data']['activeTargets']])"

Example

curl -s -G "http://localhost:19102/api/v1/query" --data-urlencode 'query=up{job="shopsentry"}'

Key points

  • A target is one address plus a job name in the Prometheus config file.
  • The job name groups the target, so its series carry job="shopsentry".
  • The config file lists targets under scrape_configs, one job per service.
  • Prometheus checks the config file with promtool before it trusts the change.
  • The Targets page shows each target and its health, with up meaning healthy.
  • The Docker Compose file adds this target the same way, and that step is stated here rather than run on this machine.

In this example, the query asks Prometheus whether the ShopSentry target is up. The answer is 1, which means the target answers and its metrics are flowing. The same check on the Targets page shows shopsentry as up.

Note

A target that answers is up, and a target that fails to answer is down.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What does up equal to 1 mean for the shopsentry target?