-
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.
feat(alerting): add email sender (#1433)
New ENV Vars: - `PEERDB_ALERTING_EMAIL_SENDER_SOURCE_EMAIL` - `PEERDB_ALERTING_EMAIL_SENDER_CONFIGURATION_SET` - `PEERDB_ALERTING_EMAIL_SENDER_REGION` - `PEERDB_ALERTING_EMAIL_SENDER_REPLY_TO_ADDRESSES` - [x] test aws cross region sending - config set is per region, have added env for setting a specific region UI: <img width="1550" alt="image" src="https://github.com/PeerDB-io/peerdb/assets/39487888/86a10ea3-418c-4593-bcfe-70fee46abbbb"> --------- Co-authored-by: Amogh-Bharadwaj <[email protected]>
- Loading branch information
1 parent
5e65fc7
commit d29f06d
Showing
20 changed files
with
592 additions
and
192 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,104 @@ | ||
package alerting | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"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/logger" | ||
"github.com/PeerDB-io/peer-flow/peerdbenv" | ||
"github.com/PeerDB-io/peer-flow/shared/aws_common" | ||
) | ||
|
||
type EmailAlertSender struct { | ||
AlertSender | ||
client *ses.Client | ||
sourceEmail string | ||
configurationSetName string | ||
replyToAddresses []string | ||
slotLagMBAlertThreshold uint32 | ||
openConnectionsAlertThreshold uint32 | ||
emailAddresses []string | ||
} | ||
|
||
func (e *EmailAlertSender) getSlotLagMBAlertThreshold() uint32 { | ||
return e.slotLagMBAlertThreshold | ||
} | ||
|
||
func (e *EmailAlertSender) getOpenConnectionsAlertThreshold() uint32 { | ||
return e.openConnectionsAlertThreshold | ||
} | ||
|
||
type EmailAlertSenderConfig struct { | ||
sourceEmail string | ||
configurationSetName string | ||
replyToAddresses []string | ||
SlotLagMBAlertThreshold uint32 `json:"slot_lag_mb_alert_threshold"` | ||
OpenConnectionsAlertThreshold uint32 `json:"open_connections_alert_threshold"` | ||
EmailAddresses []string `json:"email_addresses"` | ||
} | ||
|
||
func (e *EmailAlertSender) sendAlert(ctx context.Context, alertTitle string, alertMessage string) error { | ||
_, err := e.client.SendEmail(ctx, &ses.SendEmailInput{ | ||
Destination: &types.Destination{ | ||
ToAddresses: e.emailAddresses, | ||
}, | ||
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), | ||
ReplyToAddresses: e.replyToAddresses, | ||
Tags: []types.MessageTag{ | ||
{Name: aws.String("DeploymentUUID"), Value: aws.String(peerdbenv.PeerDBDeploymentUID())}, | ||
}, | ||
}) | ||
if err != nil { | ||
logger.LoggerFromCtx(ctx).Warn(fmt.Sprintf( | ||
"Error sending email alert from %v to %s subject=[%s], body=[%s], configurationSet=%s, replyToAddresses=[%v]", | ||
e.sourceEmail, e.emailAddresses, alertTitle, alertMessage, e.configurationSetName, e.replyToAddresses)) | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func NewEmailAlertSenderWithNewClient(ctx context.Context, region *string, config *EmailAlertSenderConfig) (*EmailAlertSender, error) { | ||
client, err := newSesClient(ctx, region) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewEmailAlertSender(client, config), nil | ||
} | ||
|
||
func NewEmailAlertSender(client *ses.Client, config *EmailAlertSenderConfig) *EmailAlertSender { | ||
return &EmailAlertSender{ | ||
client: client, | ||
sourceEmail: config.sourceEmail, | ||
configurationSetName: config.configurationSetName, | ||
replyToAddresses: config.replyToAddresses, | ||
slotLagMBAlertThreshold: config.SlotLagMBAlertThreshold, | ||
openConnectionsAlertThreshold: config.OpenConnectionsAlertThreshold, | ||
emailAddresses: config.EmailAddresses, | ||
} | ||
} | ||
|
||
func newSesClient(ctx context.Context, region *string) (*ses.Client, error) { | ||
sdkConfig, err := aws_common.LoadSdkConfig(ctx, region) | ||
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 | ||
} |
Oops, something went wrong.