-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
81c9320
commit 61bdc52
Showing
8 changed files
with
198 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
package alerting | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/ses" | ||
"github.com/aws/aws-sdk-go-v2/service/ses/types" | ||
|
||
"github.com/PeerDB-io/peer-flow/peerdbenv" | ||
) | ||
|
||
type EmailAlertSender interface { | ||
AlertSender | ||
} | ||
|
||
type emailAlertSenderImpl struct { | ||
client *ses.Client | ||
sourceEmail string | ||
configurationSetName string | ||
slotLagMBAlertThreshold uint32 | ||
openConnectionsAlertThreshold uint32 | ||
} | ||
|
||
func (e *emailAlertSenderImpl) getSlotLagMBAlertThreshold() uint32 { | ||
return e.slotLagMBAlertThreshold | ||
} | ||
|
||
func (e *emailAlertSenderImpl) getOpenConnectionsAlertThreshold() uint32 { | ||
return e.openConnectionsAlertThreshold | ||
} | ||
|
||
type EmailAlertSenderConfig struct { | ||
sourceEmail string | ||
configurationSetName string | ||
SlotLagMBAlertThreshold uint32 `json:"slot_lag_mb_alert_threshold"` | ||
OpenConnectionsAlertThreshold uint32 `json:"open_connections_alert_threshold"` | ||
} | ||
|
||
func (e *emailAlertSenderImpl) sendAlert(ctx context.Context, alertTitle string, alertMessage string) error { | ||
_, err := e.client.SendEmail(ctx, &ses.SendEmailInput{ | ||
Destination: &types.Destination{ | ||
ToAddresses: []string{}, | ||
}, | ||
Message: &types.Message{ | ||
Body: &types.Body{ | ||
Text: &types.Content{ | ||
Data: aws.String(alertMessage), | ||
Charset: aws.String("utf-8"), | ||
}, | ||
}, | ||
Subject: &types.Content{ | ||
Data: aws.String(alertTitle), | ||
Charset: aws.String("utf-8"), | ||
}, | ||
}, | ||
Source: aws.String(e.sourceEmail), | ||
ConfigurationSetName: aws.String(e.configurationSetName), | ||
Tags: []types.MessageTag{ | ||
{Name: aws.String("DeploymentUUID"), Value: aws.String(peerdbenv.PeerDBDeploymentUID())}, | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewEmailAlertSenderWithNewClient(ctx context.Context, config *EmailAlertSenderConfig) (EmailAlertSender, error) { | ||
client, err := newSesClient(ctx, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewEmailAlertSender(client, config), nil | ||
} | ||
|
||
func NewEmailAlertSender(client *ses.Client, config *EmailAlertSenderConfig) EmailAlertSender { | ||
return &emailAlertSenderImpl{ | ||
client: client, | ||
sourceEmail: config.sourceEmail, | ||
configurationSetName: config.configurationSetName, | ||
slotLagMBAlertThreshold: config.SlotLagMBAlertThreshold, | ||
openConnectionsAlertThreshold: config.OpenConnectionsAlertThreshold, | ||
} | ||
} | ||
|
||
func newSesClient(ctx context.Context, region *string) (*ses.Client, error) { | ||
sdkConfig, err := config.LoadDefaultConfig(ctx, func(options *config.LoadOptions) error { | ||
if region != nil { | ||
options.Region = *region | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
snsClient := ses.NewFromConfig(sdkConfig) | ||
return snsClient, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package alerting | ||
|
||
import "context" | ||
|
||
type AlertSender interface { | ||
sendAlert(ctx context.Context, alertTitle string, alertMessage string) error | ||
getSlotLagMBAlertThreshold() uint32 | ||
getOpenConnectionsAlertThreshold() uint32 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package alerting | ||
|
||
type ServiceType string | ||
|
||
const ( | ||
SLACK ServiceType = "slack" | ||
EMAIL ServiceType = "email" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters