Log metrics for a Service, route
to a StatsD server.
It can also be used to log metrics on Collectd
daemon by enabling its
StatsD plugin.
Starting in Gateway version 3.0.x, StatsD Advanced has been combined with the open-sourced StatsD plugin.
StatsD Advanced has been deprecated.
The StatsD Advanced plugin provides
features not available in the open-source StatsD plugin, such as:
- Ability to choose status codes to log to metrics.
- More granular status codes per workspace.
- Ability to use TCP instead of UDP.
Configuration Reference
This plugin is compatible with DB-less mode.
In DB-less mode, you configure Kong Gateway
declaratively.
Therefore, the Admin API is mostly read-only. The only tasks it can perform are all
related to handling the declarative config, including:
- Setting a target's health status in the load balancer
- Validating configurations against schemas
- Uploading the declarative configuration using the
/config
endpoint
Example plugin configuration
Enable on a service
Enable on a route
Enable on a consumer
Enable globally
The following examples provide some typical configurations for enabling
the statsd-advanced
plugin on a
service.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/services/SERVICE_NAME|SERVICE_ID/plugins \
--data "name=statsd-advanced" \
--data "config.host=127.0.0.1" \
--data "config.port=8125" \
--data "config.allow_status_codes=200-205" \
--data "config.allow_status_codes=400-499"
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: statsd-advanced-example
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
plugin: statsd-advanced
Next, apply the KongPlugin resource to a
service by annotating the
service as follows:
apiVersion: v1
kind: Service
metadata:
name: SERVICE_NAME|SERVICE_ID
labels:
app: SERVICE_NAME|SERVICE_ID
annotations:
konghq.com/plugins: statsd-advanced-example
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
name: SERVICE_NAME|SERVICE_ID
selector:
app: SERVICE_NAME|SERVICE_ID
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: statsd-advanced
service: SERVICE_NAME|SERVICE_ID
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
Replace SERVICE_NAME|SERVICE_ID
with the id
or name
of the service that this plugin configuration will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- In the Plugins section, click Add Plugin.
- Find and select the StatsD Advanced plugin.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Host:
127.0.0.1
- Config.Port:
8125
- Config.Allow Status Codes:
200-205
- Config.Allow Status Codes:
400-499
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Services section, click View for the
service row.
- From the plugin section, click Add Plugin.
- Find and select the StatsD Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already pre-filled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Host:
127.0.0.1
- Config.Port:
8125
- Config.Allow Status Codes:
200-205
- Config.Allow Status Codes:
400-499
- Click Create.
The following examples provide some typical configurations for enabling
the statsd-advanced
plugin on a
route.
Admin API
Kubernetes
Declarative (YAML)
Konnect Cloud
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/routes/ROUTE_NAME|ROUTE_ID/plugins \
--data "name=statsd-advanced" \
--data "config.host=127.0.0.1" \
--data "config.port=8125" \
--data "config.allow_status_codes=200-205" \
--data "config.allow_status_codes=400-499"
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: statsd-advanced-example
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
plugin: statsd-advanced
Then, apply it to an ingress (route or routes)
by annotating the ingress as follows:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ROUTE_NAME|ROUTE_ID
annotations:
kubernetes.io/ingress.class: kong
konghq.com/plugins: statsd-advanced-example
spec:
rules:
- host: examplehostname.com
http:
paths:
- path: /bar
backend:
service:
name: echo
port:
number: 80
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: statsd-advanced
route: ROUTE_NAME
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
Replace ROUTE_NAME|ROUTE_ID
with the id
or name
of the route that this plugin configuration
will target.
You can configure this plugin through the Konnect UI.
From the
Service Hub, select a service version, then set up the plugin:
- Select a route.
- In the Plugins section, click Add Plugin.
- Find and select the StatsD Advanced plugin.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Host:
127.0.0.1
- Config.Port:
8125
- Config.Allow Status Codes:
200-205
- Config.Allow Status Codes:
400-499
- Click Create.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Routes from the menu, then click View for the
route row.
- From the plugin section, click Add Plugin.
- Find and select the StatsD Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the route ID if it is not already prefilled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Host:
127.0.0.1
- Config.Port:
8125
- Config.Allow Status Codes:
200-205
- Config.Allow Status Codes:
400-499
- Click Create.
The following examples provide some typical configurations for enabling
the statsd-advanced
plugin on a
consumer.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/consumers/CONSUMER_NAME|CONSUMER_ID/plugins \
--data "name=statsd-advanced" \
--data "config.host=127.0.0.1" \
--data "config.port=8125" \
--data "config.allow_status_codes=200-205" \
--data "config.allow_status_codes=400-499"
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
You can combine consumer.id
, service.id
, or route.id
in the same request, to further narrow the scope of the plugin.
First, create a KongPlugin
resource:
apiVersion: configuration.konghq.com/v1
kind: KongPlugin
metadata:
name: statsd-advanced-example
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
plugin: statsd-advanced
Then, apply it to a consumer by
annotating the KongConsumer resource as follows:
apiVersion: configuration.konghq.com/v1
kind: KongConsumer
metadata:
name: CONSUMER_NAME|CONSUMER_ID
annotations:
konghq.com/plugins: statsd-advanced-example
kubernetes.io/ingress.class: kong
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
Note: The KongPlugin resource only needs to be defined once
and can be applied to any service, consumer, or route in the namespace. If you
want the plugin to be available cluster-wide, create the resource as a
KongClusterPlugin
instead of KongPlugin
.
Add this section to your declarative configuration file:
plugins:
- name: statsd-advanced
consumer: CONSUMER_NAME|CONSUMER_ID
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
Replace CONSUMER_NAME|CONSUMER_ID
with the id
or name
of the consumer that this plugin configuration will target.
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- From the Consumers section, click View for the consumer row.
- Select the Plugins tab, then click Add Plugin.
- Find and select the StatsD Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, select Scoped.
- Add the consumer ID if it is not already prefilled.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Host:
127.0.0.1
- Config.Port:
8125
- Config.Allow Status Codes:
200-205
- Config.Allow Status Codes:
400-499
- Click Create.
A plugin which is not associated to any 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.
The following examples provide some typical configurations for enabling
the statsd-advanced
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=statsd-advanced" \
--data "config.host=127.0.0.1" \
--data "config.port=8125" \
--data "config.allow_status_codes=200-205" \
--data "config.allow_status_codes=400-499"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-statsd-advanced>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
plugin: statsd-advanced
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: statsd-advanced
config:
host: 127.0.0.1
port: 8125
allow_status_codes:
- 200-205
- 400-499
You can configure this plugin through the Kong Manager UI.
- In Kong Manager, select the workspace.
- Open Plugins from the menu, then click New Plugin.
- Find and select the StatsD Advanced plugin.
Note: If the plugin is greyed out, then it is not available
for your product tier. See Kong Gateway tiers.
- If the option is available, set the plugin scope to Global.
-
Configure the plugin’s parameters.
You can test out the plugin with the following sample configuration:
- Config.Host:
127.0.0.1
- Config.Port:
8125
- Config.Allow Status Codes:
200-205
- Config.Allow Status Codes:
400-499
- Click Create.
Parameters
Here's a list of all the parameters which can be used in this plugin's configuration:
Form Parameter |
Description |
name
required
Type: string
|
The name of the plugin, in this case statsd-advanced . |
instance_name
optional
Type: string
|
An optional custom name to identify an instance of the plugin, for example statsd-advanced_my-service .
Useful when running the same plugin in multiple contexts, for example, on multiple services.
|
service.name or service.id
optional
Type: string
|
The name or ID of the service the plugin targets.
Set one of these parameters if adding the plugin to a service through the top-level /plugins endpoint.
Not required if using /services/SERVICE_NAME|SERVICE_ID/plugins . |
route.name or route.id
optional
Type: string
|
The name or ID of the route the plugin targets.
Set one of these parameters if adding the plugin to a route through the top-level /plugins endpoint.
Not required if using /routes/ROUTE_NAME|ROUTE_ID/plugins . |
consumer.name or consumer.id
optional
Type: string
|
The name or ID of the consumer the plugin targets.
Set one of these parameters if adding the plugin to a consumer through the top-level /plugins endpoint.
Not required if using /consumers/CONSUMER_NAME|CONSUMER_ID/plugins . |
enabled
optional
Type: boolean
Default value: true
|
Whether this plugin will be applied. |
config.host
required
Type: string
Default value: 127.0.0.1
|
The IP address or hostname of the StatsD server.
|
config.port
required
Type: integer
Default value: 8125
|
The port of the StatsD server.
|
config.metrics
required
Type: Array of record elements.
Default value: All metrics are logged.
|
List of Metrics to be logged. Available values are described under Metrics.
|
config.prefix
required
Type: string
Default value: kong
|
String to prefix to each metric’s name.
|
config.hostname_in_prefix
required
Type: boolean
Default value: false
|
Include the hostname in the prefix for each metric name.
|
config.udp_packet_size
required
Type: number
Default value: 0 (not combined)
|
Combine UDP packet up to the size configured. If zero (0), don’t combine the
UDP packet. Must be a number between 0 and 65507 (inclusive).
|
config.use_tcp
required
Type: boolean
Default value: false
|
Use TCP instead of UDP.
|
config.allow_status_codes
required
Type: array of string elements.
Default value: All responses are passed to log metrics.
|
List of status code ranges that are allowed to be logged in metrics.
|
config.consumer_identifier_default
required
Type: string
Default value: custom_id
|
The default consumer identifier for metrics. This will take effect when a metric’s consumer identifier is omitted. Allowed values are custom_id , consumer_id , username .
|
config.service_identifier_default
required
Type: string
Default value: service_name_or_host
|
The default service identifier for metrics. This will take effect when a metric’s service identifier is omitted. Allowed values are service_name_or_host , service_id , service_name , service_host .
|
config.workspace_identifier_default
required
Type: string
Default value: workspace_id
|
The default workspace identifier for metrics. This will take effect when a metric’s workspace identifier is omitted. Allowed values are workspace_id , workspace_name .
|
By default, the plugin sends a packet for each metric it observes. The udp_packet_size
option
configures the greatest datagram size the plugin can combine, this 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 in milliseconds between the request and response. |
kong.service.\<service_identifier>.latency |
status_count |
Tracks each status code returned in a response. |
kong.service.\<service_identifier>.request.status.\<status>.count and kong.\<service_name>.request.status.\<status>.total |
unique_users |
Tracks unique users who made a requests to the underlying service/route. |
kong.service.\<service_identifier>.user.uniques |
request_per_user |
Tracks the request count per consumer. |
kong.service.\<service_identifier>.user.\<consumer_id>.request.count |
upstream_latency |
Tracks the time in milliseconds it took for the final Service to process the request. |
kong.service.\<service_identifier>.upstream_latency |
kong_latency |
Tracks the internal Kong latency that it took to run all of the plugins, in milliseconds. |
kong.service.\<service_identifier>.kong_latency |
status_count_per_user |
Tracks the status code per consumer per service. |
kong.\<service_name>.user.\<customer_id>.request.status.\<status> and kong.\<service_name>.user.\<customer_id>.request.status.total |
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 a 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 when the request started and the response was received from the upstream server. |
kong.global.unmatched.latency |
status_count |
The status count. |
kong.global.unmatched.status.\<status>.count |
kong_latency |
The internal Kong latency that it took to run all of the plugins, in milliseconds. |
kong.global.unmatched.kong_latency |
Metric Fields
The plugin can be configured with any combination of Metrics, with each entry containing the following fields:
Field |
Description |
Datatype |
Allowed values |
name |
StatsD metrics name. Required. |
String |
Metrics |
stat_type |
Determines what sort of event a metric represents. Required. |
String |
gauge , timer , counter , histogram , meter and set |
sample_rate conditional |
Sampling rate. Required. |
Number |
number |
consumer_identifier conditional |
Authenticated user detail. |
String |
One of the following options: consumer_id , custom_id , username , null |
service_identifier conditional |
Service detail. |
String |
One of the following options:service_id , service_name , service_host , service_name_or_host , null |
workspace_identifier conditional |
Workspace detail. |
String |
One of the following options:workspace_id , workspace_name , null |
Metric Behaviors
- By default, all metrics get logged.
- Metric with
stat_type
set to counter
or gauge
must have sample_rate
defined as well.
unique_users
metric only works with stat_type
as set
.
status_count
, status_count_per_user
, status_count_per_user_per_route
and request_per_user
work only with stat_type
as counter
.
shdict_usage
work only with stat_type
as gauge
.
status_count_per_user
, request_per_user
, unique_users
and status_count_per_user_per_route
must have customer_identifier
defined.
- All metrics can optionally configure
service_identifier
; by default it’s set to service_name_or_host
.
status_count_per_workspace
must have workspace_identifier
defined.
Kong Process Errors
This logging plugin logs HTTP request and response data, and also supports streams
data (TCP, TLS, and UDP).
If you are looking for the Kong process error file (which is the nginx error file),
you can find it at the following path:
{prefix}/logs/error.log