| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelaws // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"strings" |
| 9 |
|
| 10 |
"github.com/aws/aws-sdk-go-v2/service/sns" |
| 11 |
"github.com/aws/smithy-go/middleware" |
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 14 |
) |
| 15 |
|
| 16 |
// SNSAttributeBuilder sets SNS specific attributes depending on the SNS operation is being performed. |
| 17 |
func SNSAttributeBuilder(_ context.Context, in middleware.InitializeInput, _ middleware.InitializeOutput) []attribute.KeyValue { |
| 18 |
snsAttributes := []attribute.KeyValue{semconv.MessagingSystemKey.String("aws_sns")} |
| 19 |
|
| 20 |
switch v := in.Parameters.(type) { |
| 21 |
case *sns.PublishBatchInput: |
| 22 |
snsAttributes = append(snsAttributes, |
| 23 |
semconv.MessagingDestinationName(extractDestinationName(v.TopicArn, nil)), |
| 24 |
semconv.MessagingOperationTypeSend, |
| 25 |
semconv.MessagingOperationName("publish_batch_input"), |
| 26 |
semconv.MessagingBatchMessageCount(len(v.PublishBatchRequestEntries)), |
| 27 |
) |
| 28 |
case *sns.PublishInput: |
| 29 |
snsAttributes = append(snsAttributes, |
| 30 |
semconv.MessagingDestinationName(extractDestinationName(v.TopicArn, v.TargetArn)), |
| 31 |
semconv.MessagingOperationTypeSend, |
| 32 |
semconv.MessagingOperationName("publish_input"), |
| 33 |
) |
| 34 |
} |
| 35 |
|
| 36 |
return snsAttributes |
| 37 |
} |
| 38 |
|
| 39 |
func extractDestinationName(topicArn, targetArn *string) string { |
| 40 |
if topicArn != nil && *topicArn != "" { |
| 41 |
return (*topicArn)[strings.LastIndex(*topicArn, ":")+1:] |
| 42 |
} else if targetArn != nil && *targetArn != "" { |
| 43 |
return (*targetArn)[strings.LastIndex(*targetArn, ":")+1:] |
| 44 |
} |
| 45 |
return "" |
| 46 |
} |