1 implementation

The nginx-Lua-Prometheus library was used to collect the internal indicators of Nginx and expose them to Prometheus

2 the installation

[root@VM-10-48-centos logs]# wget -O /etc/yum.repos.d/openresty.repo https://openresty.org/package/centos/openresty.repo  [root@VM-10-48-centos logs]# yum install -y openresty [root@VM-10-48-centos logs]# systemctl start openresty.serviceCopy the code

3 the configuration

downloadnginx-lua-prometheus

[root@VM-10-48-centos logs]# cd /usr/local/openresty/lualib/
[root@VM-10-48-centos logs]# git clone https://hub.fastgit.org/knyar/nginx-lua-prometheus.git
#Make sure the following three files exist
[root@VM-10-48-centos lualib]# ls -l nginx-lua-prometheus/
total 40
-rw-r--r-- 1 root root  3371 May 28 13:56 prometheus_keys.lua
-rw-r--r-- 1 root root 28754 May 28 13:56 prometheus.lua
-rw-r--r-- 1 root root  2212 May 28 13:56 prometheus_resty_counter.lua
Copy the code

Configuration openresty

[root@VM-10-48-centos openresty]# vim /usr/local/openresty/nginx/conf/nginx.conf
include /usr/local/openresty/nginx/conf.d/*.conf;
Copy the code
[root@VM-10-48-centos openresty]# vim /usr/local/openresty/nginx/conf.d/prometheus.conf lua_shared_dict prometheus_metrics 10M; lua_package_path "/usr/local/openresty/lualib/nginx-lua-prometheus/? .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; 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() } } }Copy the code

Restart openresty

[root@VM-10-48-centos prometheus]# systemctl restart openresty.service 
Copy the code

Test access

[root@VM-10-48-centos openresty]# 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"} 1
# HELP nginx_http_request_duration_seconds HTTP request latency
# TYPE nginx_http_request_duration_seconds histogramNginx_http_request_duration_seconds_bucket {host = ", "le =" 0.005 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.01 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.02 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.03 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.05 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.075 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.1 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.2 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.3 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.4 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.5 "} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 0.75 "} 6 nginx_http_request_duration_seconds_bucket{host="",le="1"} 6 Nginx_http_request_duration_seconds_bucket {host = ", "le =" 1.5 "} 6 nginx_http_request_duration_seconds_bucket{host="",le="2"} 6 nginx_http_request_duration_seconds_bucket{host="",le="3"} 6 nginx_http_request_duration_seconds_bucket{host="",le="4"} 6 nginx_http_request_duration_seconds_bucket{host="",le="5"} 6 nginx_http_request_duration_seconds_bucket{host="",le="10"}  6 nginx_http_request_duration_seconds_bucket{host="",le="+Inf"} 6 nginx_http_request_duration_seconds_count{host=""} 6 nginx_http_request_duration_seconds_sum{host=""} 0# HELP nginx_http_requests_total Number of HTTP requests
# TYPE nginx_http_requests_total counter
nginx_http_requests_total{host="",status="200"} 6
# HELP nginx_metric_errors_total Number of nginx-lua-prometheus errors
# TYPE nginx_metric_errors_total counter
nginx_metric_errors_total 0
Copy the code

Prometheus 4 configuration

[root@VM-10-48-centos prometheus]# cat prometheus.yml
  ...
  - job_name: 'Nginx'
    static_configs:
      - targets: ['10.1.10.48:9145']
Copy the code