-
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.
Merge branch 'main' into generic-simple-schema-changes
- Loading branch information
Showing
22 changed files
with
594 additions
and
194 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.