-
Notifications
You must be signed in to change notification settings - Fork 579
/
snsattributes.go
47 lines (39 loc) · 1.58 KB
/
snsattributes.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelaws // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws"
import (
"context"
"strings"
"github.com/aws/aws-sdk-go-v2/service/sns"
"github.com/aws/smithy-go/middleware"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
)
// SNSAttributeSetter sets SNS specific attributes depending on the SNS operation is being performed.
func SNSAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue {
snsAttributes := []attribute.KeyValue{semconv.MessagingSystemKey.String("aws_sns")}
switch v := in.Parameters.(type) {
case *sns.PublishBatchInput:
snsAttributes = append(snsAttributes,
semconv.MessagingDestinationName(extractDestinationName(v.TopicArn, nil)),
semconv.MessagingOperationTypePublish,
semconv.MessagingOperationName("publish_batch_input"),
semconv.MessagingBatchMessageCount(len(v.PublishBatchRequestEntries)),
)
case *sns.PublishInput:
snsAttributes = append(snsAttributes,
semconv.MessagingDestinationName(extractDestinationName(v.TopicArn, v.TargetArn)),
semconv.MessagingOperationTypePublish,
semconv.MessagingOperationName("publish_input"),
)
}
return snsAttributes
}
func extractDestinationName(topicArn, targetArn *string) string {
if topicArn != nil && *topicArn != "" {
return (*topicArn)[strings.LastIndex(*topicArn, ":")+1:]
} else if targetArn != nil && *targetArn != "" {
return (*targetArn)[strings.LastIndex(*targetArn, ":")+1:]
}
return ""
}