add alert template variable trigger_at , duration
This commit is contained in:
parent
dcb5a6cad0
commit
754693c91b
|
@ -85,7 +85,7 @@ func RetrieveChannel(ch *alerting.Channel) (*alerting.Channel, error) {
|
||||||
}
|
}
|
||||||
if !refCh.Enabled {
|
if !refCh.Enabled {
|
||||||
ch.Enabled = false
|
ch.Enabled = false
|
||||||
return ch, nil
|
return ch, fmt.Errorf("global channel is not enabled")
|
||||||
}
|
}
|
||||||
ch.Type = refCh.Type
|
ch.Type = refCh.Type
|
||||||
ch.Name = refCh.Name
|
ch.Name = refCh.Name
|
||||||
|
|
|
@ -683,6 +683,8 @@ func (engine *Engine) Do(rule *alerting.Rule) error {
|
||||||
paramsCtx = newParameterCtx(rule, checkResults, util.MapStr{
|
paramsCtx = newParameterCtx(rule, checkResults, util.MapStr{
|
||||||
alerting2.ParamEventID: alertItem.ID,
|
alerting2.ParamEventID: alertItem.ID,
|
||||||
alerting2.ParamTimestamp: alertItem.Created.Unix(),
|
alerting2.ParamTimestamp: alertItem.Created.Unix(),
|
||||||
|
"duration": alertItem.Created.Sub(alertMessage.Created).String(),
|
||||||
|
"trigger_at": alertMessage.Created.Unix(),
|
||||||
})
|
})
|
||||||
err = attachTitleMessageToCtx(recoverCfg.Title, recoverCfg.Message, paramsCtx)
|
err = attachTitleMessageToCtx(recoverCfg.Title, recoverCfg.Message, paramsCtx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -707,6 +709,8 @@ func (engine *Engine) Do(rule *alerting.Rule) error {
|
||||||
paramsCtx = newParameterCtx(rule, checkResults, util.MapStr{
|
paramsCtx = newParameterCtx(rule, checkResults, util.MapStr{
|
||||||
alerting2.ParamEventID: alertItem.ID,
|
alerting2.ParamEventID: alertItem.ID,
|
||||||
alerting2.ParamTimestamp: alertItem.Created.Unix(),
|
alerting2.ParamTimestamp: alertItem.Created.Unix(),
|
||||||
|
"duration": alertItem.Created.Sub(alertMessage.Created).String(),
|
||||||
|
"trigger_at": alertMessage.Created.Unix(),
|
||||||
})
|
})
|
||||||
|
|
||||||
alertItem.Priority = priority
|
alertItem.Priority = priority
|
||||||
|
@ -797,6 +801,8 @@ func (engine *Engine) Do(rule *alerting.Rule) error {
|
||||||
alerting2.ParamEventID: alertItem.ID,
|
alerting2.ParamEventID: alertItem.ID,
|
||||||
alerting2.ParamTimestamp: alertItem.Created.Unix(),
|
alerting2.ParamTimestamp: alertItem.Created.Unix(),
|
||||||
"priority": priority,
|
"priority": priority,
|
||||||
|
"duration": alertItem.Created.Sub(alertMessage.Created).String(),
|
||||||
|
"trigger_at": alertMessage.Created.Unix(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -934,11 +940,22 @@ func (engine *Engine) Test(rule *alerting.Rule, msgType string) ([]alerting.Acti
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("check condition error:%w", err)
|
return nil, fmt.Errorf("check condition error:%w", err)
|
||||||
}
|
}
|
||||||
|
alertMessage, err := getLastAlertMessage(rule.ID, 2 * time.Minute)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("get alert message error: %w", err)
|
||||||
|
}
|
||||||
var actionResults []alerting.ActionExecutionResult
|
var actionResults []alerting.ActionExecutionResult
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
triggerAt := now
|
||||||
|
if alertMessage != nil {
|
||||||
|
triggerAt = alertMessage.Created
|
||||||
|
}
|
||||||
paramsCtx := newParameterCtx(rule, checkResults,util.MapStr{
|
paramsCtx := newParameterCtx(rule, checkResults,util.MapStr{
|
||||||
alerting2.ParamEventID: util.GetUUID(),
|
alerting2.ParamEventID: util.GetUUID(),
|
||||||
alerting2.ParamTimestamp: time.Now().Unix(),
|
alerting2.ParamTimestamp: now.Unix(),
|
||||||
|
"duration": now.Sub(triggerAt).String(),
|
||||||
|
"trigger_at": triggerAt.Unix(),
|
||||||
} )
|
} )
|
||||||
if msgType == "escalation" || msgType == "notification" {
|
if msgType == "escalation" || msgType == "notification" {
|
||||||
title, message := rule.GetNotificationTitleAndMessage()
|
title, message := rule.GetNotificationTitleAndMessage()
|
||||||
|
@ -989,19 +1006,25 @@ func performChannels(channels []alerting.Channel, ctx map[string]interface{}) ([
|
||||||
var errCount int
|
var errCount int
|
||||||
var actionResults []alerting.ActionExecutionResult
|
var actionResults []alerting.ActionExecutionResult
|
||||||
for _, channel := range channels {
|
for _, channel := range channels {
|
||||||
|
var (
|
||||||
|
errStr string
|
||||||
|
resBytes []byte
|
||||||
|
messageBytes []byte
|
||||||
|
)
|
||||||
_, err := common.RetrieveChannel(&channel)
|
_, err := common.RetrieveChannel(&channel)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error(err)
|
log.Error(err)
|
||||||
continue
|
|
||||||
}
|
|
||||||
if !channel.Enabled {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
resBytes, err, messageBytes := common.PerformChannel(&channel, ctx)
|
|
||||||
var errStr string
|
|
||||||
if err != nil {
|
|
||||||
errCount++
|
errCount++
|
||||||
errStr = err.Error()
|
errStr = err.Error()
|
||||||
|
}else{
|
||||||
|
if !channel.Enabled {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
resBytes, err, messageBytes = common.PerformChannel(&channel, ctx)
|
||||||
|
if err != nil {
|
||||||
|
errCount++
|
||||||
|
errStr = err.Error()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
actionResults = append(actionResults, alerting.ActionExecutionResult{
|
actionResults = append(actionResults, alerting.ActionExecutionResult{
|
||||||
Result: string(resBytes),
|
Result: string(resBytes),
|
||||||
|
|
Loading…
Reference in New Issue