Skip to content

Commit a761314

Browse files
feat(otelaws): add sns attribute instrumentation (#6388)
**Notes:** * Only the `semconv/v1.27.0` was available in the latest published version of the otel module. * The current project setup doesn't take into consideration all the various hooks (`m.initializeMiddlewareBefore, m.initializeMiddlewareAfter, m.finalizeMiddlewareAfter, m.deserializeMiddleware`). All attributes are currently being set on `initializeMiddlewareAfter`. * Ref: https://opentelemetry.io/docs/specs/semconv/attributes-registry/messaging/ * Added changelog Co-authored-by: Tyler Yahn <[email protected]>
1 parent 3af31bd commit a761314

File tree

12 files changed

+124
-0
lines changed

12 files changed

+124
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1414
- Added support exporting logs via OTLP over gRPC in `go.opentelemetry.io/contrib/config`. (#6340)
1515
- The `go.opentelemetry.io/contrib/bridges/otellogr` module.
1616
This module provides an OpenTelemetry logging bridge for `github.com/go-logr/logr`. (#6386)
17+
- Added SNS instrumentation in `go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws`. (#6388)
1718

1819
### Changed
1920

instrumentation/github.com/aws/aws-lambda-go/otellambda/example/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ require (
3737
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.6 // indirect
3838
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 // indirect
3939
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6 // indirect
40+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7 // indirect
4041
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2 // indirect
4142
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 // indirect
4243
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 // indirect

instrumentation/github.com/aws/aws-lambda-go/otellambda/example/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6 h1:BbGDtTi0T1DYlm
3232
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6/go.mod h1:hLMJt7Q8ePgViKupeymbqI0la+t9/iYFBjxQCFwuAwI=
3333
github.com/aws/aws-sdk-go-v2/service/s3 v1.71.0 h1:nyuzXooUNJexRT0Oy0UQY6AhOzxPxhtt4DcBIHyCnmw=
3434
github.com/aws/aws-sdk-go-v2/service/s3 v1.71.0/go.mod h1:sT/iQz8JK3u/5gZkT+Hmr7GzVZehUMkRZpOaAwYXeGY=
35+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7 h1:N3o8mXK6/MP24BtD9sb51omEO9J9cgPM3Ughc293dZc=
36+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7/go.mod h1:AAHZydTB8/V2zn3WNwjLXBK1RAcSEpDNmFfrmjvrJQg=
3537
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2 h1:mFLfxLZB/TVQwNJAYox4WaxpIu+dFVIcExrmRmRCOhw=
3638
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2/go.mod h1:GnvfTdlvcpD+or3oslHPOn4Mu6KaCwlCp+0p0oqWnrM=
3739
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 h1:rLnYAfXQ3YAccocshIH5mzNNwZBkBo+bP6EhIxak6Hw=

instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/attributes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
v2Middleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
1010
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
11+
"github.com/aws/aws-sdk-go-v2/service/sns"
1112
"github.com/aws/aws-sdk-go-v2/service/sqs"
1213
"github.com/aws/smithy-go/middleware"
1314

@@ -25,6 +26,7 @@ const (
2526
var servicemap = map[string]AttributeSetter{
2627
dynamodb.ServiceID: DynamoDBAttributeSetter,
2728
sqs.ServiceID: SQSAttributeSetter,
29+
sns.ServiceID: SNSAttributeSetter,
2830
}
2931

3032
// SystemAttr return the AWS RPC system attribute.

instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/example/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ require (
2929
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.6 // indirect
3030
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.6 // indirect
3131
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6 // indirect
32+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7 // indirect
3233
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2 // indirect
3334
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 // indirect
3435
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.6 // indirect

instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/example/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6 h1:BbGDtTi0T1DYlm
3030
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.6/go.mod h1:hLMJt7Q8ePgViKupeymbqI0la+t9/iYFBjxQCFwuAwI=
3131
github.com/aws/aws-sdk-go-v2/service/s3 v1.71.0 h1:nyuzXooUNJexRT0Oy0UQY6AhOzxPxhtt4DcBIHyCnmw=
3232
github.com/aws/aws-sdk-go-v2/service/s3 v1.71.0/go.mod h1:sT/iQz8JK3u/5gZkT+Hmr7GzVZehUMkRZpOaAwYXeGY=
33+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7 h1:N3o8mXK6/MP24BtD9sb51omEO9J9cgPM3Ughc293dZc=
34+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7/go.mod h1:AAHZydTB8/V2zn3WNwjLXBK1RAcSEpDNmFfrmjvrJQg=
3335
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2 h1:mFLfxLZB/TVQwNJAYox4WaxpIu+dFVIcExrmRmRCOhw=
3436
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2/go.mod h1:GnvfTdlvcpD+or3oslHPOn4Mu6KaCwlCp+0p0oqWnrM=
3537
github.com/aws/aws-sdk-go-v2/service/sso v1.24.7 h1:rLnYAfXQ3YAccocshIH5mzNNwZBkBo+bP6EhIxak6Hw=

instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.22
55
require (
66
github.com/aws/aws-sdk-go-v2 v1.32.6
77
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.38.0
8+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7
89
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2
910
github.com/aws/smithy-go v1.22.1
1011
github.com/stretchr/testify v1.10.0

instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 h1:iXtILhv
1010
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1/go.mod h1:9nu0fVANtYiAePIBh2/pFUSwtJ402hLnp854CNoDOeE=
1111
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.6 h1:nbmKXZzXPJn41CcD4HsHsGWqvKjLKz9kWu6XxvLmf1s=
1212
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.6/go.mod h1:SJhcisfKfAawsdNQoZMBEjg+vyN2lH6rO6fP+T94z5Y=
13+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7 h1:N3o8mXK6/MP24BtD9sb51omEO9J9cgPM3Ughc293dZc=
14+
github.com/aws/aws-sdk-go-v2/service/sns v1.33.7/go.mod h1:AAHZydTB8/V2zn3WNwjLXBK1RAcSEpDNmFfrmjvrJQg=
1315
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2 h1:mFLfxLZB/TVQwNJAYox4WaxpIu+dFVIcExrmRmRCOhw=
1416
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.2/go.mod h1:GnvfTdlvcpD+or3oslHPOn4Mu6KaCwlCp+0p0oqWnrM=
1517
github.com/aws/smithy-go v1.22.1 h1:/HPHZQ0g7f4eUeK6HKglFz8uwVfZKgoI25rb/J+dnro=
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
13+
"go.opentelemetry.io/otel/attribute"
14+
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
15+
)
16+
17+
// SNSAttributeSetter sets SNS specific attributes depending on the SNS operation is being performed.
18+
func SNSAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue {
19+
snsAttributes := []attribute.KeyValue{semconv.MessagingSystemKey.String("aws_sns")}
20+
21+
switch v := in.Parameters.(type) {
22+
case *sns.PublishBatchInput:
23+
snsAttributes = append(snsAttributes,
24+
semconv.MessagingDestinationName(extractDestinationName(v.TopicArn, nil)),
25+
semconv.MessagingOperationTypePublish,
26+
semconv.MessagingOperationName("publish_batch_input"),
27+
semconv.MessagingBatchMessageCount(len(v.PublishBatchRequestEntries)),
28+
)
29+
case *sns.PublishInput:
30+
snsAttributes = append(snsAttributes,
31+
semconv.MessagingDestinationName(extractDestinationName(v.TopicArn, v.TargetArn)),
32+
semconv.MessagingOperationTypePublish,
33+
semconv.MessagingOperationName("publish_input"),
34+
)
35+
}
36+
37+
return snsAttributes
38+
}
39+
40+
func extractDestinationName(topicArn, targetArn *string) string {
41+
if topicArn != nil && *topicArn != "" {
42+
return (*topicArn)[strings.LastIndex(*topicArn, ":")+1:]
43+
} else if targetArn != nil && *targetArn != "" {
44+
return (*targetArn)[strings.LastIndex(*targetArn, ":")+1:]
45+
}
46+
return ""
47+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package otelaws
5+
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/aws/aws-sdk-go-v2/aws"
11+
"github.com/aws/aws-sdk-go-v2/service/sns"
12+
"github.com/aws/aws-sdk-go-v2/service/sns/types"
13+
"github.com/aws/smithy-go/middleware"
14+
"github.com/stretchr/testify/assert"
15+
16+
semconv "go.opentelemetry.io/otel/semconv/v1.27.0"
17+
)
18+
19+
func TestPublishInput(t *testing.T) {
20+
input := middleware.InitializeInput{
21+
Parameters: &sns.PublishInput{
22+
TopicArn: aws.String("arn:aws:sns:us-east-2:444455556666:my-topic"),
23+
},
24+
}
25+
26+
attributes := SNSAttributeSetter(context.Background(), input)
27+
28+
assert.Contains(t, attributes, semconv.MessagingSystemKey.String("aws_sns"))
29+
assert.Contains(t, attributes, semconv.MessagingDestinationName("my-topic"))
30+
assert.Contains(t, attributes, semconv.MessagingOperationName("publish_input"))
31+
assert.Contains(t, attributes, semconv.MessagingOperationTypePublish)
32+
}
33+
34+
func TestPublishInputWithNoDestination(t *testing.T) {
35+
input := middleware.InitializeInput{
36+
Parameters: &sns.PublishInput{},
37+
}
38+
39+
attributes := SNSAttributeSetter(context.Background(), input)
40+
41+
assert.Contains(t, attributes, semconv.MessagingSystemKey.String("aws_sns"))
42+
assert.Contains(t, attributes, semconv.MessagingDestinationName(""))
43+
assert.Contains(t, attributes, semconv.MessagingOperationName("publish_input"))
44+
assert.Contains(t, attributes, semconv.MessagingOperationTypePublish)
45+
}
46+
47+
func TestPublishBatchInput(t *testing.T) {
48+
input := middleware.InitializeInput{
49+
Parameters: &sns.PublishBatchInput{
50+
TopicArn: aws.String("arn:aws:sns:us-east-2:444455556666:my-topic-batch"),
51+
PublishBatchRequestEntries: []types.PublishBatchRequestEntry{},
52+
},
53+
}
54+
55+
attributes := SNSAttributeSetter(context.Background(), input)
56+
57+
assert.Contains(t, attributes, semconv.MessagingSystemKey.String("aws_sns"))
58+
assert.Contains(t, attributes, semconv.MessagingDestinationName("my-topic-batch"))
59+
assert.Contains(t, attributes, semconv.MessagingOperationName("publish_batch_input"))
60+
assert.Contains(t, attributes, semconv.MessagingOperationTypePublish)
61+
assert.Contains(t, attributes, semconv.MessagingBatchMessageCount(0))
62+
}

0 commit comments

Comments
 (0)