Correlate requests and responses using a unique ID transmitted over an HTTP header.
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 correlation-id
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=correlation-id" \
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
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: correlation-id-example
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
plugin: correlation-id
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: correlation-id-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: correlation-id
service: SERVICE_NAME|SERVICE_ID
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
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 Correlation ID plugin.
- 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 Correlation ID plugin.
- If the option is available, select Scoped.
- Add the service name and ID to the Service field if it
is not already pre-filled.
- Click Create.
The following examples provide some typical configurations for enabling
the correlation-id
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=correlation-id" \
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
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: correlation-id-example
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
plugin: correlation-id
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: correlation-id-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: correlation-id
route: ROUTE_NAME
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
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 Correlation ID plugin.
- 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 Correlation ID plugin.
- If the option is available, select Scoped.
- Add the route ID if it is not already prefilled.
- Click Create.
The following examples provide some typical configurations for enabling
the correlation-id
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=correlation-id" \
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
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: correlation-id-example
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
plugin: correlation-id
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: correlation-id-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: correlation-id
consumer: CONSUMER_NAME|CONSUMER_ID
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
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 Correlation ID plugin.
- If the option is available, select Scoped.
- Add the consumer ID if it is not already prefilled.
- 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 correlation-id
plugin globally.
Admin API
Kubernetes
Declarative (YAML)
Kong Manager
Make the following request:
curl -X POST http://localhost:8001/plugins/ \
--data "name=correlation-id" \
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
Create a KongClusterPlugin
resource and label it as global:
apiVersion: configuration.konghq.com/v1
kind: KongClusterPlugin
metadata:
name: <global-correlation-id>
annotations:
kubernetes.io/ingress.class: kong
labels:
global: \"true\"
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
plugin: correlation-id
Add a plugins
entry in the declarative
configuration file:
plugins:
- name: correlation-id
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
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 Correlation ID plugin.
- If the option is available, set the plugin scope to Global.
- 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 correlation-id . |
instance_name
optional
Type: string
|
An optional custom name to identify an instance of the plugin, for example correlation-id_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.header_name
optional
Type: string
Default value: Kong-Request-ID
|
The HTTP header name to use for the correlation ID.
|
config.generator
optional
Type: string
Default value: uuid#counter
|
The generator to use for the correlation ID. Accepted values are uuid , uuid#counter , and tracker . See Generators.
|
config.echo_downstream
optional
Type: boolean
Default value: false
|
Whether to echo the header back to downstream (the client).
|
How it works
When you enable this plugin, it adds a new header to all of the requests processed by Kong. This header bears the name configured in config.header_name
, and a unique value is generated according to config.generator
.
This header is always added in calls to your upstream services, and optionally echoed back to your clients according to the config.echo_downstream
setting.
If a header with the same name is already present in the client request, the plugin honors it and does not tamper with it.
Generators
uuid
Format:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
This format generates a heaxadecimal UUID for each request.
uuid#counter
Format:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx#counter
This format generates a single UUID on a per-worker basis, and further the requests simply append a counter to the UUID after a #
character. The counter
value starts at 0
for each worker, and gets incremented independently of the others.
This format provides better performance, but might be harder to store or process for analyzing (due to its format and low cardinality).
tracker
Format:
ip-port-pid-connection-connection_requests-timestamp
This correlation id contains more practical implications for each request.
The following is a detailed description of the field
form parameter |
description |
ip |
an address of the server that accepts a request |
port |
port of the server that accepts a request |
pid |
pid of the nginx worker process |
connection |
connection serial number |
connection_requests |
current number of requests made through a connection |
timestamp |
a floating-point number for the elapsed time in seconds (including milliseconds as the decimal part) from the epoch for the current timestamp from the nginx cached time |
FAQ
Can I see my correlation IDs in my Kong logs?
You can see your correlation ID in the Nginx access log if you edit your Nginx logging parameters.
To edit your Nginx parameters, do the following:
- Locate Kong Gateway’s template files and make a copy of
nginx_kong.lua
.
-
Add a log_format
section on the root level of the config file which includes the
$http_Kong_Request_ID
variable.
In the following example, we create a new log format named customformat
.
It’s a copy of the default combined
log format, but the last line adds
$http_Kong_Request_ID
, preceded by the string Kong-Request-ID=
.
Marking the variable this way is optional, and will make testing the feature easier.
Further customize the log_format
by adding or removing
variables:
log_format customformat '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" '
'Kong-Request-ID="$http_Kong_Request_ID"';
-
Use your custom log format for the proxy access log phase. Locate the following line:
access_log ${PROXY_ACCESS_LOG};
Modify it by adding the customformat
format that we just created:
access_log ${{PROXY_ACCESS_LOG}} customformat;
Note that the file contains several access_log
entries. Only modify the line
that uses ${PROXY_ACCESS_LOG}
.
-
Reload Kong:
-
Tail the access log:
tail /usr/local/kong/logs/access.log
You should now see Correlation ID entries in the access log.
Learn more in Custom Nginx templates & embedding Kong.
You can also use this plugin along with one of the logging plugins, or store the ID on your backend.
Changelog
Kong Gateway 2.2.x
- The plugin now generates a
correlation-id
value by default if the correlation ID header arrives empty.