Skywalking对Nginx进行监控
Posted 恒奇恒毅
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Skywalking对Nginx进行监控相关的知识,希望对你有一定的参考价值。
本文基于Skywalking-8.7.0和nginx-1.20.1
书接上回,Skywalking已经能对Java应用非常轻松地接入监控,典型应用中,系统都是在负载均衡器后端跑JavaWeb应用,而最常用的负载均衡器就是Nginx。如果我们想把也纳入监控,该怎么办呢?幸运的是,Skywalking已经提供了Nginx的探针收集器。官网地址:https://github.com/apache/skywalking-nginx-lua。集成过程中发现,官网的例子只支持了OpenResty,对于OpenResty来说,已经集成了LuaJIT和lua-nginx-module等常用模块,按照官网的例子添加Skywalking相关的配置即可生效。但是配置给Nginx启动的时候就会报很多错误。我们可能并不想为了接入Skywalking就去换一个负载均衡器(Nginx换成OpenResty),如此,我们必须解决Skywalking与Nginx的集成问题。
备注:Openresty是基于Nginx+Lua的高性能Web开发平台,解决了Nginx不易编程的短板。
- Nginx的升级:
Nginx的agent插件是基于Lua来编写的,所以需要Nginx增加对Lua的支持,lua-nginx-module刚好提供了此功能。而lua-nginx-module依赖于LuaJIT,所以首先我们需要安装LuaJIT在环境中,最好选择2.1版本。
对Nginx来说,需要自己编译必须的模块。依赖以下两个模块:
lua-nginx-module,版本使用lua-nginx-module-0.10.21rc1
ngx_devel_kit,版本使用ngx_devel_kit-0.3.1
编译nginx参数
configure arguments: --add-module=/path/to/ngx_devel_kit-0.3.1 --add-module=/path/to/lua-nginx-module-0.10.21rc1 --with-ld-opt=-Wl,-rpath,/usr/local/LuaJIT/lib
下面针对skywalking-nginx-lua-0.3.0和0.3.0以上版本分别说明。
- skywalking-nginx-lua-0.3.0
经过测试,skywalking-nginx-lua-0.3.0需要以下lua相关模块
lua-resty-core https://github.com/openresty/lua-resty-core
lua-resty-lrucache https://github.com/openresty/lua-resty-lrucache
lua-cjson https://github.com/openresty/lua-cjson
依赖的lua模块如下:
lua_package_path "/path/to/lua-resty-core/lua-resty-core-master/lib/?.lua;/path/to/lua-resty-lrucache-0.11/lib/?.lua;/path/to/skywalking-nginx-lua-0.3.0/lib/?.lua;;";
lua-cjson在make && make install过程中,有个需要注意的地方:
修改Makefile中一个路径
LUA_INCLUDE_DIR ?= /usr/local/LuaJIT/include/luajit-2.0
参考:https://blog.csdn.net/ymeputer/article/details/50146143
- skywalking-nginx-lua-0.3.0以上版本
skywalking-nginx-lua-0.3.0以上版本需要添加tablepool的支持,但貌似不需要cjson
lua-resty-core https://github.com/openresty/lua-resty-core
lua-resty-lrucache https://github.com/openresty/lua-resty-lrucache
lua-tablepool https://github.com/openresty/lua-tablepool
lua_package_path "/path/to/lua-resty-core/lua-resty-core-master/lib/?.lua;/path/to/lua-resty-lrucache-0.11/lib/?.lua;/path/to/lua-tablepool-master/lib/?.lua;/path/to/skywalking-nginx-lua-master/lib/?.lua;;";
其中tablepool根据其官方文档介绍其中的两个APItable.new and table.clear
需要LuaJIT2.1版本,在skywalking-nginx-lua的文档中有一段话,说可以通过require("skywalking.util").disable_tablepool()
禁用tablepool
在启动nginx的时候,会有个提示,提示安装OpenResty自己的LuaJIT版本
detected a LuaJIT version which is not OpenResty's; many optimizations will be disabled and performance will be compromised (see https://github.com/openresty/luajit2 for OpenResty's LuaJIT or, even better, consider using the OpenResty releases from https://openresty.org/en/download.html)
下面给出调试成功的配置
http
lua_package_path "/path/to/lua-resty-core/lua-resty-core-master/lib/?.lua;/path/to/lua-resty-lrucache-0.11/lib/?.lua;/path/to/lua-tablepool-master/lib/?.lua;/path/to/skywalking-nginx-lua-master/lib/?.lua;;";
# Buffer represents the register inform and the queue of the finished segment
lua_shared_dict tracing_buffer 100m;
# Init is the timer setter and keeper
# Setup an infinite loop timer to do register and trace report.
init_worker_by_lua_block
local metadata_buffer = ngx.shared.tracing_buffer
-- Set service name
metadata_buffer:set('serviceName', 'User Service Name')
-- Instance means the number of Nginx deployment, does not mean the worker instances
metadata_buffer:set('serviceInstanceName', 'User Service Instance Name')
-- type 'boolean', mark the entrySpan include host/domain
metadata_buffer:set('includeHostInEntrySpan', false)
-- set random seed
require("skywalking.util").set_randomseed()
require("skywalking.client"):startBackendTimer("http://127.0.0.1:12800")
-- If there is a bug of this `tablepool` implementation, we can
-- disable it in this way
-- require("skywalking.util").disable_tablepool()
skywalking_tracer = require("skywalking.tracer")
server
listen 8090;
location /ingress
default_type text/html;
rewrite_by_lua_block
------------------------------------------------------
-- NOTICE, this should be changed manually
-- This variable represents the upstream logic address
-- Please set them as service logic name or DNS name
--
-- Currently, we can not have the upstream real network address
------------------------------------------------------
skywalking_tracer:start("upstream service")
-- If you want correlation custom data to the downstream service
-- skywalking_tracer:start("upstream service", custom = "custom_value")
-- Target upstream service
proxy_pass http://127.0.0.1:8080/backend;
body_filter_by_lua_block
if ngx.arg[2] then
skywalking_tracer:finish()
end
log_by_lua_block
skywalking_tracer:prepareForReport()
参考:https://www.cnblogs.com/kebibuluan/p/14440228.html
以上是关于Skywalking对Nginx进行监控的主要内容,如果未能解决你的问题,请参考以下文章