Work on #12: Move each providers in their own packages
This commit is contained in:
@ -1,8 +1,15 @@
|
|||||||
package alerting
|
package alerting
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/pagerduty"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/slack"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/twilio"
|
||||||
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Slack *SlackAlertProvider `yaml:"slack"`
|
Slack *slack.AlertProvider `yaml:"slack"`
|
||||||
PagerDuty *PagerDutyAlertProvider `yaml:"pagerduty"`
|
PagerDuty *pagerduty.AlertProvider `yaml:"pagerduty"`
|
||||||
Twilio *TwilioAlertProvider `yaml:"twilio"`
|
Twilio *twilio.AlertProvider `yaml:"twilio"`
|
||||||
Custom *CustomAlertProvider `yaml:"custom"`
|
Custom *custom.AlertProvider `yaml:"custom"`
|
||||||
}
|
}
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
package alerting
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestPagerDutyAlertProvider_IsValid(t *testing.T) {
|
|
||||||
invalidProvider := PagerDutyAlertProvider{IntegrationKey: ""}
|
|
||||||
if invalidProvider.IsValid() {
|
|
||||||
t.Error("provider shouldn't have been valid")
|
|
||||||
}
|
|
||||||
validProvider := PagerDutyAlertProvider{IntegrationKey: "00000000000000000000000000000000"}
|
|
||||||
if !validProvider.IsValid() {
|
|
||||||
t.Error("provider should've been valid")
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,4 +1,4 @@
|
|||||||
package alerting
|
package custom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@ -9,18 +9,18 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CustomAlertProvider struct {
|
type AlertProvider struct {
|
||||||
Url string `yaml:"url"`
|
Url string `yaml:"url"`
|
||||||
Method string `yaml:"method,omitempty"`
|
Method string `yaml:"method,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"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *CustomAlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.Url) > 0
|
return len(provider.Url) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *CustomAlertProvider) buildRequest(serviceName, alertDescription string, resolved bool) *http.Request {
|
func (provider *AlertProvider) buildRequest(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
|
||||||
@ -62,7 +62,7 @@ func (provider *CustomAlertProvider) buildRequest(serviceName, alertDescription
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Send a request to the alert provider and return the body
|
// Send a request to the alert provider and return the body
|
||||||
func (provider *CustomAlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
func (provider *AlertProvider) Send(serviceName, alertDescription string, resolved bool) ([]byte, error) {
|
||||||
request := provider.buildRequest(serviceName, alertDescription, resolved)
|
request := provider.buildRequest(serviceName, alertDescription, resolved)
|
||||||
response, err := client.GetHttpClient().Do(request)
|
response, err := client.GetHttpClient().Do(request)
|
||||||
if err != nil {
|
if err != nil {
|
@ -1,27 +1,27 @@
|
|||||||
package alerting
|
package custom
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCustomAlertProvider_IsValid(t *testing.T) {
|
func TestAlertProvider_IsValid(t *testing.T) {
|
||||||
invalidProvider := CustomAlertProvider{Url: ""}
|
invalidProvider := AlertProvider{Url: ""}
|
||||||
if invalidProvider.IsValid() {
|
if invalidProvider.IsValid() {
|
||||||
t.Error("provider shouldn't have been valid")
|
t.Error("provider shouldn't have been valid")
|
||||||
}
|
}
|
||||||
validProvider := CustomAlertProvider{Url: "http://example.com"}
|
validProvider := AlertProvider{Url: "http://example.com"}
|
||||||
if !validProvider.IsValid() {
|
if !validProvider.IsValid() {
|
||||||
t.Error("provider should've been valid")
|
t.Error("provider should've been valid")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomAlertProvider_buildRequestWhenResolved(t *testing.T) {
|
func TestAlertProvider_buildRequestWhenResolved(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
ExpectedUrl = "http://example.com/service-name"
|
ExpectedUrl = "http://example.com/service-name"
|
||||||
ExpectedBody = "service-name,alert-description,RESOLVED"
|
ExpectedBody = "service-name,alert-description,RESOLVED"
|
||||||
)
|
)
|
||||||
customAlertProvider := &CustomAlertProvider{
|
customAlertProvider := &AlertProvider{
|
||||||
Url: "http://example.com/[SERVICE_NAME]",
|
Url: "http://example.com/[SERVICE_NAME]",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
||||||
@ -37,12 +37,12 @@ func TestCustomAlertProvider_buildRequestWhenResolved(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomAlertProvider_buildRequestWhenTriggered(t *testing.T) {
|
func TestAlertProvider_buildRequestWhenTriggered(t *testing.T) {
|
||||||
const (
|
const (
|
||||||
ExpectedUrl = "http://example.com/service-name"
|
ExpectedUrl = "http://example.com/service-name"
|
||||||
ExpectedBody = "service-name,alert-description,TRIGGERED"
|
ExpectedBody = "service-name,alert-description,TRIGGERED"
|
||||||
)
|
)
|
||||||
customAlertProvider := &CustomAlertProvider{
|
customAlertProvider := &AlertProvider{
|
||||||
Url: "http://example.com/[SERVICE_NAME]",
|
Url: "http://example.com/[SERVICE_NAME]",
|
||||||
Method: "GET",
|
Method: "GET",
|
||||||
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
Body: "[SERVICE_NAME],[ALERT_DESCRIPTION],[ALERT_TRIGGERED_OR_RESOLVED]",
|
@ -1,21 +1,22 @@
|
|||||||
package alerting
|
package pagerduty
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PagerDutyAlertProvider struct {
|
type AlertProvider struct {
|
||||||
IntegrationKey string `yaml:"integration-key"`
|
IntegrationKey string `yaml:"integration-key"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *PagerDutyAlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.IntegrationKey) == 32
|
return len(provider.IntegrationKey) == 32
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://developer.pagerduty.com/docs/events-api-v2/trigger-events/
|
// https://developer.pagerduty.com/docs/events-api-v2/trigger-events/
|
||||||
func (provider *PagerDutyAlertProvider) ToCustomAlertProvider(eventAction, resolveKey string, service *core.Service, message string) *CustomAlertProvider {
|
func (provider *AlertProvider) ToCustomAlertProvider(eventAction, resolveKey string, service *core.Service, message string) *custom.AlertProvider {
|
||||||
return &CustomAlertProvider{
|
return &custom.AlertProvider{
|
||||||
Url: "https://events.pagerduty.com/v2/enqueue",
|
Url: "https://events.pagerduty.com/v2/enqueue",
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Body: fmt.Sprintf(`{
|
Body: fmt.Sprintf(`{
|
14
alerting/provider/pagerduty/pagerduty_test.go
Normal file
14
alerting/provider/pagerduty/pagerduty_test.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package pagerduty
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestAlertProvider_IsValid(t *testing.T) {
|
||||||
|
invalidProvider := AlertProvider{IntegrationKey: ""}
|
||||||
|
if invalidProvider.IsValid() {
|
||||||
|
t.Error("provider shouldn't have been valid")
|
||||||
|
}
|
||||||
|
validProvider := AlertProvider{IntegrationKey: "00000000000000000000000000000000"}
|
||||||
|
if !validProvider.IsValid() {
|
||||||
|
t.Error("provider should've been valid")
|
||||||
|
}
|
||||||
|
}
|
@ -1,19 +1,20 @@
|
|||||||
package alerting
|
package slack
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SlackAlertProvider struct {
|
type AlertProvider struct {
|
||||||
WebhookUrl string `yaml:"webhook-url"`
|
WebhookUrl string `yaml:"webhook-url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *SlackAlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.WebhookUrl) > 0
|
return len(provider.WebhookUrl) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *SlackAlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *CustomAlertProvider {
|
func (provider *AlertProvider) ToCustomAlertProvider(service *core.Service, alert *core.Alert, result *core.Result, resolved bool) *custom.AlertProvider {
|
||||||
var message string
|
var message string
|
||||||
var color string
|
var color string
|
||||||
if resolved {
|
if resolved {
|
||||||
@ -33,7 +34,7 @@ func (provider *SlackAlertProvider) ToCustomAlertProvider(service *core.Service,
|
|||||||
}
|
}
|
||||||
results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition)
|
results += fmt.Sprintf("%s - `%s`\n", prefix, conditionResult.Condition)
|
||||||
}
|
}
|
||||||
return &CustomAlertProvider{
|
return &custom.AlertProvider{
|
||||||
Url: provider.WebhookUrl,
|
Url: provider.WebhookUrl,
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Body: fmt.Sprintf(`{
|
Body: fmt.Sprintf(`{
|
14
alerting/provider/slack/slack_test.go
Normal file
14
alerting/provider/slack/slack_test.go
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package slack
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func TestAlertProvider_IsValid(t *testing.T) {
|
||||||
|
invalidProvider := AlertProvider{WebhookUrl: ""}
|
||||||
|
if invalidProvider.IsValid() {
|
||||||
|
t.Error("provider shouldn't have been valid")
|
||||||
|
}
|
||||||
|
validProvider := AlertProvider{WebhookUrl: "http://example.com"}
|
||||||
|
if !validProvider.IsValid() {
|
||||||
|
t.Error("provider should've been valid")
|
||||||
|
}
|
||||||
|
}
|
@ -1,24 +1,25 @@
|
|||||||
package alerting
|
package twilio
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TwilioAlertProvider struct {
|
type AlertProvider struct {
|
||||||
SID string `yaml:"sid"`
|
SID string `yaml:"sid"`
|
||||||
Token string `yaml:"token"`
|
Token string `yaml:"token"`
|
||||||
From string `yaml:"from"`
|
From string `yaml:"from"`
|
||||||
To string `yaml:"to"`
|
To string `yaml:"to"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *TwilioAlertProvider) IsValid() bool {
|
func (provider *AlertProvider) IsValid() bool {
|
||||||
return len(provider.Token) > 0 && len(provider.SID) > 0 && len(provider.From) > 0 && len(provider.To) > 0
|
return len(provider.Token) > 0 && len(provider.SID) > 0 && len(provider.From) > 0 && len(provider.To) > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (provider *TwilioAlertProvider) ToCustomAlertProvider(message string) *CustomAlertProvider {
|
func (provider *AlertProvider) ToCustomAlertProvider(message string) *custom.AlertProvider {
|
||||||
return &CustomAlertProvider{
|
return &custom.AlertProvider{
|
||||||
Url: fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", provider.SID),
|
Url: fmt.Sprintf("https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json", provider.SID),
|
||||||
Method: "POST",
|
Method: "POST",
|
||||||
Body: url.Values{
|
Body: url.Values{
|
@ -1,13 +1,13 @@
|
|||||||
package alerting
|
package twilio
|
||||||
|
|
||||||
import "testing"
|
import "testing"
|
||||||
|
|
||||||
func TestTwilioAlertProvider_IsValid(t *testing.T) {
|
func TestTwilioAlertProvider_IsValid(t *testing.T) {
|
||||||
invalidProvider := TwilioAlertProvider{}
|
invalidProvider := AlertProvider{}
|
||||||
if invalidProvider.IsValid() {
|
if invalidProvider.IsValid() {
|
||||||
t.Error("provider shouldn't have been valid")
|
t.Error("provider shouldn't have been valid")
|
||||||
}
|
}
|
||||||
validProvider := TwilioAlertProvider{
|
validProvider := AlertProvider{
|
||||||
SID: "1",
|
SID: "1",
|
||||||
Token: "1",
|
Token: "1",
|
||||||
From: "1",
|
From: "1",
|
@ -1,14 +0,0 @@
|
|||||||
package alerting
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestSlackAlertProvider_IsValid(t *testing.T) {
|
|
||||||
invalidProvider := SlackAlertProvider{WebhookUrl: ""}
|
|
||||||
if invalidProvider.IsValid() {
|
|
||||||
t.Error("provider shouldn't have been valid")
|
|
||||||
}
|
|
||||||
validProvider := SlackAlertProvider{WebhookUrl: "http://example.com"}
|
|
||||||
if !validProvider.IsValid() {
|
|
||||||
t.Error("provider should've been valid")
|
|
||||||
}
|
|
||||||
}
|
|
@ -3,7 +3,7 @@ package watchdog
|
|||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/TwinProduction/gatus/alerting"
|
"github.com/TwinProduction/gatus/alerting/provider/custom"
|
||||||
"github.com/TwinProduction/gatus/config"
|
"github.com/TwinProduction/gatus/config"
|
||||||
"github.com/TwinProduction/gatus/core"
|
"github.com/TwinProduction/gatus/core"
|
||||||
"log"
|
"log"
|
||||||
@ -36,7 +36,7 @@ func handleAlertsToTrigger(service *core.Service, result *core.Result, cfg *conf
|
|||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var alertProvider *alerting.CustomAlertProvider
|
var alertProvider *custom.AlertProvider
|
||||||
if alert.Type == core.SlackAlert {
|
if alert.Type == core.SlackAlert {
|
||||||
if cfg.Alerting.Slack != nil && cfg.Alerting.Slack.IsValid() {
|
if cfg.Alerting.Slack != nil && cfg.Alerting.Slack.IsValid() {
|
||||||
log.Printf("[watchdog][handleAlertsToTrigger] Sending Slack alert because alert with description='%s' has been triggered", alert.Description)
|
log.Printf("[watchdog][handleAlertsToTrigger] Sending Slack alert because alert with description='%s' has been triggered", alert.Description)
|
||||||
@ -103,7 +103,7 @@ func handleAlertsToResolve(service *core.Service, result *core.Result, cfg *conf
|
|||||||
if !alert.SendOnResolved {
|
if !alert.SendOnResolved {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
var alertProvider *alerting.CustomAlertProvider
|
var alertProvider *custom.AlertProvider
|
||||||
if alert.Type == core.SlackAlert {
|
if alert.Type == core.SlackAlert {
|
||||||
if cfg.Alerting.Slack != nil && cfg.Alerting.Slack.IsValid() {
|
if cfg.Alerting.Slack != nil && cfg.Alerting.Slack.IsValid() {
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Sending Slack alert because alert with description='%s' has been resolved", alert.Description)
|
log.Printf("[watchdog][handleAlertsToResolve] Sending Slack alert because alert with description='%s' has been resolved", alert.Description)
|
||||||
@ -128,7 +128,7 @@ func handleAlertsToResolve(service *core.Service, result *core.Result, cfg *conf
|
|||||||
} else if alert.Type == core.CustomAlert {
|
} else if alert.Type == core.CustomAlert {
|
||||||
if cfg.Alerting.Custom != nil && cfg.Alerting.Custom.IsValid() {
|
if cfg.Alerting.Custom != nil && cfg.Alerting.Custom.IsValid() {
|
||||||
log.Printf("[watchdog][handleAlertsToResolve] Sending custom alert because alert with description='%s' has been resolved", alert.Description)
|
log.Printf("[watchdog][handleAlertsToResolve] Sending custom alert because alert with description='%s' has been resolved", alert.Description)
|
||||||
alertProvider = &alerting.CustomAlertProvider{
|
alertProvider = &custom.AlertProvider{
|
||||||
Url: cfg.Alerting.Custom.Url,
|
Url: cfg.Alerting.Custom.Url,
|
||||||
Method: cfg.Alerting.Custom.Method,
|
Method: cfg.Alerting.Custom.Method,
|
||||||
Body: cfg.Alerting.Custom.Body,
|
Body: cfg.Alerting.Custom.Body,
|
||||||
|
Reference in New Issue
Block a user