Giter Club home page Giter Club logo

lua-nginx-prometheus's Introduction

lua-nginx-prometheus

这是一个监控Nginx流量的扩展程序.

介绍

基于Openresty和Prometheus、Consul、Grafana设计的,实现了针对域名和Endpoint级别的流量统计,使用Consul做服务发现、KV存储,Grafana做性能图展示。

最终展现图

主要实现流程

POST Json 注册服务 -> Consul Service <- Prometheus -> 定时抓取 http指标接口 Nginx

POST KV 提交Endpoint -> Consul KV Service <- Nginx 定时更新需要监控的Endpoint

Grafana 读取 -> Prometheus

优点

  • 基本实现自动化集中配置,各种接口使用非常简单
  • 通过Prometheus提供了非常丰富的查询维度,例如(域名、Endpoint、状态码、协议类型、method),当然还可以非常简单地添加更多。
  • Grafana图表功能强大,非常直观地查看各个服务的状态和发现异常。

安装和使用说明

本项目是基于Openresty开发,所以事先安装好Openresty,这个非常简单。

安装Consul,这是基于golang开发的服务自动发现工具,详细查看官方文档。https://www.consul.io/

安装Prometheus,这是一个时序数据库和监控工具,性能和存储十分可靠,把Prometheus配置发现服务使用Consul。官方文档:https://prometheus.io/docs/operating/configuration/#<consul_sd_config>

安装Grafana。https://grafana.com/

安装 本扩展程序

克隆 lua-nginx-prometheus 仓库到Openresty服务器上。

克隆依赖Prometheus nginx-lua-prometheus 仓库到服务器上。

克隆依赖Consul lua-resty-consul 仓库到服务器上。

把lua-nginx-prometheus仓库中的 counter.conf文件复制到Openresty目录下的nginx/conf/conf.d目录内。

编辑 counter.conf 文件

lua_package_path "/Users/zl/Work/Counter/nginx-lua-prometheus/?.lua;;/Users/zl/Work/Counter/lua-resty-consul/lib/resty/?.lua;;/Users/zl/Work/Counter/lib/?.lua;;";

修改lua_package_path参数,把 lua-nginx-prometheus、nginx-lua-prometheus、lua-resty-consul三个目录位置指定,目录下一定是包含 ?.lua。

    consul_host = "<Your consul host ip>"
    consul_port = <Your consul port>

把consul的地址和端口替换上。

server {
    listen 9145;
    allow 127.0.0.1;
    deny all;
    access_log off;
    location /metrics {
        content_by_lua 'prometheus:collect()';
    }
}

添加allow 允许指定ip访问 指标接口。

启动Openresty后,试试 http://<ip>:9145/metrics

配置 Prometheus 服务发现功能

详细参考这个文档

https://prometheus.io/docs/operating/configuration/#<consul_sd_config>

完成后,通过Consul 的 http API进行注册服务。

curl -X PUT -d @test.json http://<ip>:<port>/v1/agent/service/register
{
  "ID": <定义唯一的ID>,
  "Name": "对应prometheus consul_sd_config",
  "Tags": [
    ""
  ],
  "Address": <Openresty地址>,
  "Port": 9145
}

注销服务

curl http://<ip>:<port>/v1/agent/service/deregister/<ID>

配置 Consul KV存储

增加域名和对应的Endpoint

curl --request PUT --data @test.json http://<ip>:<port>/v1/kv/domain/<api.qq.com>/routers

数组

[
"/users/[0-9]+/followers/",
"/users/[0-9]+/",
"/users/[0-9]+/comments/",
"/news"
]

配置Grafana 到 Prometheus上读取数据

详细文档参考 https://prometheus.io/docs/visualization/grafana/

创建图表

常见查询语句

sum(irate(nginx_http_request_duration_seconds_count{host="api.qq.com"}[5m])) by (status)

多台服务器合计每秒请求量,查询单个域名,group by 用状态码

sum(rate(nginx_http_request_duration_seconds_sum{host="api.qq.com",endpoint!="/ws"}[1m])) / sum(rate(nginx_http_request_duration_seconds_count{host="api.qq.com",endpoint!="/ws"}[1m]))

接口平均响应时间, 不包含 websocket接口

histogram 直方图非常有用,可以详细了解一下。

topk(5, sum(rate(nginx_http_request_duration_seconds_sum{host="api.qq.com",endpoint!="/ws"}[1h])) by (endpoint)/sum(rate(nginx_http_request_duration_seconds_count{host="api.qq.com",endpoint!="/ws"}[1h])) by (endpoint))

5个响应时间最大的,不包含 websocket接口

lua-nginx-prometheus's People

Contributors

vovolie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

lua-nginx-prometheus's Issues

新版本Openresty 1.15.8.1 启动报错

测试系统环境: CentOS 7.5.1804
参考安装链接
#1、先安装好Openresty 1.15.8.1

