安装

服务端性能监控最佳实践(一)—— 炫酷的Nginx请求分析监控

其中涉及的lua脚本等,github地址
不过其中的代码有问题,我fork了一份,修改后传到新地址了,具体问题见后续分析

nginx+OpenResty

Nginx 平滑升级至 OpenResty
Nginx的启动、停止与重启

bug

安装后,nginx日志报错

2021/01/23 14:38:21 [error] 85214#0: *13184 access forbidden by rule, client: 10.10.10.10, server: localhost, request: "HEAD / HTTP/1.0"
2021/01/23 14:38:21 [error] 85214#0: *13184 [lua] counter.lua:66: log(): latency=0,status=403,endpoint=nil,fullurl=nil while logging request, client: 10.10.10.10, server: localhost, request: "HEAD / HTTP/1.0"
2021/01/23 14:38:21 [error] 85214#0: *13184 [lua] prometheus.lua:317: log_error(): Wrong number of labels for nginx_http_request_duration_seconds. Expected 6, got 4 while logging request, client: 10.10.10.10, server: localhost, request: "HEAD / HTTP/1.0"

看起来是lua脚本有问题,debug看看

Mac+Idea+lua

Emmylua

IDEA+EmmyLua Lua开发环境搭建
在 mac osx 下进行 ulua 远程调试
其中如果电脑是macpro m1 (silicon架构) 架构可以加
-arch arm64
最终没搞定

Lua

改为用lua插件


bug fix

单步调试搞定后,发现counter.lua有问题
原代码

local pcall = pcall
local ngx = ngx
local ngx_log = ngx.log
local ngx_err = ngx.ERR
local _M = {}
function _M.init()uris = ngx.shared.uri_by_hostglobal_set = ngx.shared.global_setglobal_set:set("initted", false)global_set:set("looped", false)prometheus = require("prometheus").init("prometheus_metrics") metric_latency = prometheus:histogram("nginx_http_request_duration_seconds", "HTTP request latency status", {"host", "status", "scheme", "method", "endpoint", "fullurl"})
end
local function split(inputstr, sep)if sep == nil thensep = "%s"endlocal t={} ; i=1for str in string.gmatch(inputstr, "([^"..sep.."]+)") dot[i] = stri = i + 1endreturn t
end
local function parse_fullurl(request_uri)result_table = {}if string.find(request_uri, "%.") ~= nil thenreturn nilendparts = split(request_uri, "/")if table.getn(parts) == 1 thenreturn nilendfor j=1, #parts doif(j == 1) thenendpoint = "/"..parts[j]fullurl = "/"..parts[j]elseif(j <= 5) thenif tonumber(parts[j]) ~= nil thenbreakendfullurl = fullurl.."/"..parts[j]elsebreakendendresult_table["endpoint"] = endpointresult_table["fullurl"] = fullurlreturn result_table
end
function _M.log()local request_host = ngx.var.hostlocal request_uri = ngx.unescape_uri(ngx.var.uri)local request_status = ngx.var.statuslocal request_scheme = ngx.var.schemelocal request_method = ngx.var.request_methodlocal remote_ip = ngx.var.remote_addrlocal ngx_sent = ngx.var.body_bytes_sentlocal latency = ngx.var.upstream_response_time or 0result_table = parse_fullurl(request_uri)if result_table == nil thenreturnendngx_log(ngx_err,"latency=", tonumber(latency), ",status=", request_status, ",endpoint=", result_table["endpoint"], ",fullurl=", result_table["fullurl"])metric_latency:observe(tonumber(latency), {request_host, request_status, request_scheme, request_method, result_table["endpoint"], result_table["fullurl"]})
end
return _M

其中有2个问题

if string.find(request_uri, “%.”) ~= nil then
改为
if string.find(request_uri, “%.”) == nil then
~=在lua里表示不等于。lua的find会返回2个值,这里应该是url查找任意字符,从代码上看逻辑是检查字符串长度是否为0
另外一个
table.getn(parts) == 1
lua升级到5.1后,不再支持getn,改为 #
#parts == 1

正确脚本

counter.lua

