-
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: add telemetry/alerts via sns (#1411)
- Loading branch information
1 parent
39c6e7e
commit 67fd5d3
Showing
9 changed files
with
206 additions
and
19 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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
type Sender interface { | ||
SendMessage(ctx context.Context, subject string, body string, attributes Attributes) (*string, error) | ||
} | ||
|
||
type Attributes struct { | ||
Level Level | ||
DeploymentUID string | ||
Tags []string | ||
Type string | ||
} | ||
|
||
type Level string | ||
|
||
const ( | ||
INFO Level = "INFO" | ||
WARN Level = "WARN" | ||
ERROR Level = "ERROR" | ||
CRITICAL Level = "CRITICAL" | ||
) |
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,110 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"encoding/hex" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/config" | ||
"github.com/aws/aws-sdk-go-v2/service/sns" | ||
"github.com/aws/aws-sdk-go-v2/service/sns/types" | ||
"go.temporal.io/sdk/activity" | ||
) | ||
|
||
type SNSMessageSender interface { | ||
Sender | ||
} | ||
|
||
type SNSMessageSenderImpl struct { | ||
client *sns.Client | ||
topic string | ||
} | ||
|
||
type SNSMessageSenderConfig struct { | ||
Topic string `json:"topic"` | ||
} | ||
|
||
func (s *SNSMessageSenderImpl) SendMessage(ctx context.Context, subject string, body string, attributes Attributes) (*string, error) { | ||
activityInfo := activity.GetInfo(ctx) | ||
deduplicationString := strings.Join([]string{ | ||
"deployID", attributes.DeploymentUID, | ||
"subject", subject, | ||
"runID", activityInfo.WorkflowExecution.RunID, | ||
"activityName", activityInfo.ActivityType.Name, | ||
}, " || ") | ||
h := sha256.New() | ||
h.Write([]byte(deduplicationString)) | ||
deduplicationHash := hex.EncodeToString(h.Sum(nil)) | ||
|
||
publish, err := s.client.Publish(ctx, &sns.PublishInput{ | ||
Message: aws.String(body), | ||
MessageAttributes: map[string]types.MessageAttributeValue{ | ||
"level": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(string(attributes.Level)), | ||
}, | ||
"tags": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(strings.Join(attributes.Tags, ",")), | ||
}, | ||
"deploymentUUID": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(attributes.DeploymentUID), | ||
}, | ||
"entity": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(attributes.DeploymentUID), | ||
}, | ||
"type": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(attributes.Type), | ||
}, | ||
"alias": { // This will act as a de-duplication ID | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(deduplicationHash), | ||
}, | ||
}, | ||
Subject: aws.String(subject[:100]), | ||
TopicArn: aws.String(s.topic), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return publish.MessageId, nil | ||
} | ||
|
||
func NewSNSMessageSenderWithNewClient(ctx context.Context, config *SNSMessageSenderConfig) (SNSMessageSender, error) { | ||
// Topic Region must match client region | ||
region := strings.Split(strings.TrimPrefix(config.Topic, "arn:aws:sns:"), ":")[0] | ||
client, err := newSnsClient(ctx, ®ion) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &SNSMessageSenderImpl{ | ||
client: client, | ||
topic: config.Topic, | ||
}, nil | ||
} | ||
|
||
func NewSNSMessageSender(client *sns.Client, config *SNSMessageSenderConfig) SNSMessageSender { | ||
return &SNSMessageSenderImpl{ | ||
client: client, | ||
topic: config.Topic, | ||
} | ||
} | ||
|
||
func newSnsClient(ctx context.Context, region *string) (*sns.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 := sns.NewFromConfig(sdkConfig) | ||
return snsClient, nil | ||
} |