Add Correlation IDs to Kong Gateway logs

Uses: Kong Gateway decK
Incompatible with
konnect
Related Resources
Minimum Version
Kong Gateway - 3.4
TL;DR

Define the log format in the nginx_http_log_format parameter, and use $http_{header_name} to reference the header defined in the Correlation ID plugin ($http_Kong_Request_ID for the default header name). Reference the name of the format to use in the proxy_access_log parameter.

Prerequisites

decK is a CLI tool for managing Kong Gateway declaratively with state files. To complete this tutorial you will first need to install decK.

This tutorial requires a Kong Gateway license. Export your license to an environment variable:

export KONG_LICENSE_DATA='LICENSE_CONTENTS_GO_HERE'

Start Kong Gateway

Create the Kong Gateway container with the following environment variables:

  • KONG_NGINX_HTTP_LOG_FORMAT: Defines the log format to use. In this example, we’ll name the format correlation_id_log, and include for each request:
    • The time of the request
    • The method and endpoint
    • The status
    • The value of the header that contains the Correlation ID, Kong-Request-ID in this example
  • KONG_PROXY_ACCESS_LOG: Specifies the log output file and the log format to use
curl -Ls https://get.konghq.com/quickstart | bash -s -- -e KONG_LICENSE_DATA \
   -e "KONG_NGINX_HTTP_LOG_FORMAT=correlation_id_log '\$time_iso8601 - \$request - \$status - Kong-Request-ID: \$http_Kong_Request_ID'" \
   -e "KONG_PROXY_ACCESS_LOG=/dev/stdout correlation_id_log"

Enable the Correlation ID plugin

Enable the Correlation ID plugin to generate a UUID with a counter in a Kong-Request-ID header:

echo '
_format_version: "3.0"
plugins:
  - name: correlation-id
    config:
      header_name: Kong-Request-ID
      generator: uuid#counter
' | deck gateway apply -

Create a Service and a Route

To validate the configuration, we need to create a Gateway Service and a Route:

echo '
_format_version: "3.0"
services:
  - name: example-service
    url: http://httpbin.konghq.com/anything
routes:
  - name: example-route
    paths:
    - "/anything"
    service:
      name: example-service
' | deck gateway apply -

Send a request

Send a request to the Route we created to generate a log entry:

curl -i "http://localhost:8000/anything"

You should see a Kong-Request-ID header in the response.

Validate

To validate, check your Kong Gateway logs. You should see an entry in the format we defined. For example:

2025-04-18T17:44:12+00:00 - GET /anything HTTP/1.1 - 200 - Kong-Request-ID: 8b899f70-2804-44f5-a8be-910bc03c8b00#1

Cleanup

curl -Ls https://get.konghq.com/quickstart | bash -s -- -d
Something wrong?

Help us make these docs great!

Kong Developer docs are open source. If you find these useful and want to make them better, contribute today!