Debian Codegraph @

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

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelaws_test
5
6 import (
7 "context"
8 "net/http"
9 "net/http/httptest"
10 "testing"
11
12 "github.com/aws/aws-sdk-go-v2/aws"
13 "github.com/aws/aws-sdk-go-v2/service/dynamodb"
14 dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
15 smithyauth "github.com/aws/smithy-go/auth"
16 "github.com/aws/smithy-go/middleware"
17 smithyhttp "github.com/aws/smithy-go/transport/http"
18 "github.com/stretchr/testify/assert"
19 "github.com/stretchr/testify/require"
20 "go.opentelemetry.io/otel/attribute"
21 "go.opentelemetry.io/otel/codes"
22 sdktrace "go.opentelemetry.io/otel/sdk/trace"
23 "go.opentelemetry.io/otel/sdk/trace/tracetest"
24 "go.opentelemetry.io/otel/trace"
25
26 "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws"
27 )
28
29 type dynamoDBAuthResolver struct{}
30
31 func (*dynamoDBAuthResolver) ResolveAuthSchemes(context.Context, *dynamodb.AuthResolverParameters) ([]*smithyauth.Option, error) {
32 return []*smithyauth.Option{
33 {SchemeID: smithyauth.SchemeIDAnonymous},
34 }, nil
35 }
36
37 func TestDynamodbTags(t *testing.T) {
38 cases := struct {
39 responseStatus int
40 expectedRegion string
41 expectedStatusCode int
42 expectedError codes.Code
43 }{
44 responseStatus: http.StatusOK,
45 expectedRegion: "us-west-2",
46 expectedStatusCode: http.StatusOK,
47 }
48
49 server := httptest.NewServer(http.HandlerFunc(
50 func(w http.ResponseWriter, _ *http.Request) {
51 w.WriteHeader(cases.responseStatus)
52 }))
53 defer server.Close()
54
55 t.Run("dynamodb tags", func(t *testing.T) {
56 sr := tracetest.NewSpanRecorder()
57 provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
58
59 svc := dynamodb.New(dynamodb.Options{
60 Region: cases.expectedRegion,
61 BaseEndpoint: &server.URL,
62 AuthSchemeResolver: &dynamoDBAuthResolver{},
63 AuthSchemes: []smithyhttp.AuthScheme{
64 smithyhttp.NewAnonymousScheme(),
65 },
66 Retryer: aws.NopRetryer{},
67 })
68
69 _, err := svc.GetItem(t.Context(), &dynamodb.GetItemInput{
70 TableName: aws.String("table1"),
71 ConsistentRead: aws.Bool(false),
72 ProjectionExpression: aws.String("test"),
73 Key: map[string]dtypes.AttributeValue{
74 "id": &dtypes.AttributeValueMemberS{Value: "test"},
75 },
76 }, func(options *dynamodb.Options) {
77 otelaws.AppendMiddlewares(
78 &options.APIOptions, otelaws.WithAttributeBuilder(otelaws.DynamoDBAttributeBuilder), otelaws.WithTracerProvider(provider))
79 })
80
81 if cases.expectedError == codes.Unset {
82 assert.NoError(t, err)
83 } else {
84 assert.Error(t, err)
85 }
86
87 spans := sr.Ended()
88 require.Len(t, spans, 1)
89 span := spans[0]
90
91 assert.Equal(t, "DynamoDB.GetItem", span.Name())
92 assert.Equal(t, trace.SpanKindClient, span.SpanKind())
93 attrs := span.Attributes()
94 assert.Contains(t, attrs, attribute.Int("http.response.status_code", cases.expectedStatusCode))
95 assert.Contains(t, attrs, attribute.String("aws.region", cases.expectedRegion))
96 assert.Contains(t, attrs, attribute.String("rpc.method", "DynamoDB/GetItem"))
97 assert.Contains(t, attrs, attribute.String("rpc.system.name", "aws-api"))
98 assert.Contains(t, attrs, attribute.StringSlice(
99 "aws.dynamodb.table_names", []string{"table1"},
100 ))
101 assert.Contains(t, attrs, attribute.String("aws.dynamodb.projection", "test"))
102 assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false))
103 })
104 }
105
106 func TestDynamodbTagsCustomBuilder(t *testing.T) {
107 cases := struct {
108 responseStatus int
109 expectedRegion string
110 expectedStatusCode int
111 expectedError codes.Code
112 }{
113 responseStatus: http.StatusOK,
114 expectedRegion: "us-west-2",
115 expectedStatusCode: http.StatusOK,
116 }
117
118 server := httptest.NewServer(http.HandlerFunc(
119 func(w http.ResponseWriter, _ *http.Request) {
120 w.WriteHeader(cases.responseStatus)
121 }))
122 defer server.Close()
123
124 t.Run("dynamodb tags", func(t *testing.T) {
125 sr := tracetest.NewSpanRecorder()
126 provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))
127
128 svc := dynamodb.New(dynamodb.Options{
129 Region: cases.expectedRegion,
130 BaseEndpoint: &server.URL,
131 AuthSchemeResolver: &dynamoDBAuthResolver{},
132 Retryer: aws.NopRetryer{},
133 })
134
135 mycustomsetter := otelaws.AttributeBuilder(func(context.Context, middleware.InitializeInput, middleware.InitializeOutput) []attribute.KeyValue {
136 customAttributes := []attribute.KeyValue{
137 {
138 Key: "customattribute2key",
139 Value: attribute.StringValue("customattribute2value"),
140 },
141 {
142 Key: "customattribute1key",
143 Value: attribute.StringValue("customattribute1value"),
144 },
145 }
146
147 return customAttributes
148 })
149
150 _, err := svc.GetItem(t.Context(), &dynamodb.GetItemInput{
151 TableName: aws.String("table1"),
152 ConsistentRead: aws.Bool(false),
153 ProjectionExpression: aws.String("test"),
154 Key: map[string]dtypes.AttributeValue{
155 "id": &dtypes.AttributeValueMemberS{Value: "test"},
156 },
157 }, func(options *dynamodb.Options) {
158 otelaws.AppendMiddlewares(
159 &options.APIOptions, otelaws.WithAttributeBuilder(otelaws.DynamoDBAttributeBuilder, mycustomsetter), otelaws.WithTracerProvider(provider))
160 })
161
162 if cases.expectedError == codes.Unset {
163 assert.NoError(t, err)
164 } else {
165 assert.Error(t, err)
166 }
167
168 spans := sr.Ended()
169 require.Len(t, spans, 1)
170 span := spans[0]
171
172 assert.Equal(t, "DynamoDB.GetItem", span.Name())
173 assert.Equal(t, trace.SpanKindClient, span.SpanKind())
174 attrs := span.Attributes()
175 assert.Contains(t, attrs, attribute.Int("http.response.status_code", cases.expectedStatusCode))
176 assert.Contains(t, attrs, attribute.String("aws.region", cases.expectedRegion))
177 assert.Contains(t, attrs, attribute.String("rpc.method", "DynamoDB/GetItem"))
178 assert.Contains(t, attrs, attribute.StringSlice(
179 "aws.dynamodb.table_names", []string{"table1"},
180 ))
181 assert.Contains(t, attrs, attribute.String("aws.dynamodb.projection", "test"))
182 assert.Contains(t, attrs, attribute.Bool("aws.dynamodb.consistent_read", false))
183 assert.Contains(t, attrs, attribute.String("customattribute2key", "customattribute2value"))
184 assert.Contains(t, attrs, attribute.String("customattribute1key", "customattribute1value"))
185 })
186 }