-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
1 parent
3af31bd
commit a761314
Showing
12 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/snsattributes.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 "" | ||
} |
62 changes: 62 additions & 0 deletions
62
instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/snsattributes_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package otelaws | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/sns" | ||
"github.com/aws/aws-sdk-go-v2/service/sns/types" | ||
"github.com/aws/smithy-go/middleware" | ||
"github.com/stretchr/testify/assert" | ||
|
||
semconv "go.opentelemetry.io/otel/semconv/v1.27.0" | ||
) | ||
|
||
func TestPublishInput(t *testing.T) { | ||
input := middleware.InitializeInput{ | ||
Parameters: &sns.PublishInput{ | ||
TopicArn: aws.String("arn:aws:sns:us-east-2:444455556666:my-topic"), | ||
}, | ||
} | ||
|
||
attributes := SNSAttributeSetter(context.Background(), input) | ||
|
||
assert.Contains(t, attributes, semconv.MessagingSystemKey.String("aws_sns")) | ||
assert.Contains(t, attributes, semconv.MessagingDestinationName("my-topic")) | ||
assert.Contains(t, attributes, semconv.MessagingOperationName("publish_input")) | ||
assert.Contains(t, attributes, semconv.MessagingOperationTypePublish) | ||
} | ||
|
||
func TestPublishInputWithNoDestination(t *testing.T) { | ||
input := middleware.InitializeInput{ | ||
Parameters: &sns.PublishInput{}, | ||
} | ||
|
||
attributes := SNSAttributeSetter(context.Background(), input) | ||
|
||
assert.Contains(t, attributes, semconv.MessagingSystemKey.String("aws_sns")) | ||
assert.Contains(t, attributes, semconv.MessagingDestinationName("")) | ||
assert.Contains(t, attributes, semconv.MessagingOperationName("publish_input")) | ||
assert.Contains(t, attributes, semconv.MessagingOperationTypePublish) | ||
} | ||
|
||
func TestPublishBatchInput(t *testing.T) { | ||
input := middleware.InitializeInput{ | ||
Parameters: &sns.PublishBatchInput{ | ||
TopicArn: aws.String("arn:aws:sns:us-east-2:444455556666:my-topic-batch"), | ||
PublishBatchRequestEntries: []types.PublishBatchRequestEntry{}, | ||
}, | ||
} | ||
|
||
attributes := SNSAttributeSetter(context.Background(), input) | ||
|
||
assert.Contains(t, attributes, semconv.MessagingSystemKey.String("aws_sns")) | ||
assert.Contains(t, attributes, semconv.MessagingDestinationName("my-topic-batch")) | ||
assert.Contains(t, attributes, semconv.MessagingOperationName("publish_batch_input")) | ||
assert.Contains(t, attributes, semconv.MessagingOperationTypePublish) | ||
assert.Contains(t, attributes, semconv.MessagingBatchMessageCount(0)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters