惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
N
Netflix TechBlog - Medium
V
Visual Studio Blog
博客园 - Franky
小众软件
小众软件
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
宝玉的分享
宝玉的分享
量子位
大猫的无限游戏
大猫的无限游戏
人人都是产品经理
人人都是产品经理
V
V2EX
The Cloudflare Blog
月光博客
月光博客
Last Week in AI
Last Week in AI
雷峰网
雷峰网
WordPress大学
WordPress大学
博客园 - 【当耐特】
博客园 - 聂微东
IT之家
IT之家
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻

二丫讲梵

学习周刊-总第258期-2026年第15周 学习周刊-总第257期-2026年第14周 学习周刊-总第256期-2026年第13周 学习周刊-总第255期-2026年第12周 学习周刊-总第254期-2026年第11周 学习周刊-总第253期-2026年第10周 临时插播一条羊毛,免费领取450元大模型API代金券 学习周刊-总第252期-2026年第09周 学习周刊-总第251期-2026年第08周 学习周刊-总第250期-2026年第07周 学习周刊-总第249期-2026年第06周 诚邀评论,聊聊你所知欲知的我 学习周刊-总第248期-2026年第05周 我的QQ动态之2015年 我的QQ动态之2014年 我的QQ动态之2013年 我的QQ动态之2012年 我的QQ动态-2010-2011年 我的QQ动态-创栏小叙 学习周刊-总第247期-2026年第04周 Nexus社区版权益阉割--一文告诉你有哪些版本可以选择 学习周刊-总第246期-2026年第03周 学习周刊-总第245期-2026年第02周 学习周刊-总第244期-2026年第01周 学习周刊-总第243期-2025年第52周 学习周刊-总第242期-2025年第51周 开源项目ZenOps:带你领略禅意运维 学习周刊-总第241期-2025年第50周 用京东金融,享负债人生 学习周刊-总第240期-2025年第49周
prometheus结合nginx-lua-prometheus监控openresty
二丫讲梵 · 2024-10-26 · via 二丫讲梵

# 前提

这个项目是基于lua实现的,因此需要你的nginx支持了ngx_lua (opens new window)模块儿,如果不支持,则无法正常使用。

我这里使用自己封装好了的rpm包进行安装,如果你也需要,可直接用:制作openresty-rpm包 (opens new window)

# 快速配置

将代码拉到本地:

$ wget https://github.com/knyar/nginx-lua-prometheus/archive/refs/tags/0.20240525.zip
$ unzip 0.20240525.zip
$ mv nginx-lua-prometheus-0.20240525 /etc/nginx/lua

1
2
3

然后在Nginx的主配置的http区域添加如下内容:

    lua_shared_dict prometheus_metrics 10M;
    lua_package_path "/etc/nginx/lua/?.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"})
    }

    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})
    }

    server {
        listen 9145;
        server_name _;
        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()
            }
        }
    }

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

之后加载配置文件,会监听在9145端口。

请求验证一下:

$ curl localhost:9145/metrics
# HELP nginx_http_connections Number of HTTP connections
# TYPE nginx_http_connections gauge
nginx_http_connections{state="reading"} 0
nginx_http_connections{state="waiting"} 1
nginx_http_connections{state="writing"} 2
# HELP nginx_http_request_duration_seconds HTTP request latency
# TYPE nginx_http_request_duration_seconds histogram
nginx_http_request_duration_seconds_bucket{host="_",le="0.005"} 11
nginx_http_request_duration_seconds_count{host="_"} 11
nginx_http_request_duration_seconds_count{host="api.micai.online"} 1
nginx_http_request_duration_seconds_sum{host="_"} 0
nginx_http_request_duration_seconds_sum{host="api.micai.online"} 0.986
# HELP nginx_http_requests_total Number of HTTP requests
# TYPE nginx_http_requests_total counter
nginx_http_requests_total{host="_",status="200"} 11

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

然后就是配置该指标在Prometheus中的采集:

  - job_name: "openresty"
    metrics_path: '/metrics'
    static_configs:
      - targets: ['10.0.0.1:9145']
        labels:
          hostname: test-nginx-01
      - targets: ['10.0.0.2:9145']
        labels:
          hostname: test-nginx-02

1
2
3
4
5
6
7
8
9

# 其他配套

grafana大盘: 直接导入 462,如果感觉不够,还可以自行调整。

告警规则: 点我直达 (opens new window)