Events
To allow more flexibility in logging large numbers of events, we provide a different type of event logging than “alarms”. Events are stored in the Log Server DB instead of the Admin DB, and more granular filtering rules are provided on the Admin Console. However, we do not yet provide the ability to send email notifications.
- From: The source of the event.
- Log level: currently supports four levels: INFO, WARNING, ERROR, CRITICAL.
- Message: detailed event information.
- Type: The event type, usually linked using an underscore. Type obtained in real-time, so the filter option will also be empty when there are no events.
- Type Description: used to describe the event type.
- Key: filter based on event message and event type description.
In addition to providing display capabilities, OpenResty Edge also opens up the ability to send custom events, which is why event types need to be fetched in real-time.
Sending custom events
To send custom events, the following steps are required:
- Define the global Lua module in which the event is defined and sent.
- Reference the global Lua module in the application
- Sending a request to trigger an event
Next, we use an example to demonstrate the above steps.
Define the global Lua module
The code is as follows:
local ev = require("events")
local isempty = require "table.isempty"
local _M = {}
local function aggr(a, b)
if isempty(a) then
return b
end
if isempty(b) then
return a
end
a_count = a[3] or 0
b_count = b[3] or 0
a[3] = a_count + b_count
return a
end
function _M.go()
local typ = "edge_custom_event"
local desc = "Custom Event"
local level = "ERROR"
local key = "custom_event_key_1"
local interval = 3
local msg_fmt = "custom event, host: %s(rule id: %s), count: %s"
local msg_args = {
"test.com", -- host
1, -- rule id
1,
}
local res, err = ev.send_event(typ, desc, level, key, msg_fmt, msg_args, aggr, interval)
if not res then
ngx.log(ngx.ERR, "failed to send event, error: ", err)
end
return ngx.say("hello world")
end
return _M
Parameters of the send_event method.
- typ: type of the event, named with underscore (_) concatenation.
- desc: description of the event type, can be empty(nil or “”).
- level: event level, take values: INFO, WARNING, ERROR, CRITICAL.
- key: keyword, the same key means the same class of events, and Edge will aggregate events with the same key.
- msg_fmt: format of the event message. When
msg_args
is empty,msg_fmt
will be used as the event message directly. - msg_args: the message parameter, which is used to render out the event message finally.
- aggr: aggregation function, it can aggregate the events with the same key.
- interval: message reporting interval, it should be a positive integer.
Reference to the global Lua module
Send a request to trigger an event
$ curl test.com
hello world