如何修改influxdb表结构_使用nginx-lua修改influxdb API的返回结构
有一個API平臺服務,所有接口都通過API平臺轉(zhuǎn)發(fā)到實際的服務上,然后再把實際服務的結(jié)果返回給客戶端,API平臺的規(guī)范是所有實際服務的接口返回都要統(tǒng)一結(jié)構(gòu)為
{
"code": 0,
"msg": "",
"data": {}
}
否則無法處理,現(xiàn)在使用influxdb提供的api,他的返回結(jié)構(gòu)并不是API平臺需要的結(jié)構(gòu),所以需要做一層代理轉(zhuǎn)發(fā)請求并修改返回結(jié)果為API平臺需要的結(jié)構(gòu)。
采用openresty來實現(xiàn)這個需求,只需安裝好openresty,然后編寫一個配置文件即可實現(xiàn)。
實現(xiàn)配置如下:ngx_lua_restructure_influxdb_response.conf
upstream influxdb{
server 127.0.0.1:8086;
}
server {
listen 18086;
proxy_pass_request_headers off; # handle gziped capture
location / {
proxy_pass http://influxdb/query;
}
location /query {
add_header Content-Type 'application/json; charset=utf-8'; # make sure content-type is json
content_by_lua_block {
local method_map = {
GET = ngx.HTTP_GET,
POST = ngx.HTTP_POST,
}
ngx.req.read_body()
local method = method_map[ngx.req.get_method()]
local data = ngx.req.get_body_data()
local args = ngx.req.get_uri_args()
local res = ngx.location.capture("/", {method=method, data=data, args=args})
local cjson = require "cjson"
local new_body = {}
if res.status ~= ngx.HTTP_OK then
new_body["code"] = res.status
else
new_body["code"] = 0
end
new_body["msg"] = ''
new_body["data"] = cjson.decode(res.body)
ngx.say(cjson.encode(new_body))
}
}
}
influxdb的api在8086端口,所有直接的請求返回結(jié)果為:
# curl -Gs '100.113.164.108:8086/query?db=data&q=SELECT+*+FROM+cpu+limit+1&pretty=true'
{
"results": [
{
"statement_id": 0,
"series": [
{
"name": "cpu",
"columns": [
"time",
"cpu",
"host",
"usage_guest",
"usage_guest_nice",
"usage_idle",
"usage_iowait",
"usage_irq",
"usage_nice",
"usage_softirq",
"usage_steal",
"usage_system",
"usage_user"
],
"values": [
[
"2017-12-08T08:30:00Z",
"cpu-total",
"Tencent-SNG",
0,
0,
96.92993315559063,
0.4456548651008583,
0,
0,
0,
0,
1.8321366676828983,
0.7922753157508718
]
]
}
]
}
]
}
經(jīng)過18086端口的轉(zhuǎn)發(fā)后得到的結(jié)果為:
# curl -Gs '100.113.164.108:18086/query?db=db_zhiyun&q=SELECT * FROM cpu limit 1&pretty=true' | python -m json.tool
{
"code": 0,
"data": {
"results": [
{
"series": [
{
"columns": [
"time",
"cpu",
"host",
"usage_guest",
"usage_guest_nice",
"usage_idle",
"usage_iowait",
"usage_irq",
"usage_nice",
"usage_softirq",
"usage_steal",
"usage_system",
"usage_user"
],
"name": "cpu",
"values": [
[
"2017-12-08T08:30:00Z",
"cpu-total",
"Tencent-SNG",
0,
0,
96.929933155591,
0.44565486510086,
0,
0,
0,
0,
1.8321366676829,
0.79227531575087
]
]
}
],
"statement_id": 0
}
]
},
"msg": ""
}
實現(xiàn)中遇到的坑:
使用curl請求可以得到正確結(jié)果,但是通過API平臺過來的請求全部報錯,錯誤信息為在capture后拿到的body是一傳亂碼,并不是json。
使用ngx.req.raw_header()查看curl和API平臺請求的header信息的區(qū)別,發(fā)現(xiàn)curl沒有設置Accept-Encoding,而API平臺的請求都帶有Accept-Encoding: gzip, deflate,導致capture子請求(即請求真實influxdb接口)的返回結(jié)果被gzip壓縮后把json變成了亂碼,
從而導致在后面cjson.decode的時候出錯。
OPTION ONE
Placed:
more_clear_input_headers Accept-Encoding;
in "location /cap2"
OPTION TWO
Placed:
proxy_pass_request_headers off;
in "location /cap2"
OPTION THREE
Downloaded https://github.com/brimworks/lua-zlib :
make linux
cp zlib.so /usr/lib/lua/5.1/zlib.so
added following to conf:
local zlib = require("zlib")
local stream = zlib.inflate()
local inflated_body = stream(res1.body)
local res2 = ngx.location.capture("/cap2?"..inflated_body)
由于外層的nginx配置默認的content-type為application/octet-stream;,所以為了確保返回為json,又在/query中明確的設置了content-type
總結(jié)
以上是生活随笔為你收集整理的如何修改influxdb表结构_使用nginx-lua修改influxdb API的返回结构的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: pac代理模式什么意思_满镒财务:代理记
- 下一篇: 基金投资亏损回本方法 选择合适位置进行加