-
Notifications
You must be signed in to change notification settings - Fork 37
/
sns.go
97 lines (79 loc) · 2.54 KB
/
sns.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
// (c) Copyright IBM Corp. 2023
package instaawsv2
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/aws-sdk-go-v2/service/sns/types"
instana "github.com/instana/go-sensor"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
otlog "github.com/opentracing/opentracing-go/log"
)
var errUnknownSNSMethod = errors.New("sns method not instrumented")
type AWSSNSOperations struct{}
var _ AWSOperations = (*AWSSNSOperations)(nil)
func (o AWSSNSOperations) injectContextWithSpan(tr instana.TracerLogger, ctx context.Context, params interface{}) context.Context {
tags, err := o.extractTags(params)
if err != nil {
if errors.Is(err, errUnknownSNSMethod) {
tr.Logger().Error("failed to identify the sqs method: ", err.Error())
return ctx
}
}
// An exit span will be created independently without a parent span
// and sent if the user has opted in.
opts := []ot.StartSpanOption{
ext.SpanKindRPCClient,
tags,
}
parent, ok := instana.SpanFromContext(ctx)
if ok {
opts = append(opts, ot.ChildOf(parent.Context()))
}
sp := tr.Tracer().StartSpan("sns", opts...)
if err = o.injectSpanToCarrier(params, sp); err != nil {
tr.Logger().Error("failed to inject span context to the sns carrier: ", err.Error())
}
return instana.ContextWithSpan(ctx, sp)
}
func (o AWSSNSOperations) finishSpan(tr instana.TracerLogger, ctx context.Context, err error) {
sp, ok := instana.SpanFromContext(ctx)
if !ok {
tr.Logger().Error("failed to retrieve the sns child span from context.")
return
}
defer sp.Finish()
if err != nil {
sp.LogFields(otlog.Error(err))
sp.SetTag(snsError, err.Error())
}
}
func (o AWSSNSOperations) injectSpanToCarrier(params interface{}, sp ot.Span) error {
var ip *sns.PublishInput
var ok bool
if ip, ok = params.(*sns.PublishInput); !ok {
return errors.New("received param is not of type sns.PublishInput")
}
if ip.MessageAttributes == nil {
ip.MessageAttributes = make(map[string]types.MessageAttributeValue)
}
err := sp.Tracer().Inject(sp.Context(), ot.TextMap, snsMessageAttributesCarrier(ip.MessageAttributes))
if err != nil {
return err
}
return nil
}
func (o AWSSNSOperations) extractTags(params interface{}) (ot.Tags, error) {
switch params := params.(type) {
case *sns.PublishInput:
return ot.Tags{
snsTopic: stringDeRef(params.TopicArn),
snsTarget: stringDeRef(params.TargetArn),
snsPhone: stringDeRef(params.PhoneNumber),
snsSubject: stringDeRef(params.Subject),
}, nil
default:
return nil, errUnknownSNSMethod
}
}