Hello!
On Thu, Nov 27, 2014 at 11:10 PM, Samarjit Uppal wrote:
> Is there a way to see how much time each api request takes in the access
> logs?
Usually you can just log the $request_time variable. It is usually
good enough though it also includes downstream I/O time:
http://nginx.org/en/docs/http/ngx_http_log_module.html#var_request_time
For more accurate measurement, you need to calculate the time duration
in Lua yourself, write the result into a custom nginx variable, and
log that nginx variable in nginx's log_format directive. You can
directly access the underlying C functions like gettimeofday and
clock_gettime via LuaJIT's FFI. But be aware that such timing code
also have their overhead more or less.
Another way is to sample the time from the outside via dynamic tracing
tools like systemtap and dtrace in a sampling manner. So you pay
literally 0 cost when you're not sampling. And also you have a chance
to get much more different "latency measurements".
For example, the following tool can measure the pure CPU-wise
execution time of your Lua code upon every individual nginx request,
excluding any IO time:
https://github.com/openresty/stapxx#ngx-lua-exec-time
Such per-request pure CPU time measurement is difficult to perform in
Lua though.
And the following tools are handy for measure the (nonblocking)
cosocket receiving latency for every request at real time:
https://github.com/openresty/stapxx#ngx-lua-tcp-recv-time
https://github.com/openresty/stapxx#ngx-lua-tcp-total-recv-time
https://github.com/openresty/stapxx#ngx-lua-udp-recv-time
https://github.com/openresty/stapxx#ngx-lua-udp-total-recv-time
If you just want to profile and optimize your (online) Lua code, then
the Lua-land on-CPU flame graph tools are much more useful:
https://github.com/openresty/stapxx#lj-lua-stacks
Sometimes the C-land on-CPU flame graph can be also useful for online
profiling purposes:
https://github.com/openresty/nginx-systemtap-toolkit#sample-bt
> I dont see any entry related to that. Can we tweak some settings to
> enable detailed log reports per api request?
>
I'm reluctant to add built-in support to ngx_lua for this because
there are many ways to measure different aspects of "time", as
demonstrated above. We've found the systemtap tools mentioned above
much more useful than our own userland timing stats in our own online
Lua apps.
Regards,
-agentzh