Hi,
On 5/10/17 3:42 PM, Matías Giorgio wrote:
> Hi folks,
>
> I've been running performance tests on our cluster of Kong and I noticed
> that the documentation is somewhat incomplete or ambiguous in terms of
> what is been reported.
>
> Kong reports 2 different latencies (i.e. upstream latency and latency).
> The documentation doesn't say which time units are used but I assumed
You're right, our fault. There is an open issue about it that was never
addressed:
https://github.com/Mashape/getkong.org/issues/159
> they are milliseconds. Nevertheless, it is unclear what Kong calls
> "upstream". Is it first-byte or last-byte?
It depends, due to the Nginx internals. These values are indeed unclear,
or undocumented sometimes. I was indeed expecting such a question to
surface some day...
> In order to makes this discussion clearer, I drew the query lifecycle as
> follows so we can clearly express what is upstream latency, what is
> total latency, what is Kong latency (which is only reported as a header,
> not as a plugin's metric), and so on. See attachment.
>
> The legend for the attachment is:
> t_0 Client sent 1st byte
> t_1 Kong received 1st byte from Client
> t_2 Kong received full message
> t_3 Kong finished processing / 1st byte sent to upstream
> t_4 Kong sent last byte to upstream
> t_5 Kong received 1st byte from upstream
> t_6 Kong received last byte from upstream
> t_7 Kong finished processing / 1st byte sent to Client
> t_8 Last byte sent to Client
> t_9 Client received full message
Not entirely accurate, due to some Nginx internals. What actually
happens in the scenario of a successful, proxied request is closer to
something like:
1. Client opens connection
2. Kong executes SSL handshake
3. Client sends 1st byte of the request
4. Nginx receives 1st byte
5. Nginx received all request header
6. Kong executes it's early Lua code and determines to which API a
request should be routed to
7. Kong executes all necessary plugins on the request
8. (optional) Kong reads request body (if a plugin needed it)
9. Kong finished processing a request (all plugins ran)
10. Kong chooses a target from its load-balancing component
11. Kong sends request to upstream chosen by load-balancer
12. (if successful, else back to load-balancing) Kong sent last upstream
byte
13. Nginx receives the upstream status line and headers
14. Kong processes upstream status line and headers if necessary
(plugins)
15. Nginx sends the upstream status line and headers to the client
16. Nginx receives 1st byte of response body from upstream
a. Nginx streams upstream response body to client
*OR*, if a plugin needs to:
b. Kong buffers the upstream response chunk by chunk
c. Kong sends the buffered upstream response down to the client at once
17. Kong executes its 'log' phase (last byte already received by client)
I might be forgetting about something here, but this is more or less
what happens on a successful request being proxied.
What Kong measures is:
- Kong-Proxy-Latency response header: tdiff(10 - 4)
This header's intent is to give a notion of how much time was
spent inside of Nginx and Kong before proxying a request
to upstream.
- Kong-Upstream-Latency response header: tdiff(14 - 10)
This header's intent is to give a notion of how much time Nginx
waited for a response from upstream. It is flawed, as it is
tracking time spent waiting from the Lua-land (from Kong
itself), for technical limitations.
And in the basic logging serializer, that all logging plugins currently use:
- latencies.kong: tdiff(10 - 6) + if (16.b/c) { tdiff(16.c - 14) }
The intent of this property is to give an idea of how much time
was spent inside of Kong, overall (before proxying, and during
the response streaming/buffering, until the last byte was
received from upstream and the last plugin ran on the response
body).
- latencies.proxy: This is identical to the Kong-Upstream-Latency
header. A value of -1 indicates the request was not proxied upstream,
and failed/was rejected by Kong (think: authentication failure)
- latencies.request: tdiff(17 - 4)
The intent here is to give a notion of how much time, total,
everything included, was spent inside of Kong.
I hope this can bring some more insight as to what those values are
about, or try to be about. I also hope the explanation is clear enough,
this ended up longer than I expected...
We know they're not perfect, and I think we can improve them and how
they are measuring things or at least, document them better right now.
--
Thibault