-
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.
- Loading branch information
1 parent
449b86f
commit 4f6b049
Showing
6 changed files
with
180 additions
and
15 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
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 = "WARN" | ||
ERROR = "ERROR" | ||
CRITICAL = "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,84 @@ | ||
package telemetry | ||
|
||
import ( | ||
"context" | ||
"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" | ||
"strings" | ||
) | ||
|
||
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) { | ||
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(string(attributes.DeploymentUID)), | ||
}, | ||
"entity": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(string(attributes.DeploymentUID)), | ||
}, | ||
"type": { | ||
DataType: aws.String("String"), | ||
StringValue: aws.String(string(attributes.Type)), | ||
}, | ||
}, | ||
Subject: aws.String(subject), | ||
TopicArn: aws.String(S.topic), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return publish.MessageId, nil | ||
} | ||
|
||
func NewSNSMessageSenderWithNewClient(ctx context.Context, config *SNSMessageSenderConfig) (SNSMessageSender, error) { | ||
client, err := newSnsClient(ctx) | ||
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) (*sns.Client, error) { | ||
sdkConfig, err := config.LoadDefaultConfig(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
snsClient := sns.NewFromConfig(sdkConfig) | ||
return snsClient, nil | ||
} |