-
Notifications
You must be signed in to change notification settings - Fork 576
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
feat(otelaws): also instrument PublishBatchInput doc: add entry to changelog chore: sns package indirect Update instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/snsattributes.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Update instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/snsattributes.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Update instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/snsattributes.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> revert - Update CHANGELOG.md chore: add missin assertions and use context background in tests chore: add test for input wihtout destination chore(build): fix conflicts
- Loading branch information
1 parent
04815fd
commit 337ad38
Showing
11 changed files
with
122 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
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