Kong Enterprise only: This plugin is only available with a Kong Enterprise subscription.
Please inquire about Kong Enterprise by contacting us - or start a free trial today.

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.
  • 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.
  • API: a legacy entity used to represent your upstream services. Deprecated in favor of Services since CE 0.13.0 and EE 0.32.

Configuration

Enabling the plugin on a Service

Configure this plugin on a Service by making the following request:

$ curl -X POST http://kong:8001/services/{service}/plugins \
    --data "name=statsd-advanced"  \
    --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 on a Route

Configure this plugin on a Route with:

$ curl -X POST http://kong:8001/routes/{route_id}/plugins \
    --data "name=statsd-advanced"  \
    --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 on a Consumer

You can use the http://localhost:8001/plugins endpoint to enable this plugin on specific Consumers:

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

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

You can combine consumer_id and service_id

in the same request, to furthermore narrow the scope of the plugin.

Enabling the plugin on an API

If you are using an older version of Kong with the legacy API entity (deprecated in favor of Services since CE 0.13.0 and EE 0.32.), you can configure this plugin on top of such an API by making the following request:

$ curl -X POST http://kong:8001/apis/{api}/plugins \
    --data "name=statsd-advanced"  \
    --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.

Global plugins

All plugins can be configured using the http://kong:8001/plugins/ endpoint. A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of Kong) 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-advanced
service_idThe id of the Service which this plugin will target.
route_idThe id of the Route which this plugin will target.
enabledtrueWhether this plugin will be applied.
consumer_idThe id of the Consumer which this plugin will target.
api_idThe id of the API which this plugin will target. Note: The API Entity is deprecated in favor of Services since CE 0.13.0 and EE 0.32.
config.host

127.0.0.1

The IP address or host name of StatsD server to send data to

config.port

8125

The port of StatsD server to send data to

config.metrics

All metrics
are logged

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

config.prefix

kong

String to be prefixed to each metric’s name

config.udp_packet_size

0 (not combined)

Combine UDP packet up to the size configured

config.use_tcp

false

Use TCP instead of UDP

By default the Plugin sends a packet for each metric it observes. udp_packet_size configures the greatest datagram size the Plugin can combine. It should be less than 65507 according to UDP protocol. Please consider the MTU of the network when setting this parameter.

Metrics

Metric description namespace
request_count the request count kong.service.<service_identifier>.request.count
request_size the request’s body size in bytes kong.service.<service_identifier>.request.size
response_size the response’s body size in bytes kong.service.<service_identifier>.response.size
latency the time interval between the request and response kong.service.<service_identifier>.latency
status_count the status code kong.service.<service_identifier>.status.<status>.count
unique_users the unique users who made requests to the underlying Service/Route kong.service.<service_identifier>.user.uniques
request_per_user the request count per Consumer kong.service.<service_identifier>.user.<consumer_id>.count
upstream_latency the time it took for the final Service to process the request kong.service.<service_identifier>.upstream_latency
kong_latency the internal Kong latency that it took to run all the Plugins kong.service.<service_identifier>.kong_latency
status_count_per_user the status code for per Consumer per Service kong.service.<service_identifier>.user.<customer_id>.status.<status>
status_count_per_workspace the status code per Workspace kong.service.<service_identifier>.workspace.<workspace_identifier>.status.<status>
status_count_per_user_per_route the status code per Consumer per Route kong.route.<route_id>.user.<customer_id>.status.<status>
shdict_usage the usage of shared dict, sent once every minute kong.node.<node_hostname>.shdict.<shdict_name>.free_space and kong.node.<node_hostname>.shdict.<shdict_name>.capacity

If a request URI doesn’t match any Routes, the following metrics will be sent instead:

Metric description namespace
request_count the request count kong.global.unmatched.request.count
request_size the request’s body size in bytes kong.global.unmatched.request.size
response_size the response’s body size in bytes kong.global.unmatched.response.size
latency the time interval between the request started and response received from the upstream server kong.global.unmatched.latency
status_count the status code kong.global.unmatched.status.<status>.count
kong_latency the internal Kong latency that it took to run all the Plugins kong.global.unmatched.kong_latency

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
service_identifier
conditional
Service detail service_id, service_name, service_host, service_name_or_host
workspace_identifier
conditional
Workspace detail workspace_id, workspace_name

Metric Behaviors

  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, status_count_per_user_per_route and request_per_user work only with stat_type as counter.
  5. shdict_usage work only with stat_type as gauge.
  6. status_count_per_user, request_per_user, unique_users and status_count_per_user_per_route must have customer_identifier defined.
  7. All metrics can optionally configure service_identifier; by default it’s set to service_name_or_host.
  8. status_count_per_workspace must have workspace_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