Correlate requests and responses using a unique ID transmitted over an HTTP header.
Terminology
plugin
: a plugin executing actions inside IAM before or after a request has been proxied to the upstream API.Service
: the IAM entity representing an external upstream API or microservice.Route
: the IAM entity representing a way to map downstream requests to upstream services.Consumer
: the IAM entity representing a developer or machine using the API. When using IAM, a Consumer only communicates with IAM 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 IAM, to which client requests are forwarded.
Configuration
This plugin is compatible with requests with the following protocols:
http
https
This plugin is compatible with DB-less mode.
Enabling the plugin on a Service
With a database
Configure this plugin on a Service by making the following request:
$ curl -X POST http://localhost:8001/services/{service}/plugins \
--data "name=correlation-id" \
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
Without a database
Configure this plugin on a Service by adding this section do your declarative configuration file:
plugins:
- name: correlation-id
service: {service}
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
In both cases, {service}
is the id
or name
of the Service that this plugin configuration will target.
Enabling the plugin on a Route
With a database
Configure this plugin on a Route with:
$ curl -X POST http://localhost:8001/routes/{route}/plugins \
--data "name=correlation-id" \
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
Without a database
Configure this plugin on a Route by adding this section do your declarative configuration file:
plugins:
- name: correlation-id
route: {route}
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
In both cases, {route}
is the id
or name
of the Route that this plugin configuration will target.
Enabling the plugin on a Consumer
With a database
You can use the http://localhost:8001/plugins
endpoint to enable this plugin
on specific Consumers:
$ curl -X POST http://localhost:8001/consumers/{consumer}/plugins \
--data "name=correlation-id" \
\
--data "config.header_name=Kong-Request-ID" \
--data "config.generator=uuid#counter" \
--data "config.echo_downstream=false"
Without a database
Configure this plugin on a Consumer by adding this section do your declarative configuration file:
plugins:
- name: correlation-id
consumer: {consumer}
config:
header_name: Kong-Request-ID
generator: uuid#counter
echo_downstream: false
In both cases, {consumer}
is the id
or username
of the Consumer that this plugin configuration will target.
You can combine consumer_id
and
service_id
in the same request, to furthermore narrow the scope of the plugin.
Global plugins
- Using a database, all plugins can be configured using the
http://localhost:8001/plugins/
endpoint. - Without a database, all plugins can be configured via the
plugins:
entry on the declarative configuration file.
A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of IAM) 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 parameter | description |
---|---|
name | The name of the plugin to use, in this case correlation-id |
service_id | The id of the Service which this plugin will target. |
route_id | The id of the Route which this plugin will target. |
enabled default value: true | Whether this plugin will be applied. |
consumer_id | The id of the Consumer which this plugin will target. |
config.header_name
optional default value:
|
The HTTP header name to use for the correlation ID. |
config.generator
optional default value:
|
The generator to use for the correlation ID. Accepted values are |
config.echo_downstream
optional default value:
|
Whether to echo the header back to downstream (the client). |
How it works
When enabled, this plugin will add a new header to all of the requests processed by IAM. This header bears the name configured in config.header_name
, and a unique value generated according to config.generator
.
This header is always added to calls to your upstream services, and optionally echoed back to your clients according to the config.echo_downstream
setting.
If a header bearing the same name is already present in the client request, it is honored and this plugin will not temper with it.
Generators
uuid
Format:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Using this format, a UUID is generated in its hexadecimal form for each request.
uuid#counter
Format:
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx#counter
In this format, a single UUID is generated on a per-worker basis, and further 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 a 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
In this format, the 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 which accepted a request |
port |
port of the server which accepted 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 time stamp from the nginx cached time |
FAQ
Can I see my correlation ids in my IAM logs?
The correlation id will not show up in the Nginx access or error logs. As such, we suggest you use this plugin alongside one of the Logging plugins, or store this id on your backend-side.