yum install yum-utils
yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo
yum -y install openresty --enablerepo="openresty"

ln -s /usr/local/openresty/nginx/sbin/nginx /usr/local/sbin/nginx

#2、安装 扩展程序

cd /opt
git clone http://www.github.com/hamishforbes/lua-resty-consul
git clone https://github.com/knyar/nginx-lua-prometheus
git clone https://github.com/vovolie/lua-nginx-prometheus
mkdir -p /usr/local/openresty/nginx/conf/conf.d
cp lua-nginx-prometheus/counter.conf /usr/local/openresty/nginx/conf/conf.d/

#3、编辑 counter.conf 文件

[root@test-elk3 conf]# cat /usr/local/openresty/nginx/conf/conf.d/counter.conf
# Please copy to nginx's conf.d directory
# Set search paths for pure Lua external libraries (';;' is the default path):
lua_package_path "/opt/nginx-lua-prometheus/?.lua;;/opt/lua-resty-consul/lib/resty/?.lua;;/opt/lua-nginx-prometheus/lib/?.lua;;";

# Set Prometheus global dict
lua_shared_dict prometheus_metrics 10M; #init 10M memory
lua_shared_dict uri_by_host 10M;
lua_shared_dict global_set 1M;
# Development option, if deploy production, pls cache on!
lua_code_cache off;

init_by_lua_block {
    counter = require 'counter'
    counter.init()
    consul_host = "192.168.9.53"
    consul_port = 8500
}

log_by_lua_block {
    counter.log()
}

# Expose prometheus's metrics scrape port
server {
    listen 9145;
    allow all;
    access_log off;
    location /metrics {
        content_by_lua 'prometheus:collect()';
    }
}

#4、启动openresty报错如下

[root@test-elk3 conf]# nginx
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/conf.d/counter.conf:10
nginx: [error] init_by_lua error: /opt/lua-nginx-prometheus/lib/counter.lua:2: module 'resty.http' not found:
    no field package.preload['resty.http']
    no file '/opt/nginx-lua-prometheus/resty/http.lua'
    no file '/usr/local/openresty/site/lualib/resty/http.ljbc'
    no file '/usr/local/openresty/site/lualib/resty/http/init.ljbc'
    no file '/usr/local/openresty/lualib/resty/http.ljbc'
    no file '/usr/local/openresty/lualib/resty/http/init.ljbc'
    no file '/usr/local/openresty/site/lualib/resty/http.lua'
    no file '/usr/local/openresty/site/lualib/resty/http/init.lua'
    no file '/usr/local/openresty/lualib/resty/http.lua'
    no file '/usr/local/openresty/lualib/resty/http/init.lua'
    no file './resty/http.lua'
    no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
    no file '/usr/local/share/lua/5.1/resty/http.lua'
    no file '/usr/local/share/lua/5.1/resty/http/init.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http/init.lua'
    no file '/opt/lua-resty-consul/lib/resty/resty/http.lua'
    no file '/usr/local/openresty/site/lualib/resty/http.ljbc'
    no file '/usr/local/openresty/site/lualib/resty/http/init.ljbc'
    no file '/usr/local/openresty/lualib/resty/http.ljbc'
    no file '/usr/local/openresty/lualib/resty/http/init.ljbc'
    no file '/usr/local/openresty/site/lualib/resty/http.lua'
    no file '/usr/local/openresty/site/lualib/resty/http/init.lua'
    no file '/usr/local/openresty/lualib/resty/http.lua'
    no file '/usr/local/openresty/lualib/resty/http/init.lua'
    no file './resty/http.lua'
    no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
    no file '/usr/local/share/lua/5.1/resty/http.lua'
    no file '/usr/local/share/lua/5.1/resty/http/init.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http/init.lua'
    no file '/opt/lua-nginx-prometheus/lib/resty/http.lua'
    no file '/usr/local/openresty/site/lualib/resty/http.ljbc'
    no file '/usr/local/openresty/site/lualib/resty/http/init.ljbc'
    no file '/usr/local/openresty/lualib/resty/http.ljbc'
    no file '/usr/local/openresty/lualib/resty/http/init.ljbc'
    no file '/usr/local/openresty/site/lualib/resty/http.lua'
    no file '/usr/local/openresty/site/lualib/resty/http/init.lua'
    no file '/usr/local/openresty/lualib/resty/http.lua'
    no file '/usr/local/openresty/lualib/resty/http/init.lua'
    no file './resty/http.lua'
    no file '/usr/local/openresty/luajit/share/luajit-2.1.0-beta3/resty/http.lua'
    no file '/usr/local/share/lua/5.1/resty/http.lua'
    no file '/usr/local/share/lua/5.1/resty/http/init.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http.lua'
    no file '/usr/local/openresty/luajit/share/lua/5.1/resty/http/init.lua'
    no file '/usr/local/openresty/site/lualib/resty/http.so'
    no file '/usr/local/openresty/lualib/resty/http.so'
    no file './resty/http.so'
    no file '/usr/local/lib/lua/5.1/resty/http.so'
    no file '/usr/local/openresty/luajit/lib/lua/5.1/resty/http.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'
    no file '/usr/local/openresty/site/lualib/resty.so'
    no file '/usr/local/openresty/lualib/resty.so'
    no file './resty.so'
    no file '/usr/local/lib/lua/5.1/resty.so'
    no file '/usr/local/openresty/luajit/lib/lua/5.1/resty.so'
    no file '/usr/local/lib/lua/5.1/loadall.so'
