Monitoring with StatsD

StatsD is a network daemon that collects and aggregates performance metrics by listening on the network for text based statistics data, published by applications.

This guide will help you setup a test Kong Gateway and StatsD service. Then you will generate sample requests to Kong Gateway and observe the collected monitoring data.

Prerequisites

  • Docker is used to run StatsD and supporting services locally.
  • curl is used to send requests to Kong Gateway. curl is pre-installed on most systems.
  • Netcat is installed as nc on the system PATH. nc is used to send requests to the StatsD management interface. nc is pre-installed on many systems.

Configure StatsD monitoring

  1. Install Kong Gateway:

    This step is optional if you wish to use an existing Kong Gateway installation. When using an existing Kong Gateway, you will need to modify the commands to account for network connectivity and installed Kong Gateway services and routes.

    1. curl -Ls https://get.konghq.com/quickstart | bash -s -- -m

    The -m flag instructs the script to install a mock service that is used in this guide to generate sample metrics.

    Once the Kong Gateway is ready, you will see the following message:

    1. Kong is ready!
  2. Run a StatsD container to capture monitoring data:

    1. docker run -d --rm -p 8126:8126 \
    2. --name kong-quickstart-statsd --network=kong-quickstart-net \
    3. statsd/statsd:latest
  3. Install the StatsD Kong Gateway plugin, configuring the hostname and port of the listening StatsD service:

    1. curl -X POST http://localhost:8001/plugins/ \
    2. --data "name=statsd" \
    3. --data "config.host=kong-quickstart-statsd" \
    4. --data "config.port=8125"

    You should receive a JSON response with details about the installed plugin.

  4. Generate sample traffic to the mock service. This allows you to observe metrics generated from the StatsD plugin. The following command generates 60 requests over one minute. Run the following in a new terminal:

    1. for _ in {1..60}; do {curl localhost:8000/mock/request; sleep 1; } done
  5. Query the StatsD management interface to see the collected metrics from Kong Gateway:

    1. echo "counters" | nc localhost 8126

    You should see a response similar to the following:

    1. {
    2. 'statsd.bad_lines_seen': 0,
    3. 'statsd.packets_received': 56,
    4. 'statsd.metrics_received': 56,
    5. 'kong.mock.request.count': 7,
    6. 'kong.mock.request.status.200': 7,
    7. 'kong.mock.request.status.total': 7
    8. }
    9. END

See the StatsD plugin documentation for more information about how to use and configure the plugin.

Clean up

Once you are done experimenting with StatsD and Kong Gateway, you can use the following commands to stop and remove the software ran in this guide:

  1. docker stop kong-quickstart-statsd
  2. curl -Ls https://get.konghq.com/quickstart | bash -s -- -d

More information