...

/

Using Prometheus and Grafana to Visualize the Metrics

Using Prometheus and Grafana to Visualize the Metrics

Let’s input the metrics to Prometheus and visualize them using Grafana.

Coding example

To be able to pull the metrics, Prometheus needs a proper configuration file. The configuration file that is going to be used is as follows:

Press + to interact
# prometheus.yml
scrape_configs:
- job_name: GoServer
scrape_interval: 5s
static_configs:
- targets: ['goapp:1234']

We tell Prometheus to connect to a host named goapp using port number 1234. Prometheus pulls data every five seconds, according to the value of the scrape_interval field. We should put prometheus.yml in the prometheus directory, which should be in the same directory as the docker-compose.yml file that is presented next.

Prometheus, as well as Grafana, and the Go application are going to run as Docker containers using the next docker-compose.yml file:

Press + to interact
version: "3"
services:
goapp:
image: goapp
container_name: goapp
restart: always
ports:
- 1234:1234
networks:
- monitoring

This is the part that deals with the Go application that collects the metrics. The Docker image name, as well as the internal hostname of the Docker container, is goapp. We should define the port number that is going to be open for connections. In this case, both the internal and external port numbers are 1234. The internal one is mapped to the external one. Additionally, we should put all Docker images under the same network, which in this case is called monitoring ...