使用Openresty接收前端性能監控指標並存入Redis
阿新 • • 發佈:2019-01-23
keep host time log close 監控 pen geoip type /data/openresty/html/post.lua
local json=require ‘cjson‘; local function close_redis(red) if not red then return end local pool_max_idle_time = 1000 local pool_size = 100 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) if not ok then ngx.say("set keepalive error : ", err) end end local redis = require("resty.redis") local red = redis:new() red:set_timemout(5) local ip = "192.168.0.215" local port = 6379 local ok, err = red:connect(ip, port) if not ok then ngx.say("connect to redis error:",err) return close_redis(red) end local request_method=ngx.var.request_method; if request_method == "GET" then ngx.say(json.encode({"only post"})); return; end; local request_method=ngx.var.request_method; if request_method == "GET" then ngx.say(json.encode({"only post"})); return; end; local headers=ngx.req.get_headers() function get_client_ip() local ip=headers["X-REAL-IP"] or headers["X_FORWARDED_FOR"] or ngx.var.remote_addr or "0.0.0.0" return ip end ngx.req.read_body(); local body_data=json.decode(ngx.req.get_body_data()); body_data["time"]=os.date("%Y/%m/%d %H:%M:%S", os.time()+3600*8) body_data["user-agent"]=headers["user-agent"] body_data["clientIP"]=get_client_ip() body_data["referer"]=headers["referer"] ok, err = red:lpush("logstash-vue", json.encode(body_data)) if not ok then ngx.say("write redis error:", err) return close_redis(red) end ngx.say(json.encode(body_data))
api.conf
server { listen 80 default_server; server_name fengwan.blog.51cto.com; error_log logs/error.log; add_header ‘Access-Control-Allow-Origin‘ ‘*‘; add_header Access-Control-Allow-Headers "X-Requested-With"; add_header Access-Control-Allow-Methods "GET,POST"; location / { lua_code_cache off; default_type application/json; content_by_lua_file /data/openresty/html/post.lua; } }
logstash.conf
input { redis { data_type => "list" key => "logstash-vue" host => "192.168.0.215" port => 6379 threads => 5 } } filter { useragent { source => "user-agent" target => "ua" } date { locale => "en" match => ["time", "yyyy/MM/dd HH:mm:ss"] } geoip { source => "clientIP" database => "/GeoLite2-City.mmdb" } } output { stdout { codec => rubydebug } }
使用Openresty接收前端性能監控指標並存入Redis