forked from kornaz/ngx_stats
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog.lua
More file actions
77 lines (59 loc) · 2.97 KB
/
log.lua
File metadata and controls
77 lines (59 loc) · 2.97 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
ngx.update_time()
local stats = ngx.shared.ngx_stats;
local group = ngx.var.stats_group
local req_time = (tonumber(ngx.now() - ngx.req.start_time()) * 1000)
local status = tostring(ngx.status)
local status_code_class = common.get_status_code_class(status)
local remote_user = ngx.var.remote_user
if remote_user == nil or remote_user == "" then
remote_user = 'unknown_user'
end
-- Geral stats
local upstream_response_time = tonumber(ngx.var.upstream_response_time)
-- Set default group, if it's not defined by nginx variable
if not group or group == "" then
group = 'other'
end
common.incr_or_create(stats, common.key({group, 'requests_total'}), 1)
if req_time >= 0 and req_time < 100 then
common.incr_or_create(stats, common.key({group, 'request_times', '0-100'}), 1)
elseif req_time >= 100 and req_time < 500 then
common.incr_or_create(stats, common.key({group, 'request_times', '100-500'}), 1)
elseif req_time >= 500 and req_time < 1000 then
common.incr_or_create(stats, common.key({group, 'request_times', '500-1000'}), 1)
elseif req_time >= 1000 then
common.incr_or_create(stats, common.key({group, 'request_times', '1000-inf'}), 1)
end
if upstream_response_time then
common.incr_or_create(stats, common.key({group, 'upstream_requests_total'}), 1)
common.incr_or_create(stats, common.key({group, 'upstream_resp_time_sum'}), (upstream_response_time or 0))
end
if common.in_table(ngx.var.upstream_cache_status, cache_status) then
local status = string.lower(ngx.var.upstream_cache_status)
common.incr_or_create(stats, common.key({group, 'cache', status}), 1)
end
common.incr_or_create(stats, common.key({group, 'status', status_code_class}), 1)
common.incr_or_create(stats, common.key({group, 'status_user_' .. status_code_class, remote_user}), 1)
-- Traffic being sent to and from the client
common.update(stats, common.key({group, 'traffic', 'received'}), ngx.var.request_length)
common.update(stats, common.key({group, 'traffic', 'sent'}), ngx.var.bytes_sent)
--[[ Connection statistics
Active connections
==================
The current number of active client connections including Waiting connections.
Reading connections
===================
The current number of connections where nginx is reading the request header.
Waiting connections
===================
The current number of idle client connections waiting for a request.
Writing connections
===================
The current number of connections where nginx is writing the response back to the client.
]]--
common.update(stats, common.key({'connections', 'active'}), ngx.var.connections_active)
common.update(stats, common.key({'connections', 'idle'}), ngx.var.connections_waiting)
common.update(stats, common.key({'connections', 'reading'}), ngx.var.connections_reading)
common.update(stats, common.key({'connections', 'writing'}), ngx.var.connections_writing)
common.update(stats, common.key({'requests', 'current'}), ngx.var.connection_requests)
common.incr_or_create(stats, common.key({'requests', 'total'}), 1)