From 24c3a84db928bba4a70ea4cd3510276ace7a4da6 Mon Sep 17 00:00:00 2001 From: wei Date: Sat, 30 Sep 2023 19:51:03 +0800 Subject: [PATCH] fix(alerting): add condition results to ntfy (#582) --- alerting/provider/ntfy/ntfy.go | 14 ++++++++++++-- alerting/provider/ntfy/ntfy_test.go | 4 ++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/alerting/provider/ntfy/ntfy.go b/alerting/provider/ntfy/ntfy.go index 1c67f45f..4b1fe257 100644 --- a/alerting/provider/ntfy/ntfy.go +++ b/alerting/provider/ntfy/ntfy.go @@ -78,17 +78,27 @@ type Body struct { // buildRequestBody builds the request body for the provider func (provider *AlertProvider) buildRequestBody(endpoint *core.Endpoint, alert *alert.Alert, result *core.Result, resolved bool) []byte { - var message, tag string + var message, results, tag string if resolved { tag = "white_check_mark" message = "An alert has been resolved after passing successfully " + strconv.Itoa(alert.SuccessThreshold) + " time(s) in a row" } else { - tag = "x" + tag = "rotating_light" message = "An alert has been triggered due to having failed " + strconv.Itoa(alert.FailureThreshold) + " time(s) in a row" } + for _, conditionResult := range result.ConditionResults { + var prefix string + if conditionResult.Success { + prefix = "šŸŸ¢" + } else { + prefix = "šŸ”“" + } + results += fmt.Sprintf("\n%s %s", prefix, conditionResult.Condition) + } if len(alert.GetDescription()) > 0 { message += " with the following description: " + alert.GetDescription() } + message += results body, _ := json.Marshal(Body{ Topic: provider.Topic, Title: "Gatus: " + endpoint.DisplayName(), diff --git a/alerting/provider/ntfy/ntfy_test.go b/alerting/provider/ntfy/ntfy_test.go index 5aea9865..6786f3b3 100644 --- a/alerting/provider/ntfy/ntfy_test.go +++ b/alerting/provider/ntfy/ntfy_test.go @@ -79,14 +79,14 @@ func TestAlertProvider_buildRequestBody(t *testing.T) { Provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 1}, Alert: alert.Alert{Description: &firstDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: false, - ExpectedBody: `{"topic":"example","title":"Gatus: endpoint-name","message":"An alert has been triggered due to having failed 3 time(s) in a row with the following description: description-1","tags":["x"],"priority":1}`, + ExpectedBody: `{"topic":"example","title":"Gatus: endpoint-name","message":"An alert has been triggered due to having failed 3 time(s) in a row with the following description: description-1\nšŸ”“ [CONNECTED] == true\nšŸ”“ [STATUS] == 200","tags":["rotating_light"],"priority":1}`, }, { Name: "resolved", Provider: AlertProvider{URL: "https://ntfy.sh", Topic: "example", Priority: 2}, Alert: alert.Alert{Description: &secondDescription, SuccessThreshold: 5, FailureThreshold: 3}, Resolved: true, - ExpectedBody: `{"topic":"example","title":"Gatus: endpoint-name","message":"An alert has been resolved after passing successfully 5 time(s) in a row with the following description: description-2","tags":["white_check_mark"],"priority":2}`, + ExpectedBody: `{"topic":"example","title":"Gatus: endpoint-name","message":"An alert has been resolved after passing successfully 5 time(s) in a row with the following description: description-2\nšŸŸ¢ [CONNECTED] == true\nšŸŸ¢ [STATUS] == 200","tags":["white_check_mark"],"priority":2}`, }, } for _, scenario := range scenarios {