Log metrics for a Service, Route (or the deprecated API entity) to a StatsD server. It can also be used to log metrics on Collectd daemon by enabling its Statsd plugin.


Terminology

  • plugin: a plugin executing actions inside Kong before or after a request has been proxied to the upstream API.
  • API: (deprecated) the Kong entity representing your upstream service placed behind Kong, for which Kong proxies requests to.
  • Service: the Kong entity representing an external upstream API or microservice.
  • Route: the Kong entity representing a way to map downstream requests to upstream services.
  • Consumer: the Kong entity representing a developer or machine using the API. When using Kong, a Consumer only communicates with Kong which proxies every call to the said upstream API.
  • Credential: a unique string associated with a Consumer, also referred to as an API key.
  • upstream service: this refers to your own API/service sitting behind Kong, to which client requests are forwarded.

Configuration

Enabling the plugin for a Service

Configure on top of a Service by executing the following request on your Kong server:

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=statsd"  \
    --data "config.host=127.0.0.1" \
    --data "config.port=8125"
  • service: the id or name of the Service that this plugin configuration will target.

Enabling the plugin for a Route

Configure on top of a Route with:

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=statsd"  \
    --data "config.host=127.0.0.1" \
    --data "config.port=8125"
  • route_id: the id of the Route that this plugin configuration will target.

Enabling the plugin for an API

If you are using the deprecated API entity, you can configure on top of an API by executing the following request on your Kong server:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=statsd"  \
    --data "config.host=127.0.0.1" \
    --data "config.port=8125"
  • api: either id or name of the API that this plugin configuration will target.

Enabling the plugin for a Consumer

You can use the http://localhost:8001/plugins endpoint to target Consumers:

$ curl -X POST http://kong:8001/plugins \
    --data "name=statsd" \
    --data "consumer_id={consumer_id}"  \
    --data "config.host=127.0.0.1" \
    --data "config.port=8125"

Where consumer_id: The id of the Consumer we want to associate with this plugin.

You can combine adding consumer_id and service_id in the same request.

Once applied, any user with a valid credential can access the Service/API. To restrict usage to only some of the authenticated users, also add the ACL plugin (not covered here) and create whitelist or blacklist groups of users.

Global plugins

All plugins can be configured using the http:/kong:8001/plugins/ endpoint. A plugin which is not associated to any API, Service, Route or Consumer is considered "global", and will be run on every request. Read the Plugin Reference and the Plugin Precedence sections for more information.

Parameters

Here's a list of all the parameters which can be used in this plugin's configuration:

form parameterdefaultdescription
nameThe name of the plugin to use, in this case statsd
api_idThe id of the API which this plugin will target.
service_idThe id of the Service which this plugin will target.
route_idThe id of the Route which this plugin will target.
consumer_idThe id of the Consumer which this plugin will target.
config.host
optional

127.0.0.1

The IP address or host name to send data to.

config.port
optional

8125

The port to send data to on the upstream server

config.metrics
optional

All metrics
are logged

List of Metrics to be logged. Available values are described under Metrics.

config.prefix
optional

kong

String to be prefixed to each metric's name.

Metrics

Metrics the plugin supports logging into the StatsD server.

Metric description namespace
request_count tracks the request kong.<api_name>.request.count
request_size tracks the request's body size in bytes kong.<api_name>.request.size
response_size tracks the response's body size in bytes kong.<api_name>.response.size
latency tracks the time interval between the request started and response received from the upstream server kong.<api_name>.latency
status_count tracks each status code returned in a response kong.<api_name>.status.<status>.count and kong.<api_name>.status.<status>.total
unique_users tracks unique users who made a requests to the underlying Service/Route (or API) kong.<api_name>.user.uniques
request_per_user tracks request/user kong.<api_name>.user.<consumer_id>.count
upstream_latency tracks the time it took for the final service to process the request kong.<api_name>.upstream_latency
kong_latency tracks the internal Kong latency that it took to run all the plugins kong.<api_name>.kong_latency
status_count_per_user tracks request/status/user kong.<api_name>.user.<customer_id>.status.<status> and kong.<api_name>.user.<customer_id>.status.total

Metric Fields

Plugin can be configured with any combination of Metrics, with each entry containing the following fields.

Field description allowed values
name StatsD metric's name Metrics
stat_type determines what sort of event the metric represents gauge, timer, counter, histogram, meter and set
sample_rate
conditional
sampling rate number
customer_identifier
conditional
authenticated user detail consumer_id, custom_id, username

Metric Requirements

  1. By default all metrics get logged.
  2. Metric with stat_type set to counter or gauge must have sample_rate defined as well.
  3. unique_users metric only works with stat_type as set.
  4. status_count, status_count_per_user and request_per_user work only with stat_type as counter.
  5. status_count_per_user, request_per_user and unique_users must have customer_identifier defined.

Kong Process Errors

This logging plugin will only log HTTP request and response data. If you are looking for the Kong process error file (which is the nginx error file), then you can find it at the following path: {prefix}/logs/error.log

Keep up with the latest features