背景
我們想要從Nginx接受請求開始,生成一個Unique Tracing ID舍咖,不僅記錄在Nginx的日志中矩父,也要貫穿到整個后臺的服務,從而利用這個ID方便問題的排查排霉。
方案一
利用Nginx豐富的內置變量窍株,拼接出一個“unique enough id”。這里使用了五個變量:
- $pid: Nginx worker process id
- $msec: timestamp in millisecond
- $remote_addr: client address
- $connection: TCP connection serial number
- $connection_requests: current number of requests made through a connection
實現(xiàn)步驟
1.在nginx.conf的location模塊里:
location / {
proxy_pass http://upstream;
set $req_id $pid.$msec.$remote_addr.$connection.$connection_requests;
proxy_set_header X-Request-Id $req_id;
}
2.在http模塊的 log_format 里加上 $req_id攻柠,至此Nginx的日志中將包含這個ID
log_format trace '... $req_id';
3.在后臺服務中可以通過下面的方式獲取$req_id
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write(self.request.headers["X-Request-Id"])
4.重啟Nginx
nginx -s reload
問題
格式混亂球订,信息冗余,生成的效果如下:
97372.1493211301.686.127.0.0.1.471.32
方案二
使用Nginx內置的變量 $request_id
這是最直接的辦法瑰钮,使用Nginx自帶的一個$request_id冒滩,一個16位比特的隨機數(shù),用32位的16進制數(shù)表示浪谴。
proxy_set_header X-Request-Id $request_id;
問題
這Nginx 1.11.0 版本新增加的feature开睡,使用Nginx舊版本,或者依賴某些二次開發(fā)的Nginx版本苟耻,例如 Tengine 繼承的是Nginx 1.8.1 版本篇恒,都面臨著升級Nginx的問題。
方案三
使用 Lua 生成一個uuid.
利用Lua輕量小巧的特性梁呈,嵌入到Nginx的配置文件當中婚度,然后生成一個uuid.
實現(xiàn)步驟
1.在 http 模塊里加入:
map $host $uuid {
default '';
}
lua_package_path '/path/to/uuid4.lua';
init_by_lua '
uuid4 = require "uuid4"
math = require "math"
';
2.在server模塊里加入:
set_by_lua $uuid '
return uuid4.getUUID()
';
3.在location模塊里加入:
proxy_set_header X-Request-Id $uuid;
4.uuid4.lua
引用自 第三方庫
--[[
The MIT License (MIT)
Copyright (c) 2012 Toby Jennings
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--]]
local M = {}
-----
math.randomseed( os.time() )
math.random()
-----
local function num2bs(num)
local _mod = math.fmod or math.mod
local _floor = math.floor
--
local result = ""
if(num == 0) then return "0" end
while(num > 0) do
result = _mod(num,2) .. result
num = _floor(num*0.5)
end
return result
end
--
local function bs2num(num)
local _sub = string.sub
local index, result = 0, 0
if(num == "0") then return 0; end
for p=#num,1,-1 do
local this_val = _sub( num, p,p )
if this_val == "1" then
result = result + ( 2^index )
end
index=index+1
end
return result
end
--
local function padbits(num,bits)
if #num == bits then return num end
if #num > bits then print("too many bits") end
local pad = bits - #num
for i=1,pad do
num = "0" .. num
end
return num
end
--
local function getUUID()
local _rnd = math.random
local _fmt = string.format
--
_rnd()
--
local time_low_a = _rnd(0, 65535)
local time_low_b = _rnd(0, 65535)
--
local time_mid = _rnd(0, 65535)
--
local time_hi = _rnd(0, 4095 )
time_hi = padbits( num2bs(time_hi), 12 )
local time_hi_and_version = bs2num( "0100" .. time_hi )
--
local clock_seq_hi_res = _rnd(0,63)
clock_seq_hi_res = padbits( num2bs(clock_seq_hi_res), 6 )
clock_seq_hi_res = "10" .. clock_seq_hi_res
--
local clock_seq_low = _rnd(0,255)
clock_seq_low = padbits( num2bs(clock_seq_low), 8 )
--
local clock_seq = bs2num(clock_seq_hi_res .. clock_seq_low)
--
local node = {}
for i=1,6 do
node[i] = _rnd(0,255)
end
--
local guid = ""
guid = guid .. padbits(_fmt("%X",time_low_a), 4)
guid = guid .. padbits(_fmt("%X",time_low_b), 4)
guid = guid .. padbits(_fmt("%X",time_mid), 4)
guid = guid .. padbits(_fmt("%X",time_hi_and_version), 4)
guid = guid .. padbits(_fmt("%X",clock_seq), 4)
--
for i=1,6 do
guid = guid .. padbits(_fmt("%X",node[i]), 2)
end
--
return guid
end
--
M.getUUID = getUUID
return M
問題
Lua的這個模塊太長蘸秘,擔心性能問題官卡,需要進行性能評估蝗茁。
方案四
還是利用Lua腳本,使用時間戳加隨機數(shù)的方式
關鍵步驟:
set_by_lua $rdm_number '
return os.time() .. os.clock()*100 .. math.random(1000000000, os.time())
';
問題
os.time()的精確度在1秒寻咒,os.clock()的精確度在0.01秒哮翘,這樣處理之后,總的精度在10毫秒毛秘,沒有達到要求饭寺。
Lua有一個 Luasocket 模塊,可以達到毫秒級別的精度叫挟,但是需要安裝艰匙。
方案五
結合Nginx的 $msec 變量和 Lua 的隨機數(shù)
關鍵配置
server {
...
set_by_lua $rdm_number '
return math.random(1000000000, os.time())
';
location / {
...
set $req_id $msec$rdm_number;
proxy_set_header X-Request-Id $req_id;
}
}
終記
最終確定方案五,簡單抹恳,方便员凝,影響最小。
在方案選擇奋献、測試過程中健霹,還遇到了環(huán)境搭建相關的問題,將記錄在下篇文章中瓶蚂,敬請期待糖埋!
參考
1.http://stackoverflow.com/questions/17748735/setting-a-trace-id-in-nginx-load-balancer
2.https://blog.ryandlane.com/2014/12/11/using-lua-in-nginx-for-unique-request-ids-and-millisecond-times-in-logs/
3.http://www.jb51.net/article/82167.htm
4.http://nginx.org/en/docs/http/ngx_http_core_module.html#.24args
5.http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_id