Metrics
In the previous steps, we have explored how to send logs to a centralized server so that activity across multiple services can be viewed on the same screen. In this step, we are going to set up metrics from the petstore
to send it to InfluxDB and then subsequently viewed from Chronograf UI.
To start InfluxDB, Chronograf and Kapacitor, a docker-compose file will be used from the light-docker repository.
Now let’s start all services defined in the docker-compose-metrics.yml
cd ~/networknt/light-docker
docker-compose -f docker-compose-metrics.yml up
First, let’s make sure that InfluxDB and Chronograf can be accessed.
http://localhost:8888/
Make sure Chronograf admin page is shown up and connect to the metrics database.
You need to change the URL from http://localhost:8086 to http://influxdb:8086 and set the database name to metrics.
The user is root
, and the password is root
as well.
Once both Influxdb and Chronograf are up and running, let’s stop the open API petstore container by issuing “docker ps” on another terminal to find out the container_id of the petstore. Note: several docker containers are running, so double check you have picked the right container_id.
Now run the following command to stop the example petstore
docker stop [container_id of petstore]
In the next step, we will start the same container with an externalized metrics config so that the server can connect to the Influxdb container to report the metrics.
To make it simple, we are going to use docker-compose from light-config-test.
There is a folder at ~/networknt/light-config-test/light-example-4j/rest/openapi/petstore/metrics
with security.yml and metrics.yml
Here is the content of metrics.yml
enabled: true
influxdbProtocol: http
influxdbHost: influxdb
influxdbPort: 8086
influxdbName: metrics
influxdbUser: root
influxdbPass: root
reportInMinutes: 1
The security.yml has enableVerifyJwt set as true.
Please note that the above configuration is only for testing.
Now start the docker-compose
cd ~/networknt/light-config-test/light-example-4j/rest/openapi/petstore/metrics
docker-compose up
Access the one endpoint several times with the curl command and wait for one minute.
curl -k -H "Authorization: Bearer eyJraWQiOiIxMDAiLCJhbGciOiJSUzI1NiJ9.eyJpc3MiOiJ1cm46Y29tOm5ldHdvcmtudDpvYXV0aDI6djEiLCJhdWQiOiJ1cm46Y29tLm5ldHdvcmtudCIsImV4cCI6MTc5NDg3MzA1MiwianRpIjoiSjFKdmR1bFFRMUF6cjhTNlJueHEwQSIsImlhdCI6MTQ3OTUxMzA1MiwibmJmIjoxNDc5NTEyOTMyLCJ2ZXJzaW9uIjoiMS4wIiwidXNlcl9pZCI6InN0ZXZlIiwidXNlcl90eXBlIjoiRU1QTE9ZRUUiLCJjbGllbnRfaWQiOiJmN2Q0MjM0OC1jNjQ3LTRlZmItYTUyZC00YzU3ODc0MjFlNzIiLCJzY29wZSI6WyJ3cml0ZTpwZXRzIiwicmVhZDpwZXRzIl19.gUcM-JxNBH7rtoRUlxmaK6P4xZdEOueEqIBNddAAx4SyWSy2sV7d7MjAog6k7bInXzV0PWOZZ-JdgTTSn6jTb4K3Je49BcGz1BRwzTslJIOwmvqyziF3lcg6aF5iWOTjmVEF0zXwMJtMc_IcF9FAA8iQi2s5l0DYgkMrjkQ3fBhWnopgfkzjbCuZU2mHDSQ6DJmomWpnE9hDxBp_lGjsQ73HWNNKN-XmBEzH-nz-K5-2wm_hiCq3d0BXm57VxuL7dxpnIwhOIMAYR04PvYHyz2S-Nu9dw6apenfyKe8-ydVt7KHnnWWmk1ErlFzCHmsDigJz0ku0QX59lM7xY5i4qA" https://localhost:8443/v1/pets/111
Go to http://localhost:8888/ and select metrics database and select “SHOW MEASUREMENTS”. You will find several measurements created. Some of them are for the API view, and some of them are for the client view.
You can use Grafana to connect to InfluxDB and create dashboards on Grafana to visualize the metrics in production.
In this step, we have enabled metrics so that we can have a centralized metrics connection at runtime.