-
Notifications
You must be signed in to change notification settings - Fork 97
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add telemetry/alerts via sns #1411
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6e41cb5
feat: add telemetry via sns
iamKunalGupta 4ec0bab
feat: add end of snaphost event
iamKunalGupta 16fd466
chore: linter fixes
iamKunalGupta a49ea06
fix: topic region must match client region
iamKunalGupta 83e990b
fix: add return
iamKunalGupta acc7b28
fix: error not propagating properly
iamKunalGupta df706b7
fix: sns subject limit
iamKunalGupta eb95ea8
feat: add alias as a de-duplication ID
iamKunalGupta 44e92cf
chore: use sha as dedup id instead
iamKunalGupta 0214642
feat: attach activity info with body
iamKunalGupta 1286635
refactor: use different env and dedup strategy
iamKunalGupta a616dc5
chore: remove extra activity info from body
iamKunalGupta 67414b7
chore: linter fixes
iamKunalGupta File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use
strings.Cut
: https://pkg.go.dev/strings#CutMay also want
strings.CutPrefix
with error if Topic is lacking prefix