stack traceback:
    [C]: in function 'require'
    /opt/lua-nginx-prometheus/lib/counter.lua:2: in main chunk
    [C]: in function 'require'
    init_by_lua:2: in main chunk

#附上nginx的版本等信息

[root@test-elk3 conf]# nginx -V
nginx version: openresty/1.15.8.1
built by gcc 7.3.1 20180303 (Red Hat 7.3.1-5) (GCC) 
built with OpenSSL 1.1.0j  20 Nov 2018 (running with OpenSSL 1.1.0k  28 May 2019)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl/include' --add-module=../ngx_devel_kit-0.3.1rc1 --add-module=../echo-nginx-module-0.61 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.31 --add-module=../ngx_lua-0.10.15 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.7 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl/lib' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-dtrace-probes --with-stream --with-stream_ssl_preread_module --with-http_ssl_module

谢谢!

无法使用?

你好,我按照你的文档进行安装,访问报错。

consul 和 openresty 正常启动。

ls /data
lua-nginx-prometheus  lua-resty-consul  nginx-lua-prometheus

把counter.conf拷贝到了 conf.d/counter.conf .

修改如下:

lua_package_path "/data/nginx-lua-prometheus/prometheus.lua;;/data/lua-resty-consul/lib/resty/consul.lua;;/data/lua-nginx-prometheus/lib/counter.lua;;";

lua_shared_dict prometheus_metrics 10M; 
lua_shared_dict uri_by_host 10M;
lua_shared_dict global_set 1M;
lua_code_cache off;

init_by_lua_block {
    counter = require 'counter'
    counter.init()
    consul_host = "192.168.111.11"
    consul_port = 8500
}

log_by_lua_block {
    counter.log()
}

server {
    listen 9145;
    allow all;
    location /metrics {
        content_by_lua 'prometheus:collect()';
    }

}

但是访问:http://192.168.111.12:9145/metrics 报500错误。

日志:

2018/04/14 20:23:56 [error] 5202#5202: *1 lua entry thread aborted: runtime error: content_by_lua(counter.conf:23):1: attempt to index global 'prometheus' (a nil value)
stack traceback:
coroutine 0:
        content_by_lua(counter.conf:23): in function <content_by_lua(counter.conf:23):1>, client: 192.168.111.1, server: , request: "GET /metrics HTTP/1.1", host: "192.168.111.12:9145"
2018/04/14 20:23:56 [error] 5202#5202: *1 failed to run log_by_lua*: log_by_lua(counter.conf:17):2: attempt to call field 'log' (a nil value)
stack traceback:
        log_by_lua(counter.conf:17):2: in function <log_by_lua(counter.conf:17):1> while logging request, client: 192.168.111.1, server: , request: "GET /metrics HTTP/1.1", host: "192.168.111.12:9145"

我不知道是不是lua_package_path配置的有问题,但是你的文档中没有写清晰,请告知如何配置???谢谢。

配置Prometheus服务发现

您好!
能否麻烦详细给个关于Prometheus配置Consul 服务发现的例子?我在Consul官方网站下载了软件,解压缩后是一个consul的可执行程序,模具前不清楚Prometheus如何配置服务发现,其它模块都已经安装好了。

谢谢!

no file '/usr/local/lib/lua/5.1/loadall.so'

修改counter.conf里面的路径之后,重启nginx,报错了

lua_package_path "/mnt/work/nginx-lua-prometheus/?.lua;/mnt/work/lua-resty-consul/lib/resty/?.lua;/mnt/work/lua-nginx-prometheus/lib/?.lua;;";

报的错:
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: no file '/usr/local/lib/lua/5.1/loadall.so'
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: stack traceback:
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: [C]: in function 'require'
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: /mnt/work/lua-nginx-prometheus/lib/counter.lua:2: in main chunk
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: [C]: in function 'require'
Jun 15 20:45:24 izuf6278r1bks33022y99pz nginx[32269]: init_by_lua:2: in main chunk
Jun 15 20:45:24 izuf6278r1bks33022y99pz systemd[1]: nginx.service: control process exited, code=exited status=1
Jun 15 20:45:24 izuf6278r1bks33022y99pz systemd[1]: Failed to start nginx.service.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.