-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog.lua
49 lines (43 loc) · 1.43 KB
/
log.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
local utils = require "utils"
local redis = require "redtool"
local config = require "config"
local upstream = require "ngx.upstream"
if ngx.var.backend == "" then
ngx.log(ngx.ERR, "invalid domain: ", ngx.var.host)
return
end
local host = ngx.var.host
local status = tonumber(ngx.var.upstream_status)
local cost = tonumber(ngx.var.upstream_response_time)
local function calc_status(premature)
if not utils.check_if_analysis(host) then
return
end
local rds = redis:new()
local status_key = config.REDIS_PREFIX.."erulb:"..host..":status"
local cost_key = config.REDIS_PREFIX.."erulb:"..host..":cost"
local total_key = config.REDIS_PREFIX.."erulb:"..host..":count"
local miss_key = config.REDIS_PREFIX.."erulb:"..host..":miss"
local hit_key = config.REDIS_PREFIX.."erulb:"..host..":hit"
local wrong_key = config.REDIS_PREFIX.."erulb:"..host..":wrong"
local right_key = config.REDIS_PREFIX.."erulb:"..host..":right"
rds:incr(total_key)
if not status then
rds:incr(miss_key)
ngx.log(ngx.ERR, host, ' ', status, ' ', cost)
return
end
rds:incr(hit_key)
rds:hincrby(status_key, status, 1)
if tonumber(status) > 499 then
rds:incr(wrong_key)
else
rds:incr(right_key)
end
rds:incrbyfloat(cost_key, cost)
end
local ok, err = ngx.timer.at(0, calc_status)
if not ok then
ngx.log(ngx.ERR, "failed to create timer: ", err)
return
end