您的位置:首页 > 房产 > 建筑 > 深圳广告公司转让_网易企业邮箱收件服务器主机名_深圳网站优化_企业网络营销方案策划

深圳广告公司转让_网易企业邮箱收件服务器主机名_深圳网站优化_企业网络营销方案策划

2025/1/8 6:46:15 来源:https://blog.csdn.net/weixin_48502062/article/details/144907897  浏览:    关键词:深圳广告公司转让_网易企业邮箱收件服务器主机名_深圳网站优化_企业网络营销方案策划
深圳广告公司转让_网易企业邮箱收件服务器主机名_深圳网站优化_企业网络营销方案策划

本节重点介绍 :

  • 编写lua脚本做promql的检查替换
  • nginx拦截prometheus查询请求使用lua处理

编写lua脚本做promql的检查替换

获取请求参数

function replace_work()--Nginx服务器中使用lua获取get或post参数local request_method = ngx.var.request_method;local args = {}--获取参数的值if "GET" == request_method thenargs = ngx.req.get_uri_args();elseif "POST" == request_method thenngx.req.read_body();args = ngx.req.get_post_args();endlocal q_query = args["query"];local q_start = args["start"];local q_end = args["end"];local q_step = args["step"];
end

根据查询的promql算m5d

    local md5_str = get_str_md5(q_query)if md5_str == null thenreturnend
function get_str_md5(input_s)local resty_md5 = require "resty.md5"local md5 = resty_md5:new()if not md5 thenngx.log(ngx.ERR, "failed to create md5 object")returnendlocal ok = md5:update(input_s)if not ok thenngx.log(ngx.ERR, "failed to add data")returnendlocal digest = md5:final()local str = require "resty.string"local md5_str = str.to_hex(digest)return md5_str
end

根据md5去redis中query

    local redis_query_key = "hke:heavy_expr:" .. md5_str--ngx.log(ngx.ERR, "redis_query_key: ",redis_query_key)local redis_get_res = redis_get(redis_query_key)if redis_get_res == true thenq_query = redis_query_keyend
function redis_get(key)-- start of redislocal redis = require "resty.redis"local red = redis:new()--red:set_timeouts(1000, 1000, 1000)local ok, conn_err = red:connect("localhost", 6379)if not ok thenngx.log(ngx.ERR, "[redis]failed to connect redis server:", conn_err)return falseendlocal res, get_err = red:get(key)if get_err thenngx.log(ngx.ERR, "[redis]failed to get value by key: ", key, "err:", get_err)return falseendred:set_keepalive(30000, 1000)if res ~= ngx.null thenngx.log(ngx.INFO, "[redis]success  get value by key: ", key, "value: ", res)return trueelsereturn falseend-- end of  redis
end

如果redis中有结果,就替换查询语句为聚合后的

    if redis_get_res == true thenq_query = redis_query_keyendlocal new_args = {}new_args["query"] = q_querynew_args["start"] = q_startnew_args["end"] = q_endnew_args["step"] = q_stepngx.req.set_uri_args(new_args)--ngx.req.set_uri_args("end=" .. q_end)--local arg = ngx.req.get_uri_args()--for k, v in pairs(arg) do--    ngx.say("[GET ] key:", k, " v:", v)--end

完整的 prome_redirect.lua

function get_str_md5(input_s)local resty_md5 = require "resty.md5"local md5 = resty_md5:new()if not md5 thenngx.log(ngx.ERR, "failed to create md5 object")returnendlocal ok = md5:update(input_s)if not ok thenngx.log(ngx.ERR, "failed to add data")returnendlocal digest = md5:final()local str = require "resty.string"local md5_str = str.to_hex(digest)return md5_str
endfunction redis_get(key)-- start of redislocal redis = require "resty.redis"local red = redis:new()--red:set_timeouts(1000, 1000, 1000)local ok, conn_err = red:connect("localhost", 6379)if not ok thenngx.log(ngx.ERR, "[redis]failed to connect redis server:", conn_err)return falseendlocal res, get_err = red:get(key)if get_err thenngx.log(ngx.ERR, "[redis]failed to get value by key: ", key, "err:", get_err)return falseendred:set_keepalive(30000, 1000)if res ~= ngx.null thenngx.log(ngx.INFO, "[redis]success  get value by key: ", key, "value: ", res)return trueelsereturn falseend-- end of  redis
endfunction replace_work()--Nginx服务器中使用lua获取get或post参数local request_method = ngx.var.request_method;local args = {}--获取参数的值if "GET" == request_method thenargs = ngx.req.get_uri_args();elseif "POST" == request_method thenngx.req.read_body();args = ngx.req.get_post_args();endlocal q_query = args["query"];local q_start = args["start"];local q_end = args["end"];local q_step = args["step"];local md5_str = get_str_md5(q_query)if md5_str == null thenreturnendlocal redis_query_key = "hke:heavy_expr:" .. md5_str--ngx.log(ngx.ERR, "redis_query_key: ",redis_query_key)local redis_get_res = redis_get(redis_query_key)if redis_get_res == true thenq_query = redis_query_keyendlocal new_args = {}new_args["query"] = q_querynew_args["start"] = q_startnew_args["end"] = q_endnew_args["step"] = q_stepngx.req.set_uri_args(new_args)--ngx.req.set_uri_args("end=" .. q_end)--local arg = ngx.req.get_uri_args()--for k, v in pairs(arg) do--    ngx.say("[GET ] key:", k, " v:", v)--endendreturn replace_work();

nginx拦截prometheus查询请求使用lua处理

  • ngx_prome_redirect.conf
# 真实prometheus后端,使用前请修改
upstream real_prometheus {server 1.1.1.1:9090;server 2.2.2.2:9090;}server{listen 9992;server_name _;location / {  proxy_set_header Host $host:$server_port;proxy_pass http://real_prometheus;} location /api/v1/query_range { access_by_lua_file /usr/local/openresty/nginx/lua_files/prome_redirect.lua;proxy_pass http://real_prometheus;}}
  • grafana发来的请求经过nginx,使用lua脚本处理
  • 然后转发到真实的prometheus 查询

本节重点总结 :

  • 编写lua脚本做promql的检查替换
  • nginx拦截prometheus查询请求使用lua处理

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com