Add Prometheus
As with the other services we also use an existing docker image to run the Prometheus server. By mounting a volume to the container we can configure the server as we already did with Kong.
Create the configuration
We will put the configuration for Prometheus in a directory called prometheus:
mkdir prometheus
In this directory we create a file called prometheus.yml with the following content:
# A scrape configuration containing exactly one endpoint to scrape:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: 'kong'
scrape_interval: 15s
static_configs:
- targets: ['kong:8001']
Add a service
Now we can add the Prometheus service to our docker-compose.yaml:
prometheus:
container_name: prometheus
image: prom/prometheus:v2.14.0
depends_on:
- "kong"
restart: always
networks:
- kong-rover-demo
volumes:
- ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml
Configure the plugin
To activate the Prometheus plugin on our service, we only need to add one line to our kong configuration. In the plugin section please add the line - name: prometheus.
plugins:
- name: key-auth
- name: prometheus
Test /metrics
Lets see what has changed and start our services. As we didn’t expose the AdminAPI endpoint we have to call it from within the docker network. We can easily do this with Docker:
docker container exec -i -t kong \
wget -O - http://localhost:8001/metrics
As we didn’t call our API there are only a few metrics available. So lets use our API as we already did by running a few commands (see the (cURL-)commands from the first section).
When we call the /metrics endpoint again, we see a bunch of new metrics. But we want to see them rendered in a dashboard. So lets stop the services again and add Grafana.