| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelaws |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
awsMiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" |
| 10 |
"github.com/aws/aws-sdk-go-v2/service/sqs" |
| 11 |
"github.com/aws/smithy-go/middleware" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"go.opentelemetry.io/otel/attribute" |
| 14 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 15 |
) |
| 16 |
|
| 17 |
func TestOperationAttr(t *testing.T) { |
| 18 |
operation := "test-operation" |
| 19 |
attr := OperationAttr(operation) |
| 20 |
assert.Equal(t, attribute.String("rpc.method", operation), attr) |
| 21 |
} |
| 22 |
|
| 23 |
func TestRegionAttr(t *testing.T) { |
| 24 |
region := "test-region" |
| 25 |
attr := RegionAttr(region) |
| 26 |
assert.Equal(t, attribute.String("aws.region", region), attr) |
| 27 |
} |
| 28 |
|
| 29 |
func TestServiceAttr(t *testing.T) { |
| 30 |
service := "test-service" |
| 31 |
attr := ServiceAttr(service) |
| 32 |
assert.Equal(t, semconv.RPCMethod(service), attr) |
| 33 |
} |
| 34 |
|
| 35 |
func TestRequestIDAttr(t *testing.T) { |
| 36 |
requestID := "test-request-id" |
| 37 |
attr := RequestIDAttr(requestID) |
| 38 |
assert.Equal(t, attribute.String("aws.request_id", requestID), attr) |
| 39 |
} |
| 40 |
|
| 41 |
func TestSystemAttribute(t *testing.T) { |
| 42 |
attr := SystemAttr() |
| 43 |
assert.Equal(t, semconv.RPCSystemNameKey.String("aws-api"), attr) |
| 44 |
} |
| 45 |
|
| 46 |
func TestMethodAttr(t *testing.T) { |
| 47 |
tests := []struct { |
| 48 |
name string |
| 49 |
service string |
| 50 |
operation string |
| 51 |
want attribute.KeyValue |
| 52 |
}{ |
| 53 |
{ |
| 54 |
name: "both service and operation", |
| 55 |
service: "DynamoDB", |
| 56 |
operation: "GetItem", |
| 57 |
want: attribute.String("rpc.method", "DynamoDB/GetItem"), |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "service only", |
| 61 |
service: "Route 53", |
| 62 |
operation: "", |
| 63 |
want: attribute.String("rpc.method", "Route 53"), |
| 64 |
}, |
| 65 |
{ |
| 66 |
name: "operation only", |
| 67 |
service: "", |
| 68 |
operation: "DescribeInstances", |
| 69 |
want: attribute.String("rpc.method", "DescribeInstances"), |
| 70 |
}, |
| 71 |
{ |
| 72 |
name: "both empty", |
| 73 |
service: "", |
| 74 |
operation: "", |
| 75 |
want: attribute.String("rpc.method", ""), |
| 76 |
}, |
| 77 |
} |
| 78 |
for _, tt := range tests { |
| 79 |
t.Run(tt.name, func(t *testing.T) { |
| 80 |
attr := MethodAttr(tt.service, tt.operation) |
| 81 |
assert.Equal(t, tt.want, attr) |
| 82 |
}) |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
func TestDefaultAttributeBuilderNotSupportedService(t *testing.T) { |
| 87 |
testCtx := awsMiddleware.SetServiceID(t.Context(), "not-implemented-service") |
| 88 |
|
| 89 |
attr := DefaultAttributeBuilder(testCtx, middleware.InitializeInput{}, middleware.InitializeOutput{}) |
| 90 |
assert.Empty(t, attr) |
| 91 |
} |
| 92 |
|
| 93 |
func TestDefaultAttributeBuilderOnSupportedService(t *testing.T) { |
| 94 |
testCtx := awsMiddleware.SetServiceID(t.Context(), sqs.ServiceID) |
| 95 |
testQueueURL := "test-queue-url" |
| 96 |
|
| 97 |
attr := DefaultAttributeBuilder(testCtx, middleware.InitializeInput{ |
| 98 |
Parameters: &sqs.SendMessageInput{ |
| 99 |
QueueUrl: &testQueueURL, |
| 100 |
}, |
| 101 |
}, middleware.InitializeOutput{}) |
| 102 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 103 |
semconv.MessagingSystemAWSSQS, |
| 104 |
semconv.ServerAddress(testQueueURL), |
| 105 |
}, attr) |
| 106 |
} |