Skip to content

Commit

Permalink
webhook上报支持
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Oct 7, 2023
1 parent ffd8ea4 commit 0a3ed65
Show file tree
Hide file tree
Showing 15 changed files with 80 additions and 342 deletions.
9 changes: 6 additions & 3 deletions bin/share.conf
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,15 @@ set_env("QUANTA_DB_CACHE_MAX", "4096")
--每帧落地最大数量
set_env("QUANTA_DB_CACHE_FLUSH", "10")

--webhook日志等级
--webhook设置
-----------------------------------------------------
set_env("QUANTA_WEBHOOK_LVL", "6")
--log: 日志采集
--http:http发送
set_env("QUANTA_WEBHOOK_MODE", "log")
--webhook地址设置
--set_env("QUANTA_LARK_URL", "https://open.feishu.cn/open-apis/bot/v2/hook/9a6565bf-51b4-48ab-80f0-64caf4d58ab8")
--set_env("QUANTA_DING_URL", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a4c64781-adad-4ddc-a615-6bc232ce71ef")
--set_env("QUANTA_WECHAT_URL", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ee6d97c5-477d-436f-83db-dd5361a5e8bd")
set_env("QUANTA_WECHAT_URL", "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=d7cd918c-f608-4762-87af-bb8c7fc51074")

--graylog配置
-----------------------------------------------------
Expand Down
97 changes: 0 additions & 97 deletions script/agent/proxy_agent.lua

This file was deleted.

23 changes: 9 additions & 14 deletions script/basic/logger.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ local LOG_LEVEL = log.LOG_LEVEL

local title = quanta.title
local monitors = _ENV.monitors or {}
local dispatch = false

logger = {}
logfeature = {}
Expand All @@ -36,8 +35,8 @@ function logger.daemon(daemon)
log.daemon(daemon)
end

function logger.add_monitor(monitor, lvl)
monitors[monitor] = lvl
function logger.add_monitor(monitor)
monitors[monitor] = true
end

function logger.remove_monitor(monitor)
Expand Down Expand Up @@ -73,7 +72,11 @@ local function logger_output(flag, feature, lvl, lvl_name, fmt, ...)
lprint(LOG_LEVEL.WARN, 0, title, feature, wfmt, lvl_name, msg, info.short_src, info.linedefined)
return
end
return msg
if msg then
for monitor in pairs(monitors) do
monitor:dispatch_log(msg, lvl_name)
end
end
end

local LOG_LEVEL_OPTIONS = {
Expand All @@ -84,19 +87,11 @@ local LOG_LEVEL_OPTIONS = {
[LOG_LEVEL.FATAL] = { "fatal", 0x01 },
[LOG_LEVEL.DUMP] = { "dump", 0x01 | 0x02 },
}

for lvl, conf in pairs(LOG_LEVEL_OPTIONS) do
local lvl_name, flag = tunpack(conf)
logger[lvl_name] = function(fmt, ...)
local msg = logger_output(flag, "", lvl, lvl_name, fmt, ...)
if msg and (not dispatch) then
dispatch = true
pcall(function ()
for monitor in pairs(monitors) do
monitor:dispatch_log(msg, lvl_name)
end
end)
dispatch = false
end
logger_output(flag, "", lvl, lvl_name, fmt, ...)
end
end

Expand Down
4 changes: 2 additions & 2 deletions script/constant.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
local KernCode = enum("KernCode", 0)
KernCode.SUCCESS = 0 --成功
KernCode.FAILED = 1 --系统错误,请重试
KernCode.PARAM_ERROR = 2 --业务参数错误
KernCode.TOO_FAST = 3 --操作太快
KernCode.TOO_FAST = 2 --操作太快
KernCode.PARAM_ERROR = 3 --业务参数错误
KernCode.UPHOLD = 4 --服务维护
KernCode.RPC_FAILED = 5 --RPC调用失败
KernCode.OPERATOR_SELF = 6 --不能对自己操作
Expand Down
62 changes: 46 additions & 16 deletions script/driver/webhook.lua
Original file line number Diff line number Diff line change
@@ -1,51 +1,81 @@
--webhook.lua
import("network/http_client.lua")

local jencode = json.encode
local sformat = string.format

local LIMIT_COUNT = 3 -- 周期内最大次数
local WEBPATH = environ.get("QUANTA_WEBHOOK_PATH", "./webhooks/")
local log_dump = logfeature.dump("webhooks", WEBPATH, true)

local thread_mgr = quanta.get("thread_mgr")
local http_client = quanta.get("http_client")

local HOST_IP = environ.get("QUANTA_HOST_IP")
local HOUR_S = quanta.enum("PeriodTime", "HOUR_S")

local LIMIT_COUNT = 3 -- 周期内最大次数

local Webhook = singleton()
local prop = property(Webhook)
prop:reader("mode", nil) --mode
prop:reader("title", "") --title
prop:reader("hooks", {}) --webhook通知接口
prop:reader("notify_limit", {}) --控制同样消息的发送频率

function Webhook:__init()
self.hooks.lark_log = environ.get("QUANTA_LARK_URL")
self.hooks.ding_log = environ.get("QUANTA_DING_URL")
self.hooks.wechat_log = environ.get("QUANTA_WECHAT_URL")
local mode = environ.get("QUANTA_WEBHOOK_MODE")
if mode then
--添加webhook功能
self.mode = mode
logger.add_monitor(self)
self.title = sformat("%s | %s", HOST_IP, quanta.service_name)
--初始化hooks
self.hooks.lark_log = environ.get("QUANTA_LARK_URL")
self.hooks.ding_log = environ.get("QUANTA_DING_URL")
self.hooks.wechat_log = environ.get("QUANTA_WECHAT_URL")
end
end

--hook_log
function Webhook:hook_log(url, body)
if self.mode == "log" then
log_dump(jencode(body))
return
end
--http输出
thread_mgr:entry(url, function()
http_client:call_post(url, body)
end)
end

--飞书
function Webhook:lark_log(url, title, context)
local text = sformat("%s\n %s", title, context)
function Webhook:lark_log(url, context)
local text = sformat("%s\n %s", self.title, context)
local body = { msg_type = "text", content = { text = text } }
http_client:call_post(url, body)
self:hook_log(url, body)
end

--企业微信
--at_members: 成员列表,数组,如 at_members = {"wangqing", "@all"}
--at_mobiles: 手机号列表,数组, 如 at_mobiles = {"156xxxx8827", "@all"}
function Webhook:wechat_log(url, title, context, at_mobiles, at_members)
local text = sformat("%s\n %s", title, context)
function Webhook:wechat_log(url, context, at_mobiles, at_members)
local text = sformat("%s\n %s", self.title, context)
local body = { msgtype = "text", text = { content = text, mentioned_list = at_members, mentioned_mobile_list = at_mobiles } }
http_client:call_post(url, body)
self:hook_log(url, body)
end

--钉钉
--at_all: 是否群at,如 at_all = false/false
--at_mobiles: 手机号列表,数组, 如 at_mobiles = {"189xxxx8325", "156xxxx8827"}
function Webhook:ding_log(url, title, context, at_mobiles, at_all)
local text = sformat("%s\n %s", title, context)
function Webhook:ding_log(url, context, at_mobiles, at_all)
local text = sformat("%s\n %s", self.title, context)
local body = { msgtype = "text", text = { content = text }, at = { atMobiles = at_mobiles, isAtAll = at_all } }
http_client:call_post(url, body)
self:hook_log(url, body)
end

function Webhook:notify(title, content, ...)
if next(self.hooks) then
--dispatch_log
function Webhook:dispatch_log(content)
if self.mode then
local now = quanta.now
local notify = self.notify_limit[content]
if not notify then
Expand All @@ -60,7 +90,7 @@ function Webhook:notify(title, content, ...)
end
notify.count = notify.count + 1
for hook_api, url in pairs(self.hooks) do
self[hook_api](self, url, title, content, ...)
self[hook_api](self, url, content)
end
end
end
Expand Down
9 changes: 2 additions & 7 deletions script/feature/worker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ local function init_core()
import("kernel/thread_mgr.lua")
import("kernel/event_mgr.lua")
import("kernel/config_mgr.lua")
import("kernel/perfeval_mgr.lua")
end

--初始化网络
Expand All @@ -39,12 +40,6 @@ local function init_network()
quanta.socket_mgr = socket_mgr
end

--初始化统计
local function init_statis()
import("agent/proxy_agent.lua")
import("kernel/perfeval_mgr.lua")
end

--协程改造
local function init_coroutine()
coroutine.yield = function(...)
Expand Down Expand Up @@ -75,6 +70,7 @@ end
local function init_mainloop()
import("kernel/timer_mgr.lua")
import("kernel/update_mgr.lua")
import("driver/webhook.lua")
event_mgr = quanta.get("event_mgr")
thread_mgr = quanta.get("thread_mgr")
update_mgr = quanta.get("update_mgr")
Expand All @@ -88,7 +84,6 @@ function quanta.init()
--主循环
init_coroutine()
init_mainloop()
init_statis()
--网络
init_network()
--加载协议
Expand Down
9 changes: 2 additions & 7 deletions script/kernel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ local function init_core()
import("kernel/thread_mgr.lua")
import("kernel/event_mgr.lua")
import("kernel/config_mgr.lua")
import("kernel/perfeval_mgr.lua")
end

--初始化网络
Expand Down Expand Up @@ -64,16 +65,11 @@ local function init_mainloop()
import("kernel/timer_mgr.lua")
import("kernel/update_mgr.lua")
import("feature/scheduler.lua")
import("driver/webhook.lua")
update_mgr = quanta.get("update_mgr")
scheduler = quanta.get("scheduler")
end

--初始化统计
local function init_statis()
import("agent/proxy_agent.lua")
import("kernel/perfeval_mgr.lua")
end

function quanta.init()
--核心加载
init_core()
Expand All @@ -86,7 +82,6 @@ function quanta.init()
init_coroutine()
init_mainloop()
init_network()
init_statis()
--其他模式
if quanta.mode <= QuantaMode.ROUTER then
--加载monitor
Expand Down
6 changes: 2 additions & 4 deletions script/kernel/perfeval_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ local env_status = environ.status
local tclock_ms = timer.clock_ms
local qdefer = quanta.defer

local proxy_agent = quanta.get("proxy_agent")

local PerfevalMgr = singleton()
local prop = property(PerfevalMgr)
prop:reader("eval_id", 0)
Expand Down Expand Up @@ -75,8 +73,8 @@ function PerfevalMgr:start(eval_name)
end

function PerfevalMgr:stop(eval_data)
local clock_ms = tclock_ms()
proxy_agent:statistics("on_perfeval", eval_data, clock_ms)
--local clock_ms = tclock_ms()
--proxy_agent:statistics("on_perfeval", eval_data, clock_ms)
self.eval_list[eval_data.co][eval_data.eval_id] = nil
end

Expand Down
Loading

0 comments on commit 0a3ed65

Please sign in to comment.