-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathpropagation.go
168 lines (139 loc) · 3.96 KB
/
propagation.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// (c) Copyright IBM Corp. 2021
// (c) Copyright Instana Inc. 2021
package instaawssdk
import (
"encoding/base64"
"encoding/json"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/sns"
"github.com/aws/aws-sdk-go/service/sqs"
instana "github.com/instana/go-sensor"
"github.com/opentracing/opentracing-go"
)
const (
// FieldT is the trace ID message attribute key
FieldT = "X_INSTANA_T"
// FieldS is the span ID message attribute key
FieldS = "X_INSTANA_S"
// FieldL is the trace level message attribute key
FieldL = "X_INSTANA_L"
)
// SpanContextFromSQSMessage returns the trace context from an SQS message
func SpanContextFromSQSMessage(msg *sqs.Message, sensor instana.TracerLogger) (opentracing.SpanContext, bool) {
spanContext, err := sensor.Tracer().Extract(
opentracing.TextMap,
SQSMessageAttributesCarrier(msg.MessageAttributes),
)
if err != nil {
return nil, false
}
return spanContext, true
}
// SQSMessageAttributesCarrier creates a new trace context carrier suitable for (opentracing.Tracer).Inject()
// that uses SQS message attributes as a storage
func SQSMessageAttributesCarrier(attrs map[string]*sqs.MessageAttributeValue) messageAttributesCarrier {
return messageAttributesCarrier{
Attrs: sqsMessageAttributes(attrs),
}
}
// SNSMessageAttributesCarrier creates a new trace context carrier suitable for (opentracing.Tracer).Inject()
// that uses SNS message attributes as a storage
func SNSMessageAttributesCarrier(attrs map[string]*sns.MessageAttributeValue) messageAttributesCarrier {
return messageAttributesCarrier{
Attrs: snsMessageAttributes(attrs),
}
}
type messageAttributesCarrier struct {
Attrs interface {
Get(string) (string, bool)
Set(string, string)
Del(string)
}
}
func (c messageAttributesCarrier) Set(key, val string) {
switch strings.ToLower(key) {
case instana.FieldT:
c.Attrs.Set(FieldT, val)
case instana.FieldS:
c.Attrs.Set(FieldS, val)
case instana.FieldL:
c.Attrs.Set(FieldL, val)
}
}
func (c messageAttributesCarrier) ForeachKey(handler func(key, val string) error) error {
if v, ok := c.Attrs.Get(FieldT); ok {
handler(instana.FieldT, v)
}
if v, ok := c.Attrs.Get(FieldS); ok {
handler(instana.FieldS, v)
}
if v, ok := c.Attrs.Get(FieldL); ok {
handler(instana.FieldL, v)
}
return nil
}
type sqsMessageAttributes map[string]*sqs.MessageAttributeValue
func (attrs sqsMessageAttributes) Get(key string) (string, bool) {
if v, ok := attrs[key]; ok {
return aws.StringValue(v.StringValue), ok
}
return "", false
}
func (attrs sqsMessageAttributes) Set(key, val string) {
attrs[key] = &sqs.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(val),
}
}
func (attrs sqsMessageAttributes) Del(key string) {
delete(attrs, key)
}
type snsMessageAttributes map[string]*sns.MessageAttributeValue
func (attrs snsMessageAttributes) Get(key string) (string, bool) {
if v, ok := attrs[key]; ok {
return aws.StringValue(v.StringValue), ok
}
return "", false
}
func (attrs snsMessageAttributes) Set(key, val string) {
attrs[key] = &sns.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(val),
}
}
func (attrs snsMessageAttributes) Del(key string) {
delete(attrs, key)
}
func (attrs *snsMessageAttributes) UnmarshalJSON(data []byte) error {
var vs map[string]struct {
Type string `json:"Type"`
Value string `json:"Value"`
}
if err := json.Unmarshal(data, &vs); err != nil {
return err
}
if *attrs == nil {
*attrs = make(snsMessageAttributes, len(vs))
}
for k, v := range vs {
switch strings.ToLower(v.Type) {
case "string":
(*attrs)[k] = &sns.MessageAttributeValue{
DataType: aws.String("String"),
StringValue: aws.String(v.Value),
}
case "binary":
val, err := base64.StdEncoding.DecodeString(v.Value)
if err == nil {
(*attrs)[k] = &sns.MessageAttributeValue{
DataType: aws.String("Binary"),
BinaryValue: val,
}
}
default:
// skip
}
}
return nil
}