- Http:(这里我加入了prometheus的监控)
lua_package_path "/app/soft/tengine/conf/skywalking-nginx-lua/lib/skywalking/?.lua;;";
lua_shared_dict tracing_buffer 100m;
include /app/soft/tengine/lua/prometheus/prometheus.conf;
- 初始化后台信息/app/soft/tengine/lua/prometheus/prometheus.conf:(官网有问题,我的后端是7.0.0,用的v2版本)
#nginx启动就连接链路后端
init_worker_by_lua_block {
function getTabBySplitString(str, char)
if (str == "" or not str or char == "" or not char) then
return
end
local subStrTab = {}
while(true) do
local pos = string.find(str, char)
if (not pos) then
subStrTab[#subStrTab + 1] = str
break
end
local subStr = string.sub(str, 1, pos-1)
subStrTab[#subStrTab + 1] = subStr
local endPos = string.len(str)
str = str.sub(str, pos+1, endPos)
end
return subStrTab
end
function getInfo(str)
local info_tab = {}
for lin in io.lines(str) do
local KeyValue = getTabBySplitString(lin,"=")
info_tab[KeyValue[1]]=KeyValue[2]
end
return info_tab
end
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"})
local metadata_buffer = ngx.shared.tracing_buffer
local info_tab = getInfo("/app/soft/tengine/lua/powerapm/info.txt")
metadata_buffer:set('serviceName', info_tab['serviceName'])
-- Instance means the number of Nginx deloyment, does not mean the worker instances
metadata_buffer:set('serviceInstanceName', info_tab['serviceInstanceName'])
require("client"):startBackendTimer(info_tab['clientUrl'])
}
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})
}
- LUA脚本获取配置信息:/app/soft/tengine/lua/prometheus/prometheus.conf配置文件:
serviceName=yhh_tengine:777
serviceInstanceName=ip
clientUrl=http://ip:12800
- server:
include /app/soft/tengine/lua/powerapm/powerapm.conf;(链路注册,必须存在)
#模拟agent进行链路注册采集
# ------------------------------------------------------
# -- Mock OAP server to provide register and trace collection
# ------------------------------------------------------
location /v2/service/register {
default_type text/html;
lua_need_request_body on;
content_by_lua_block {
local cjson = require('cjson')
ngx.log(ngx.DEBUG, 'Service register request = ', ngx.req.get_body_data())
local param = cjson.decode(ngx.req.get_body_data())
local registeredInfo = {}
registeredInfo[1] = {key=param.services[1].serviceName, value=1}
ngx.say(cjson.encode(registeredInfo))
}
}
location /v2/instance/register {
default_type text/html;
lua_need_request_body on;
content_by_lua_block {
local cjson = require('cjson')
ngx.log(ngx.DEBUG, 'Service instance register request = ', ngx.req.get_body_data())
local param = cjson.decode(ngx.req.get_body_data())
local registeredInfo = {}
registeredInfo[1] = {key=param.instances[1].instanceUUID, value=1}
ngx.say(cjson.encode(registeredInfo))
}
}
location /v2/instance/heartbeat {
default_type text/html;
lua_need_request_body on;
content_by_lua_block {
local cjson = require('cjson')
ngx.log(ngx.DEBUG, 'Service instance ping request = ', ngx.req.get_body_data())
}
}
location /v2/segments {
default_type text/html;
lua_need_request_body on;
content_by_lua_block {
local cjson = require('cjson')
ngx.log(ngx.DEBUG, 'Received segment = ', ngx.req.get_body_data())
}
}
- location:(后期有优化)
#proxy_pass前后都一样,增强
rewrite_by_lua_block {
require("tracer"):start(ngx.var.request_uri)
}
proxy_pass http://power-apm-manager;
body_filter_by_lua_block {
if ngx.arg[2] then
require("tracer"):finish()
end
}
log_by_lua_block {
require("tracer"):prepareForReport()
}
优化`
location /venus {
rewrite_by_lua_file /app/soft/tengine/lua/powerapm/rewrite_by_lua_file.lua;
proxy_pass http://venus-20005;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Connection "";
proxy_http_version 1.1;
body_filter_by_lua_file /app/soft/tengine/lua/powerapm/body_filter_by_lua_file.lua;
log_by_lua_file /app/soft/tengine/lua/powerapm/log_by_lua_file.lua;
}