二丫讲梵 二丫讲梵
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)

二丫讲梵

行者常至,为者常成
首页
  • 最佳实践
  • 迎刃而解
  • Nginx
  • Php
  • Zabbix
  • AWS
  • Prometheus
  • Grafana
  • CentOS
  • Systemd
  • Docker
  • Rancher
  • Ansible
  • Ldap
  • Gitlab
  • GitHub
  • Etcd
  • Consul
  • RabbitMQ
  • Kafka
  • MySql
  • MongoDB
  • OpenVPN
  • KVM
  • VMware
  • Other
  • ELK
  • K8S
  • LLM
  • Nexus
  • Jenkins
  • 随写编年
  • 家人物语
  • 追忆青春
  • 父亲的朋友圈
  • 电影音乐
  • 效率工具
  • 博客相关
  • Shell
  • 前端实践
  • Vue学习笔记
  • Golang学习笔记
  • Golang编程技巧
  • 学习周刊
  • Obsidian插件周刊
关于
友链
  • 本站索引

    • 分类
    • 标签
    • 归档
  • 本站页面

    • 导航
    • 打赏
  • 我的工具

    • 备忘录清单 (opens new window)
    • json2go (opens new window)
    • gopher (opens new window)
    • 微信MD编辑 (opens new window)
    • 国内镜像 (opens new window)
    • 出口IP查询 (opens new window)
    • 代码高亮工具 (opens new window)
  • 外站页面

    • 开往 (opens new window)
    • ldapdoc (opens new window)
    • HowToStartOpenSource (opens new window)
    • vdoing-template (opens new window)
GitHub (opens new window)
  • 最佳实践

  • 迎刃而解

  • Nginx

  • Php

  • Zabbix

  • AWS

  • Prometheus

    • Prometheus安装部署及简单监控
    • Prometheus邮件报警配置
    • Prometheus配置Grafana Dashboard
    • Prometheus 监控之 Redis
    • Prometheus 监控之 MySQL
    • 从CPU的获取来学习理解Prometheus查询语句
    • Prometheus监控之kafka集群
    • Prometheus监控之elasticsearch集群
    • Prometheus关于with和by的作用及用法
    • 利用promwrite对prometheus进行remote-write写入
    • prometheus结合nginx-lua-prometheus监控openresty
      • 前提
      • 快速配置
      • 其他配套
  • Grafana

  • Loki

  • CentOS

  • Supervisord

  • Systemd

  • Docker

  • Docker-Compose

  • Rancher

  • Ansible

  • OpenLdap

  • GitLab

  • GitHub

  • Etcd

  • Consul

  • RabbitMQ

  • Kafka

  • Mysql

  • MongoDB

  • OpenVPN

  • Kvm

  • VMware

  • 配置文件详解

  • Other

  • 运维观止
  • Prometheus
二丫讲梵
2024-10-25
目录

prometheus结合nginx-lua-prometheus监控openresty

文章发布较早,内容可能过时,阅读注意甄别。

# 前提

这个项目是基于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)

微信 支付宝
上次更新: 2024/10/25, 21:38:43
利用promwrite对prometheus进行remote-write写入
grafana绘图使用插件或表盘等资料收集整理

← 利用promwrite对prometheus进行remote-write写入 grafana绘图使用插件或表盘等资料收集整理→

最近更新
01
学习周刊-总第212期-2025年第21周
05-22
02
从赵心童世锦赛夺冠聊聊我的斯诺克情缘
05-16
03
学习周刊-总第211期-2025年第20周
05-15
更多文章>
Theme by Vdoing | Copyright © 2017-2025 | 点击查看十年之约 | 浙ICP备18057030号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式