Home > Enterprise >  How to log "x-kong-proxy-latency" in custom log formatter in Kong
How to log "x-kong-proxy-latency" in custom log formatter in Kong

Time:11-21

I would like to log values of "x-kong-proxy-latency" and "x-kong-upstream-latency" headers to Kong log. How can I get access to those value in log_format?

      KONG_PROXY_ACCESS_LOG: /dev/stdout custom_formatter
      KONG_NGINX_HTTP_LOG_FORMAT: custom_formatter 'xkpl $$x_kong_proxy_latency'

This gets me an error:

2022-11-20 00:13:18   Run with --v (verbose) or --vv (debug) for more details
2022-11-20 00:14:20 Error: could not prepare Kong prefix at /usr/local/kong: nginx configuration is invalid (exit code 1):
2022-11-20 00:14:20 nginx: [emerg] unknown "x_kong_proxy_latency" variable
2022-11-20 00:14:20 nginx: configuration file /usr/local/kong/nginx.conf test failed

Now, what is the correct way to get this data in a variable?

CodePudding user response:

As the error implied, x_kong_proxy_latency is not a variable nginx knows by default.
x-kong-proxy-latency and x-kong-upstream-latency are HTTP headers that are sent to the client indicating kong latency and upstream latency respectively

Since these headers are created by Kong to send t clients, we can use $sent_http_ nginx's prefix to inject the header into access_log, for example:

KONG_PROXY_ACCESS_LOG:/dev/stdout latency
KONG_NGINX_HTTP_LOG_FORMAT:latency '$$sent_http_x_kong_proxy_latency $$sent_http_x_kong_upstream_latency'

will inject the value of both headers you're looking for.
In case you are looking for more configs, you can look at the nginx document here

  • Related