local pcall = pcall
local ngx = ngx
local ngx_log = ngx.log
local ngx_err = ngx.ERR
local _M = {}
function _M.init()uris = ngx.shared.uri_by_hostglobal_set = ngx.shared.global_setglobal_set:set("initted", false)global_set:set("looped", false)prometheus = require("prometheus").init("prometheus_metrics") metric_latency = prometheus:histogram("nginx_http_request_duration_seconds", "HTTP request latency status", {"host", "status", "scheme", "method", "endpoint", "fullurl"})
end
local function split(inputstr, sep)if sep == nil thensep = "%s"endlocal t={} ; i=1for str in string.gmatch(inputstr, "([^"..sep.."]+)") dot[i] = stri = i + 1endreturn t
end
local function parse_fullurl(request_uri)result_table = {}if string.find(request_uri, "%.") == nil thenreturn nilendparts = split(request_uri, "/")if #parts == 1 thenreturn nilendfor j=1, #parts doif(j == 1) thenendpoint = "/"..parts[j]fullurl = "/"..parts[j]elseif(j <= 5) thenif tonumber(parts[j]) ~= nil thenbreakendfullurl = fullurl.."/"..parts[j]elsebreakendendresult_table["endpoint"] = endpointresult_table["fullurl"] = fullurlreturn result_table
end
function _M.log()local request_host = ngx.var.hostlocal request_uri = ngx.unescape_uri(ngx.var.uri)local request_status = ngx.var.statuslocal request_scheme = ngx.var.schemelocal request_method = ngx.var.request_methodlocal remote_ip = ngx.var.remote_addrlocal ngx_sent = ngx.var.body_bytes_sentlocal latency = ngx.var.upstream_response_time or 0result_table = parse_fullurl(request_uri)if result_table == nil thenreturnendngx_log(ngx_err,"latency=", tonumber(latency), ",status=", request_status, ",endpoint=", result_table["endpoint"], ",fullurl=", result_table["fullurl"])metric_latency:observe(tonumber(latency), {request_host, request_status, request_scheme, request_method, result_table["endpoint"], result_table["fullurl"]})
end
return _M

请求http://xxx.xxxx.xxx.xxx:9145/metrics
正常返回了

配置prometheus + Grafana

按照文章配置好后,发现页面endpoint, host等变量有数据,但是图表没有东西

找个panel点击编辑后

发现这里的 nginx_http_request_duration_seconds:qps_by_instance_host_endpoint_fullurl_2XX ,写的job,但是grafana里没有找到对应的定义,prometheus里也没有。
google了一下,发现
GIt地址
里定义了

按照这个prometheus.yml里的rule的定义,拷贝文件到服务器

再看grafana有图了

改进

抓回数据后,发现有对静态资源的请求也在日志范围内,如果想去掉静态请求,可以修改counter.lua文件,例如

    if string.find(request_uri, ".html") ~= nil thenreturn nilendif string.find(request_uri, ".js") ~= nil thenreturn nilendif string.find(request_uri, ".css") ~= nil thenreturn nilendif string.find(request_uri, ".png") ~= nil thenreturn nilendif string.find(request_uri, "/static/") ~= nil thenreturn nilend

