Introduce configureable place holders for alerting

This commit is contained in:
Robert Hoppe 2021-02-17 12:39:17 +01:00
parent ea9623f695
commit 2873d96b9f
3 changed files with 64 additions and 9 deletions

View File

@ -16,11 +16,19 @@ import (
// AlertProvider is the configuration necessary for sending an alert using a custom HTTP request // AlertProvider is the configuration necessary for sending an alert using a custom HTTP request
// Technically, all alert providers should be reachable using the custom alert provider // Technically, all alert providers should be reachable using the custom alert provider
type AlertProvider struct { type AlertProvider struct {
URL string `yaml:"url"` URL string `yaml:"url"`
Method string `yaml:"method,omitempty"` Method string `yaml:"method,omitempty"`
Insecure bool `yaml:"insecure,omitempty"` Insecure bool `yaml:"insecure,omitempty"`
Body string `yaml:"body,omitempty"` Body string `yaml:"body,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"` Headers map[string]string `yaml:"headers,omitempty"`
Placeholders map[string]map[string]string `yaml:"placeholders,omitempty"`
}
var DefaultPlaceholderValues = map[string]map[string]string{
"ALERT_TRIGGERED_OR_RESOLVED": map[string]string{
"triggered": "TRIGGERED",
"resolved": "RESOLVED",
},
} }
// IsValid returns whether the provider's configuration is valid // IsValid returns whether the provider's configuration is valid
@ -33,10 +41,28 @@ func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, aler
return provider return provider
} }
// GetPlaceholderValue returns the Placeholder value
func (provider *AlertProvider) GetPlaceholderValue(name, status string) string {
if _, ok := provider.Placeholders[name]; ok {
if val, ok := provider.Placeholders[name][status]; ok {
return val
}
} else {
if _, ok := DefaultPlaceholderValues[name]; ok {
if val, ok := DefaultPlaceholderValues[name][status]; ok {
return val
}
}
}
return ""
}
func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request { func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription string, resolved bool) *http.Request {
body := provider.Body body := provider.Body
providerURL := provider.URL providerURL := provider.URL
method := provider.Method method := provider.Method
if strings.Contains(body, "[ALERT_DESCRIPTION]") { if strings.Contains(body, "[ALERT_DESCRIPTION]") {
body = strings.ReplaceAll(body, "[ALERT_DESCRIPTION]", alertDescription) body = strings.ReplaceAll(body, "[ALERT_DESCRIPTION]", alertDescription)
} }
@ -45,9 +71,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
} }
if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") { if strings.Contains(body, "[ALERT_TRIGGERED_OR_RESOLVED]") {
if resolved { if resolved {
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", "RESOLVED") body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "resolved"))
} else { } else {
body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", "TRIGGERED") body = strings.ReplaceAll(body, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered"))
} }
} }
if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") { if strings.Contains(providerURL, "[ALERT_DESCRIPTION]") {
@ -58,9 +84,9 @@ func (provider *AlertProvider) buildHTTPRequest(serviceName, alertDescription st
} }
if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") { if strings.Contains(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]") {
if resolved { if resolved {
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", "RESOLVED") providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "resolved"))
} else { } else {
providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", "TRIGGERED") providerURL = strings.ReplaceAll(providerURL, "[ALERT_TRIGGERED_OR_RESOLVED]", provider.GetPlaceholderValue("ALERT_TRIGGERED_OR_RESOLVED", "triggered"))
} }
} }
if len(method) == 0 { if len(method) == 0 {

View File

@ -68,3 +68,28 @@ func TestAlertProvider_ToCustomAlertProvider(t *testing.T) {
t.Error("customAlertProvider should've been equal to customAlertProvider") t.Error("customAlertProvider should've been equal to customAlertProvider")
} }
} }
func TestAlertProvider_buildHTTPRequestWithCustomPlaceholder(t *testing.T) {
const (
ExpectedURL = "http://example.com/service-name?event=MYTEST&description=alert-description"
ExpectedBody = "service-name,alert-description,MYTEST"
)
customAlertProvider := &AlertProvider{
URL: "http://example.com/[SERVICE_NAME]?event=[ALERT_TRIGGERED_OR_RESOLVED]&description=[ALERT_DESCRIPTION]",
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
Headers: nil,
Placeholders: map[string]map[string]string{
"ALERT_TRIGGERED_OR_RESOLVED": {
"resolved": "MYTEST",
},
},
}
request := customAlertProvider.buildHTTPRequest("service-name", "alert-description", true)
if request.URL.String() != ExpectedURL {
t.Error("expected URL to be", ExpectedURL, "was", request.URL.String())
}
body, _ := ioutil.ReadAll(request.Body)
if string(body) != ExpectedBody {
t.Error("expected body to be", ExpectedBody, "was", string(body))
}
}

View File

@ -344,6 +344,10 @@ alerting:
access-key: "1" access-key: "1"
originator: "31619191918" originator: "31619191918"
recipients: "31619191919" recipients: "31619191919"
placeholders:
ALERT_TRIGGERED_OR_RESOLVED:
triggered: "partial_outage"
resolved: "operational"
services: services:
- name: twinnation - name: twinnation
url: https://twinnation.org/health url: https://twinnation.org/health