Nginx is awesome, but it ' s missing some common features. For instance, a common thing-to-add to access logs are a unique ID per request, so it can track the flow of a single Request through multiple services. Another thing it's missing is the ability to log request_time in milliseconds, rather than seconds with a millisecond gran Ularity. Using Lua, we can add these features ourselves.
I'll show the whole solution, then I'll break it down into parts:
http {... map $host $request _time_ms {default '; The map $host $uuid {default '; } lua_package_path '/etc/nginx/uuid4.lua '; Init_by_lua ' Uuid4 = require "uuid4" math = require "math"; Log_by_lua ' Ngx.var.request_time_ms = Math.floor (Tonumber (ngx.var.request_time) * 1000) '; Log_format Mycustomformat ' [$time _local] $request $status $request _length $bytes _sent $request _time_ms $uuid '; Access_log/var/log/nginx/access.log Mycustomformat, ...} server {... Set_by_lua $uuid ' if ngx.var.http_x_request_id = nil then return Uuid4.getuuid () Else R Eturn ngx.var.http_x_request_id end ';..}
It ' s necessary to set variables before we use them in Lua. Using map is a trick to set variables in the HTTP context (you can ' t use set $variable " in HTTP). For the case of the UUID, we is going to set it on the server section (during the rewrite context), but in case it's not set, We want to avoid throwing errors. Here's how we set these variables:
map $host $request_time_ms { default ‘‘; } map $host $uuid { default ‘‘; }
Next we add a uuid4 library to our path, and include the libraries into our context:
lua_package_path ‘/etc/nginx/uuid4.lua‘; init_by_lua ‘ uuid4 = require "uuid4" math = require "math" ‘;
Using The Log_by_lua function, we'll set the Request_time_ms variable we'll use the Log_format config. This Lua function was called in the log context, before logs was written, allowing us to make the variables available to it :
log_by_lua ‘ ngx.var.request_time_ms = math.floor(tonumber(ngx.var.request_time) * 1000) ‘;
Next we set the log format, and use it for the access log:
log_format mycustomformat ‘[$time_local] "$request" $status $request_length $bytes_sent $request_time_ms $uuid‘; access_log /var/log/nginx/access.log mycustomformat;
Lastly, we set the UUID during the rewrite context in the server section, using Set_by_lua. To facilitate following a request across services, we ll reuse the header if it's already set. If the header isn ' t set, then this request didn ' t come from another service, so we'll generate a UUID:
server {... set_by_lua $uuid ‘ if ngx.var.http_x_request_id == nil then return uuid4.getUUID() else return ngx.var.http_x_request_id end ‘...}
If you ' re trusting this header data on any the, you should is sure to filter/restrict the header appropriately so that th E-Client can ' t change it.
Update (Thursday December): Edited the post to move the UUID generation into the server sections and using Set_by_lua, so that the UUID can set to/f Rom the header to flow through the stacks properly. Shout out to Asher Feldman for working out a better solution with me.
Resources:
Using Lua in Nginx for unique request IDs and millisecond times in logs:http://ryandlane.com/blog/2014/12/11/using-lua-in- nginx-for-unique-request-ids-and-millisecond-times-in-logs/
Simple-Nginx Lua script to add UUID-to-each-request for end-to-end request tracking:https://gist.github.com/erikcw/e999e1f b438dbbb91533
Is there a-to-log a per request unique ID for nginx?:http://serverfault.com/questions/580394/is-there-a-way-to-log-a-p Er-request-unique-id-for-nginx
Http://stackoverflow.com/questions/17748735/setting-a-trace-id-in-nginx-load-balancer
Https://github.com/kali/nginx-operationid
"Schema" Nginx how to set the X-request-id request header, log the request time: milliseconds?