Debug requests

Kong Gateway admins can debug requests by collecting timing information about a given request, on demand. Request debugging is triggered using a secure token and the resulting data is returned in a response header named X-Kong-Request-Debug-Output.

Request debugging provides the following insights:

  • Time spent in various Kong Gateway components, such as plugins, DNS resolution, and load balancing.
  • Contextual information, such as the domain name tried during these processes.

Note: This feature is meant for live debugging. The JSON schema of the header containing the timing should never be considered static and is always subject to change.

Enable request debugging

Request debugging is enabled by default and has the following configurations in kong.conf:

  1. request_debug = on | off # enable or disable request debugging
  2. request_debug_token <token> # Set debug token explicitly. Otherwise, it will be generated randomly when Kong starts, restarts, and reloads.

The debug token (X-Kong-Request-Debug-Token) is used to authenticate the client and send the debug request, preventing abuse.

You can find the debug token in the following locations:

  • Kong Gateway error log: The debug token is logged in the error log (notice level) when Kong Gateway starts, restarts, or reloads. The log line will have the [request-debug] prefix to aid in searching.
  • Filesystem: The debug token is also stored in a file at {prefix}/.request_debug_token and updated when Kong Gateway starts, restarts, or reloads.

Debug request configuration

To debug a request, add the following request headers:

  • At a minimum, you should set the X-Kong-Request-Debug header.
  • If the requests originate from loopback addresses, the X-Kong-Request-Debug-Token header also needs to be set.

X-Kong-Request-Debug header

If the X-Kong-Request-Debug header is set to *, timing information will be collected and exported for the current request.

In Kong Gateway Enterprise, you can also specify a list of filters, separated by commas, to filter the scope of the time information that is collected. Each filters specifies which phase to collect timing information from. The following filters are supported:

  • rewrite
  • access
  • balancer
  • response
  • header_filter
  • body_filter
  • upstream
  • log

If this header isn’t present or contains an unknown value, timing information will not be collected for the current request.

X-Kong-Request-Debug-Log header

If the X-Kong-Request-Debug-Token header is set to true, timing information will also be logged in the Kong Gateway error log with a log level of notice. By default, the X-Kong-Request-Debug-Token header is set to false. The log line will have the [request-debug] prefix to aid in searching.

Debug request example

The following is an example debug request:

  1. curl http://localhost:8000/example \
  2. -H "X-Kong-Request-Debug: *" \
  3. -H "X-Kong-Request-Debug-Token: xxxxxx"

Here’s an example of the output of the response header:

  1. {
  2. "request_id": "2effa21fb2d36d31f80ed02635cde86b",
  3. "workspace_id": "7b7f79f2-8d52-470c-a307-e76f986041cf",
  4. "child": {
  5. "rewrite": {
  6. "total_time": 0
  7. },
  8. "access": {
  9. "child": {
  10. "dns": {
  11. "child": {
  12. "example.host": {
  13. "child": {
  14. "resolve": {
  15. "total_time": 0,
  16. "cache_hit": true
  17. }
  18. },
  19. "total_time": 0
  20. }
  21. },
  22. "total_time": 0
  23. }
  24. },
  25. "total_time": 1
  26. },
  27. "header_filter": {
  28. "total_time": 0
  29. },
  30. "balancer": {
  31. "total_time": 0
  32. },
  33. "upstream": {
  34. "total_time": 100,
  35. "child": {
  36. "time_to_first_byte": {
  37. "total_time": 20
  38. },
  39. "streaming": {
  40. "Total_time": 80
  41. }
  42. }
  43. }
  44. }
  45. }

If you analyze the example debug output, you can see that:

  • The unit of total_time is millisecond.
  • The DNS resolution for example.host was cached, which is why it’s so fast in the example.
  • The upstream took 100ms in this request.
    • The elapsed time from Kong Gateway sending the request to the upstream to Kong Gateway receiving the first byte is 20ms.
    • The elapsed time from the first byte to the last byte from the upstream is 80ms.

You can also filter the debug output:

  1. curl http://localhost:8000/example \
  2. -H "X-Kong-Request-Debug: upstream" \
  3. -H "X-Kong-Request-Debug-Token: xxxxxx"

This will return something like the following:

  1. {
  2. "request_id": "a1a1530f8ddb6f6f2462916ae002b715",
  3. "child": {
  4. "upstream": {
  5. "total_time": 363,
  6. "child": {
  7. "time_to_first_byte": {
  8. "total_time": 363
  9. }
  10. }
  11. }
  12. }
  13. }

Truncation for large debugging output

The downstream system may impose a size restriction on response headers, leading Kong Gateway to truncate the X-Kong-Request-Output if it exceeds 2KB. This truncated output will be unconditionally logged in the error_log.

  1. curl http://localhost:8000/large_debugging_output \
  2. -H "X-Kong-Request-Debug: *" \
  3. -H "X-Kong-Request-Debug-Token: xxxxxx"

This will return something like the following:

  1. {
  2. "request_id": "60ca0a4f8e5e936c43692f49b27d2932",
  3. "truncated": true,
  4. "message": "Output is truncated, please check the error_log for full output by filtering with the request_id."
  5. }

Debug request outputs that exceed 3KB are split into multiple parts with the request_id as an identifier.

Note: The debug output doesn’t have a consistent pattern and may change in the future. It wasn’t designed to be processed by automated tools. Rather, it was intended for human readability.