-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqs.go
59 lines (51 loc) · 1.58 KB
/
sqs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package spanctx
import (
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/opentracing/opentracing-go"
)
type sqsInjectAttributeCarrier map[string]*sqs.MessageAttributeValue
func (c sqsInjectAttributeCarrier) ForeachKey(handler func(key, val string) error) error {
for k, val := range c {
if *val.DataType != "String" {
continue
}
if err := handler(k, *val.StringValue); err != nil {
return err
}
}
return nil
}
func (c sqsInjectAttributeCarrier) Set(key, val string) {
c[key] = &sqs.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(val),
}
}
func AddToSQSMessageInput(spanCtx opentracing.SpanContext, pubInput *sqs.SendMessageInput) error {
if spanCtx == nil || pubInput == nil {
return nil
}
if pubInput.MessageAttributes == nil {
pubInput.MessageAttributes = make(sqsInjectAttributeCarrier)
}
c := sqsInjectAttributeCarrier(pubInput.MessageAttributes)
return opentracing.GlobalTracer().Inject(spanCtx, opentracing.TextMap, c)
}
type sqsExtractAttributeCarrier map[string]events.SQSMessageAttribute
func (c sqsExtractAttributeCarrier) ForeachKey(handler func(key, val string) error) error {
for k, val := range c {
if val.DataType != "String" {
continue
}
if err := handler(k, *val.StringValue); err != nil {
return err
}
}
return nil
}
func GetFromSQSEvent(event events.SQSEvent) (opentracing.SpanContext, error) {
c := sqsExtractAttributeCarrier(event.Records[0].MessageAttributes)
return opentracing.GlobalTracer().Extract(opentracing.TextMap, c)
}