How to get the time when Kong Gateway sends a request to upstream and receives a response

Uses: Kong Gateway
TL;DR

How do I get the time when Kong Gateway sends a request to upstream and the time Kong Gateway gets the whole response from upstream?

Use the Post-Function plugin (end of the access phase) to log the send time, and the Pre-Function plugin (start of the log phase) to log the receive time. Set untrusted_lua_sandbox_requires = socket first so the plugins can call socket.gettime().

Steps

Kong handles requests in phases: the access phase ends just before the request is sent to upstream, and the log phase begins just after the full response is received.

  • Send time: Obtainable at the end of the access phase. The Post-Function plugin runs after all other plugins, so it can capture this timestamp.
  • Receive time: Obtainable at the beginning of the log phase. The Pre-Function plugin runs before all other plugins in the log phase, so it can capture this timestamp.

To implement this:

  1. Set the following parameter for Kong Gateway to use the socket package in the Pre-Function/Post-Function plugins:

    untrusted_lua_sandbox_requires = socket

    If you run Kong in a container, set the following env var instead:

    KONG_UNTRUSTED_LUA_SANDBOX_REQUIRES = socket
  2. Enable the Post-Function plugin on the target Route/Service object to log the send time:

    plugins:
    - name: post-function
      config:
        access:
        - |-
          local socket = require "socket"
          local s_time = socket.gettime()*1000
          kong.log('sending time(ms): ', s_time)
      enabled: true
  3. Enable the Pre-Function plugin on the target Route/Service object to log receive time:

    plugins:
    - name: pre-function
      config:
        log:
        - |-
          local socket = require "socket"
          local r_time = socket.gettime()*1000
          kong.log('receiving time(ms): ', r_time)
      enabled: true
  4. Send a request to Kong Gateway again, and you will see the following times (ms) in the Kong Gateway error log:

  • The time when Kong Gateway sends a request to upstream
  • The time Kong Gateway gets the whole response from upstream

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!