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:
-
Set the following parameter for Kong Gateway to use the socket package in the Pre-Function/Post-Function plugins:
untrusted_lua_sandbox_requires = socketIf you run Kong in a container, set the following env var instead:
KONG_UNTRUSTED_LUA_SANDBOX_REQUIRES = socket -
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 -
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 -
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