Grafana(10445) +Pormetheus + Nginx 监控 Http API 2xx 3xx 4xx相关推荐

  1. web应用F12查看报错(前后端bug判断、2XX/3XX/4XX/5XX常见状态码解析)

    chrom浏览器为例 (1)打开开发者工具,在浏览器菜单栏选择工具-开发者工具,快捷键是F12 (2)打开之后切换到Network页签,操作就可以看到请求响应 (3)再选择响应的链接,切换到Previ ...

  2. HTTP请求错误 2xx 3xx 4xx 5xx

    2xx (成功) 表示成功处理了请求的状态代码. 代码 说明 200 (成功) 服务器已成功处理了请求.通常,这表示服务器提供了请求的网页. 201 (已创建) 请求成功并且服务器创建了新的资源. 2 ...

  3. 浏览器状态响应码详解 1XX/2XX/3XX/4XX/5XX

    Http通信协议返回状态码详解 1. 1XX(临时响应) 表示临时响应并需要请求者继续执行操作的状态码. 1xx(临时响应) 100(继续) 请求者应当继续提出请求.服务器返回此代码表示已收到请求的第 ...

  4. (四) prometheus + grafana + alertmanager 配置Kafka监控

    安装请看https://blog.51cto.com/liuqs/2027365 ,最好是对应的版本组件,否则可能会有差别. (一)prometheus + grafana + alertmanage ...

  5. 基于InfluxDB+Grafana打造大数据监控利器--转

    这是一个大数据爆发的时代.面对信息的激流.多元化数据的涌现,我们在获取.存储.传输.理解.分析.应用.维护大数据时,无疑需要一种便捷的信息交流通道,以便快速.有效.准确地理解和驾驭这个过程.本文将通过 ...

  6. 使用Prometheus+grafana打造高逼格监控平台

    前言: 笔者看来, 监控不应该只是监控,除了及时有效的报警,更应该"好看",因为视觉上的感受更能给我们直观的感受,更能从绚丽的走势中发现异常, 如果你觉得监控就应该像老牌监控nag ...

  7. grafana+zabbix 部署分布式监控系统

    环境 :Ubuntu 16.04 1.安装grafana $ wget https://s3-us-west-2.amazonaws.com/grafana-releases/release/graf ...

  8. 视频教程-Prometheus+Grafana搭建全方位的监控告警系统-Linux

    Prometheus+Grafana搭建全方位的监控告警系统 高级运维工程师.资深DevOps工程师,精通kubernetes容器编排工具,熟练使用linux操作系统,多年线上线下教学经验 韩先超 ¥ ...

  9. 基于InfluxDB+Grafana打造大数据监控利器

    转载:https://www.cnblogs.com/davidwang456/p/7795263.html 这是一个大数据爆发的时代.面对信息的激流.多元化数据的涌现,我们在获取.存储.传输.理解. ...

最新文章

  1. 详解最大似然估计(MLE)、最大后验概率估计(MAP),以及贝叶斯公式的理解...
  2. WinCE启动次数的记录
  3. 正确的线程中止-标志位
  4. Java面向对象(15)--static关键字静态理解与使用
  5. LeetCode 1711. 大餐计数(map计数 + 二分查找)
  6. 北妈每日一题:JS从无序乱码找我要的数字!
  7. MySQL基础总结,认真看完这篇就够了!!!
  8. 聊聊redisson的分布式锁
  9. mybatis13--2级缓存
  10. 【MacOs系统-M2安装2022新版AWVS渗透工具】-保姆级安装教程
  11. Charles 弱网测试
  12. 史上最后一位数学全才——庞加莱
  13. 树莓派 linux安装中文语言包6,树莓派安装XBMC并让其支持中文,
  14. python面向对象编程指南 豆瓣_Python面向对象编程
  15. 表贴电阻尺寸与什么有关_贴片电阻简介:功率大小与尺寸对应表
  16. 在树莓派上安装配置远程摄像头监控motion
  17. Nosql初探(voldemort)
  18. Android添加UserAgent
  19. 北京19家A类定点医疗机构名单以及分布图
  20. prometeus, grafana部署以及监控mysql

热门文章

  1. itext html to pdf设置边距,iText:设置边距是否有效?
  2. GIS海量数据的存储和读取
  3. Already included file name .......
  4. 实对称阵的谱半径是连续函数
  5. 蓝桥杯真题 19省2-年号字串 小明用字母 A 对应数字 1, B 对应 2,以此类推,用 Z 对应 26。对于 27以上的数字,小明用两位或更长位的字符串来对应,例如 AA 对应 27, AB
  6. 写给计算机老师的一封信800,写给老师的一封信800字
  7. JS特效——鼠标点击特效
  8. 小米笔记本适合计算机专业吗,小米电脑笔记本好吗应该选哪个,小米入手使用感受...
  9. 外校保研北大计算机,北大2018年本校保研率超53% 外校生多来自双一流名校
  10. 摩拜ofo均取消月卡优惠;陌陌7.35亿美元收购探探;京东腾讯入股步步高丨价值早报