Skip to content

Commit e9223fd

Browse files
committed
feat(alerting): Add RESULT_CONDITIONS in custom alert to have more information on an alert while using custom alerting module
1 parent ce1777c commit e9223fd

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1551,7 +1551,8 @@ Furthermore, you may use the following placeholders in the body (`alerting.custo
15511551
- `[ENDPOINT_GROUP]` (resolved from `endpoints[].group`)
15521552
- `[ENDPOINT_URL]` (resolved from `endpoints[].url`)
15531553
- `[RESULT_ERRORS]` (resolved from the health evaluation of a given health check)
1554-
1554+
- `[RESULT_CONDITIONS]` (condition results from the health evaluation of a given health check)
1555+
-
15551556
If you have an alert using the `custom` provider with `send-on-resolved` set to `true`, you can use the
15561557
`[ALERT_TRIGGERED_OR_RESOLVED]` placeholder to differentiate the notifications.
15571558
The aforementioned placeholder will be replaced by `TRIGGERED` or `RESOLVED` accordingly, though it can be modified

alerting/provider/custom/custom.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,57 @@ func (provider *AlertProvider) buildHTTPRequest(cfg *Config, ep *endpoint.Endpoi
108108
url = strings.ReplaceAll(url, "[ENDPOINT_GROUP]", ep.Group)
109109
body = strings.ReplaceAll(body, "[ENDPOINT_URL]", ep.URL)
110110
url = strings.ReplaceAll(url, "[ENDPOINT_URL]", ep.URL)
111+
<<<<<<< HEAD
111112
resultErrors := strings.ReplaceAll(strings.Join(result.Errors, ","), "\"", "\\\"")
112113
body = strings.ReplaceAll(body, "[RESULT_ERRORS]", resultErrors)
113114
url = strings.ReplaceAll(url, "[RESULT_ERRORS]", resultErrors)
115+
116+
var formattedConditionResults string
117+
if len(result.ConditionResults) > 0 {
118+
for index, conditionResult := range result.ConditionResults {
119+
var prefix string
120+
if conditionResult.Success {
121+
prefix = "✅"
122+
} else {
123+
prefix = "❌"
124+
}
125+
formattedConditionResults += fmt.Sprintf("%s - `%s`", prefix, conditionResult.Condition)
126+
if index < len(result.ConditionResults)-1 {
127+
formattedConditionResults += ", "
128+
}
129+
}
130+
}
131+
132+
body = strings.ReplaceAll(body, "[RESULT_CONDITIONS]", formattedConditionResults)
133+
url = strings.ReplaceAll(url, "[RESULT_CONDITIONS]", formattedConditionResults)
134+
135+
||||||| b388cc87
136+
body = strings.ReplaceAll(body, "[RESULT_ERRORS]", strings.Join(result.Errors, ","))
137+
url = strings.ReplaceAll(url, "[RESULT_ERRORS]", strings.Join(result.Errors, ","))
138+
=======
139+
body = strings.ReplaceAll(body, "[RESULT_ERRORS]", strings.Join(result.Errors, ","))
140+
url = strings.ReplaceAll(url, "[RESULT_ERRORS]", strings.Join(result.Errors, ","))
141+
142+
var formattedConditionResults string
143+
if len(result.ConditionResults) > 0 {
144+
for index, conditionResult := range result.ConditionResults {
145+
var prefix string
146+
if conditionResult.Success {
147+
prefix = "✅"
148+
} else {
149+
prefix = "❌"
150+
}
151+
formattedConditionResults += fmt.Sprintf("%s - `%s`", prefix, conditionResult.Condition)
152+
if index < len(result.ConditionResults)-1 {
153+
formattedConditionResults += ", "
154+
}
155+
}
156+
}
157+
158+
body = strings.ReplaceAll(body, "[RESULT_CONDITIONS]", formattedConditionResults)
159+
url = strings.ReplaceAll(url, "[RESULT_CONDITIONS]", formattedConditionResults)
160+
161+
>>>>>>> c230f982a4ea73cd3d17b8a2553db240776a498f
114162
if resolved {
115163
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(cfg, true))
116164
url = strings.ReplaceAll(url, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetAlertStatePlaceholderValue(cfg, true))

0 commit comments

Comments
 (0)