Java OpenResty Spring Spring Boot MySQL Redis MongoDB PostgreSQL Linux Android Nginx 面试 算法 小程序 Arthas JVM juc AQS Docker DevOps


Prometheus+Grafana+nginx-lua-prometheus 监控 OpenResty

Prometheus Grafana DevOps 大约 3291 字

查看工作目录

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 端口

http://localhost:9145/metrics

修改 Prometheus 配置

scrape_configs下新增job_nameopenresty(名称随意)

  - 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是否上线。

http://localhost:9090/targets

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

相关链接

https://github.com/knyar/nginx-lua-prometheus

https://luarocks.org/modules/knyar/nginx-lua-prometheus

阅读 571 · 发布于 2022-02-27

————        END        ————

Give me a Star, Thanks:)

https://github.com/fendoudebb

扫描下方二维码关注公众号和小程序↓↓↓

扫描二维码关注我
昵称:
随便看看 换一批