forked from oakman/nginx-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.vhost
140 lines (122 loc) · 6.16 KB
/
metrics.vhost
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
lua_shared_dict prometheus_metrics 10M;
lua_package_path '/usr/local/openresty/luajit/lib/?.lua;;';
init_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
http_requests = prometheus:counter(
"nginx_http_requests", "Number of HTTP requests", {"host", "status"})
http_request_time = prometheus:histogram(
"nginx_http_request_time", "HTTP request time", {"host"})
http_request_bytes_received = prometheus:counter(
"nginx_http_request_bytes_received", "Number of HTTP request bytes received", {"host"})
http_request_bytes_sent = prometheus:counter(
"nginx_http_request_bytes_sent", "Number of HTTP request bytes sent", {"host"})
http_connections = prometheus:gauge(
"nginx_http_connections", "Number of HTTP connections", {"state"})
http_upstream_cache_status = prometheus:counter(
"nginx_http_upstream_cache_status", "Number of HTTP upstream cache status", {"host", "status"})
http_upstream_requests = prometheus:counter(
"nginx_http_upstream_requests", "Number of HTTP upstream requests", {"addr", "status"})
http_upstream_response_time = prometheus:histogram(
"nginx_http_upstream_response_time", "HTTP upstream response time", {"addr"})
http_upstream_header_time = prometheus:histogram(
"nginx_http_upstream_header_time", "HTTP upstream header time", {"addr"})
http_upstream_bytes_received = prometheus:counter(
"nginx_http_upstream_bytes_received", "Number of HTTP upstream bytes received", {"addr"})
http_upstream_bytes_sent = prometheus:counter(
"nginx_http_upstream_bytes_sent", "Number of HTTP upstream bytes sent", {"addr"})
http_upstream_connect_time = prometheus:histogram(
"nginx_http_upstream_connect_time", "HTTP upstream connect time", {"addr"})
http_upstream_first_byte_time = prometheus:histogram(
"nginx_http_upstream_first_byte_time", "HTTP upstream first byte time", {"addr"})
http_upstream_session_time = prometheus:histogram(
"nginx_http_upstream_session_time", "HTTP upstream session time", {"addr"})
}
log_by_lua_block {
local function split(str)
local array = {}
for mem in string.gmatch(str, '([^, ]+)') do
table.insert(array, mem)
end
return array
end
local function getWithIndex(str, idx)
if str == nil then
return nil
end
return split(str)[idx]
end
local host = ngx.var.host
local status = ngx.var.status
http_requests:inc(1, {host, status})
http_request_time:observe(ngx.now() - ngx.req.start_time(), {host})
http_request_bytes_sent:inc(tonumber(ngx.var.bytes_sent), {host})
if ngx.var.bytes_received ~= nil then
http_request_bytes_received:inc(tonumber(ngx.var.bytes_received), {host})
end
local upstream_cache_status = ngx.var.upstream_cache_status
if upstream_cache_status ~= nil then
http_upstream_cache_status:inc(1, {host, upstream_cache_status})
end
local upstream_addr = ngx.var.upstream_addr
if upstream_addr ~= nil then
local addrs = split(upstream_addr)
local upstream_status = ngx.var.upstream_status
local upstream_response_time = ngx.var.upstream_response_time
local upstream_connect_time = ngx.var.upstream_connect_time
local upstream_first_byte_time = ngx.var.upstream_first_byte_time
local upstream_header_time = ngx.var.upstream_header_time
local upstream_session_time = ngx.var.upstream_session_time
local upstream_bytes_received = ngx.var.upstream_bytes_received
local upstream_bytes_sent = ngx.var.upstream_bytes_sent
-- compatible for nginx commas format
for idx, addr in ipairs(addrs) do
if table.getn(addrs) > 1 then
upstream_status = getWithIndex(ngx.var.upstream_status, idx)
upstream_response_time = getWithIndex(ngx.var.upstream_response_time, idx)
upstream_connect_time = getWithIndex(ngx.var.upstream_connect_time, idx)
upstream_first_byte_time = getWithIndex(ngx.var.upstream_first_byte_time, idx)
upstream_header_time = getWithIndex(ngx.var.upstream_header_time, idx)
upstream_session_time = getWithIndex(ngx.var.upstream_session_time, idx)
upstream_bytes_received = getWithIndex(ngx.var.upstream_bytes_received, idx)
upstream_bytes_sent = getWithIndex(ngx.var.upstream_bytes_sent, idx)
end
http_upstream_requests:inc(1, {addr, upstream_status})
http_upstream_response_time:observe(tonumber(upstream_response_time), {addr})
http_upstream_header_time:observe(tonumber(upstream_header_time), {addr})
-- ngx.config.nginx_version >= 1011004
if upstream_first_byte_time ~= nil then
http_upstream_first_byte_time:observe(tonumber(upstream_first_byte_time), {addr})
end
if upstream_connect_time ~= nil then
http_upstream_connect_time:observe(tonumber(upstream_connect_time), {addr})
end
if upstream_session_time ~= nil then
http_upstream_session_time:observe(tonumber(upstream_session_time), {addr})
end
if upstream_bytes_received ~= nil then
http_upstream_bytes_received:inc(tonumber(upstream_bytes_received), {addr})
end
if upstream_bytes_sent ~= nil then
http_upstream_bytes_sent:inc(tonumber(upstream_bytes_sent), {addr})
end
end
end
}
server {
listen 9527;
location /status {
vhost_traffic_status_display;
vhost_traffic_status_display_format html;
}
location /metrics {
content_by_lua_block {
if ngx.var.connections_active ~= nil then
http_connections:set(ngx.var.connections_active, {"active"})
http_connections:set(ngx.var.connections_reading, {"reading"})
http_connections:set(ngx.var.connections_waiting, {"waiting"})
http_connections:set(ngx.var.connections_writing, {"writing"})
end
prometheus:collect()
}
}
}