|
| 1 | +package telemetry |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 10 | + "github.com/aws/aws-sdk-go-v2/config" |
| 11 | + "github.com/aws/aws-sdk-go-v2/service/sns" |
| 12 | + "github.com/aws/aws-sdk-go-v2/service/sns/types" |
| 13 | + "go.temporal.io/sdk/activity" |
| 14 | +) |
| 15 | + |
| 16 | +type SNSMessageSender interface { |
| 17 | + Sender |
| 18 | +} |
| 19 | + |
| 20 | +type SNSMessageSenderImpl struct { |
| 21 | + client *sns.Client |
| 22 | + topic string |
| 23 | +} |
| 24 | + |
| 25 | +type SNSMessageSenderConfig struct { |
| 26 | + Topic string `json:"topic"` |
| 27 | +} |
| 28 | + |
| 29 | +func (s *SNSMessageSenderImpl) SendMessage(ctx context.Context, subject string, body string, attributes Attributes) (*string, error) { |
| 30 | + activityInfo := activity.GetInfo(ctx) |
| 31 | + deduplicationString := strings.Join([]string{ |
| 32 | + "deployID", attributes.DeploymentUID, |
| 33 | + "subject", subject, |
| 34 | + "runID", activityInfo.WorkflowExecution.RunID, |
| 35 | + "activityName", activityInfo.ActivityType.Name, |
| 36 | + }, " || ") |
| 37 | + h := sha256.New() |
| 38 | + h.Write([]byte(deduplicationString)) |
| 39 | + deduplicationHash := hex.EncodeToString(h.Sum(nil)) |
| 40 | + |
| 41 | + publish, err := s.client.Publish(ctx, &sns.PublishInput{ |
| 42 | + Message: aws.String(body), |
| 43 | + MessageAttributes: map[string]types.MessageAttributeValue{ |
| 44 | + "level": { |
| 45 | + DataType: aws.String("String"), |
| 46 | + StringValue: aws.String(string(attributes.Level)), |
| 47 | + }, |
| 48 | + "tags": { |
| 49 | + DataType: aws.String("String"), |
| 50 | + StringValue: aws.String(strings.Join(attributes.Tags, ",")), |
| 51 | + }, |
| 52 | + "deploymentUUID": { |
| 53 | + DataType: aws.String("String"), |
| 54 | + StringValue: aws.String(attributes.DeploymentUID), |
| 55 | + }, |
| 56 | + "entity": { |
| 57 | + DataType: aws.String("String"), |
| 58 | + StringValue: aws.String(attributes.DeploymentUID), |
| 59 | + }, |
| 60 | + "type": { |
| 61 | + DataType: aws.String("String"), |
| 62 | + StringValue: aws.String(attributes.Type), |
| 63 | + }, |
| 64 | + "alias": { // This will act as a de-duplication ID |
| 65 | + DataType: aws.String("String"), |
| 66 | + StringValue: aws.String(deduplicationHash), |
| 67 | + }, |
| 68 | + }, |
| 69 | + Subject: aws.String(subject[:100]), |
| 70 | + TopicArn: aws.String(s.topic), |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + return publish.MessageId, nil |
| 76 | +} |
| 77 | + |
| 78 | +func NewSNSMessageSenderWithNewClient(ctx context.Context, config *SNSMessageSenderConfig) (SNSMessageSender, error) { |
| 79 | + // Topic Region must match client region |
| 80 | + region := strings.Split(strings.TrimPrefix(config.Topic, "arn:aws:sns:"), ":")[0] |
| 81 | + client, err := newSnsClient(ctx, ®ion) |
| 82 | + if err != nil { |
| 83 | + return nil, err |
| 84 | + } |
| 85 | + return &SNSMessageSenderImpl{ |
| 86 | + client: client, |
| 87 | + topic: config.Topic, |
| 88 | + }, nil |
| 89 | +} |
| 90 | + |
| 91 | +func NewSNSMessageSender(client *sns.Client, config *SNSMessageSenderConfig) SNSMessageSender { |
| 92 | + return &SNSMessageSenderImpl{ |
| 93 | + client: client, |
| 94 | + topic: config.Topic, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func newSnsClient(ctx context.Context, region *string) (*sns.Client, error) { |
| 99 | + sdkConfig, err := config.LoadDefaultConfig(ctx, func(options *config.LoadOptions) error { |
| 100 | + if region != nil { |
| 101 | + options.Region = *region |
| 102 | + } |
| 103 | + return nil |
| 104 | + }) |
| 105 | + if err != nil { |
| 106 | + return nil, err |
| 107 | + } |
| 108 | + snsClient := sns.NewFromConfig(sdkConfig) |
| 109 | + return snsClient, nil |
| 110 | +} |
0 commit comments