How do you get the Correlation ID inside the access phase of a custom plugin or a pre-function plugin? During our testing we noticed that if the Correlation ID was manually added as part of the request package prior, it will be picked up successfully by the pre-function plugin inside the Access Phase. However, if we execute the Correlation ID plugin to generate the Correlation ID we then notice that the Correlation ID is not accessible inside the Access Phase.
Kong Gateway: How to get Correlation ID inside the Access Phase when using Correlation ID Plugin
How do you get the Correlation ID inside the Access phase when using the Correlation ID plugin?
Whether the Correlation ID is available in the Access phase of another plugin depends on plugin execution priority. If your custom or pre-function plugin runs before the correlation-id plugin (priority 100001), the ID won’t exist yet. In Kong Gateway Enterprise, use a post-function plugin instead — its priority (-1000) is lower, so it always runs after correlation-id and can read the generated ID via kong.request.get_header("kong-request-id").
Solution
This depends on the plugin execution order. If the Correlation ID plugin runs after your pre-function plugin then it will not be able to capture the Correlation ID being added by the Correlation ID plugin.
The Correlation ID plugin priority changes depending on which type of Kong Gateway is being utilized open-source, free mode or enterprise.
To grab the Correlation ID in Kong Gateway Enterprise, you need to use the post-function plugin as the priority is lower than the Correlation ID.
Plugins with higher priority will be executed first.
PLUGIN PRIORITY
correlation-id 100001
post-function -1000Post-function plugin configuration:
- config:
access:
- kong.log.err(kong.request.get_header("kong-request-id"))Now inside the Kong Gateway logs, you will see the Correlation ID logged:
2023/04/03 13:49:21 [error] 2106#0: *2164920 [kong] [string "kong.log.err(kong.request.get_header("kong-re..."]:1 [post-function] e940eb22-5a83-4821-824a-b8faec93ded9, client: 192.168.0.1, server: kong, request: "POST /test HTTP/1.1", host: "localhost:8000"