Prometheus+Grafana+nginx-lua-prometheus 监控 OpenResty
Prometheus Grafana DevOps About 3,291 words查看工作目录
nginx -V
输出:
--prefix=/usr/local/openresty/nginx
安装 nginx-lua-prometheus
luarocks install nginx-lua-prometheus 0.20210206-1
输出:安装在了/usr/local/openresty/luajit
目录下。
[root@6145512566fd /]# luarocks install nginx-lua-prometheus
Installing https://luarocks.org/nginx-lua-prometheus-0.20210206-1.src.rock
nginx-lua-prometheus 0.20210206-1 depends on lua >= 5.1 (5.1-1 provided by VM)
No existing manifest. Attempting to rebuild...
nginx-lua-prometheus 0.20210206-1 is now installed in /usr/local/openresty/luajit (license: MIT)
添加配置文件
命名为prometheus.conf
lua_shared_dict prometheus_metrics 10M;
# luarocks 安装的 nginx-lua-prometheus 代码路径在 /usr/local/openresty/luajit/share/lua/5.1 下
lua_package_path '$prefix/../luajit/share/lua/5.1/?.lua;$prefix/../luajit/share/lua/5.1/?/init.lua;;';
init_worker_by_lua_block {
prometheus = require("prometheus").init("prometheus_metrics")
metric_requests = prometheus:counter("nginx_http_requests_total", "Number of HTTP requests", {"host", "status"})
metric_latency = prometheus:histogram("nginx_http_request_duration_seconds", "HTTP request latency", {"host"})
metric_connections = prometheus:gauge("nginx_http_connections", "Number of HTTP connections", {"state"})
metric_requests_uri = prometheus:counter("nginx_http_requests_uri_total", "Number of HTTP requests_uri", {"host","uri", "status"})
metric_response_sizes = prometheus:histogram("nginx_http_response_size_bytes", "Size of HTTP responses", nil, {10,100,1000,10000,100000,1000000})
metric_bytes = prometheus:counter("nginx_http_request_size_bytes", "Total size of incoming requests")
}
log_by_lua_block {
metric_requests:inc(1, {ngx.var.server_name, ngx.var.status})
metric_latency:observe(tonumber(ngx.var.request_time), {ngx.var.server_name})
metric_requests_uri:inc(1, {ngx.var.server_name,ngx.var.request_uri, ngx.var.status})
metric_response_sizes:observe(tonumber(ngx.var.bytes_sent))
metric_bytes:inc(tonumber(ngx.var.request_length))
}
server {
listen 9145;
location /metrics {
content_by_lua_block {
metric_connections:set(ngx.var.connections_reading, {"reading"})
metric_connections:set(ngx.var.connections_waiting, {"waiting"})
metric_connections:set(ngx.var.connections_writing, {"writing"})
prometheus:collect()
}
}
}
metrics 端口
修改 Prometheus 配置
scrape_configs
下新增job_name
为openresty
(名称随意)
- job_name: 'openresty'
static_configs:
- targets: ['192.168.0.100:9145']
完整的scrape_configs
如下
scrape_configs:
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
- job_name: 'openresty'
static_configs:
- targets: ['192.168.0.100:9145']
重启 Prometheus
重启后可查看nginx-lua-prometheus
是否上线。
Grafana Dashboard
Nginx Lua: https://grafana.com/grafana/dashboards/10223
备注
Docker
方式启动的话需要额外暴露9145
端口
docker run -d \
--name=openresty \
-p 80:80 -p 9145:9145 \
-v $PWD/openresty/conf/conf.d:/etc/nginx/conf.d \
-v $PWD/openresty/logs:/usr/local/openresty/nginx/logs \
-v $PWD/openresty/html:/usr/local/openresty/nginx/html \
openresty/openresty:1.19.9.1-centos7
相关链接
Views: 5,413 · Posted: 2022-02-27
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...