| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelaws |
| 5 |
|
| 6 |
import ( |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/aws/aws-sdk-go-v2/aws" |
| 10 |
"github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 11 |
dtypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types" |
| 12 |
"github.com/aws/smithy-go/middleware" |
| 13 |
"github.com/stretchr/testify/assert" |
| 14 |
"go.opentelemetry.io/otel/attribute" |
| 15 |
) |
| 16 |
|
| 17 |
func TestDynamodbTagsBatchGetItemInput(t *testing.T) { |
| 18 |
input := middleware.InitializeInput{ |
| 19 |
Parameters: &dynamodb.BatchGetItemInput{ |
| 20 |
RequestItems: map[string]dtypes.KeysAndAttributes{ |
| 21 |
"table1": { |
| 22 |
Keys: []map[string]dtypes.AttributeValue{ |
| 23 |
{ |
| 24 |
"id": &dtypes.AttributeValueMemberS{Value: "123"}, |
| 25 |
}, |
| 26 |
}, |
| 27 |
}, |
| 28 |
}, |
| 29 |
}, |
| 30 |
} |
| 31 |
|
| 32 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 33 |
|
| 34 |
assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) |
| 35 |
} |
| 36 |
|
| 37 |
func TestDynamodbTagsBatchWriteItemInput(t *testing.T) { |
| 38 |
input := middleware.InitializeInput{ |
| 39 |
Parameters: &dynamodb.BatchWriteItemInput{ |
| 40 |
RequestItems: map[string][]dtypes.WriteRequest{ |
| 41 |
"table1": { |
| 42 |
{ |
| 43 |
DeleteRequest: &dtypes.DeleteRequest{ |
| 44 |
Key: map[string]dtypes.AttributeValue{ |
| 45 |
"id": &dtypes.AttributeValueMemberS{Value: "123"}, |
| 46 |
}, |
| 47 |
}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
PutRequest: &dtypes.PutRequest{ |
| 51 |
Item: map[string]dtypes.AttributeValue{ |
| 52 |
"id": &dtypes.AttributeValueMemberS{Value: "234"}, |
| 53 |
}, |
| 54 |
}, |
| 55 |
}, |
| 56 |
}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
} |
| 60 |
|
| 61 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 62 |
|
| 63 |
assert.Contains(t, attributes, attribute.StringSlice("aws.dynamodb.table_names", []string{"table1"})) |
| 64 |
} |
| 65 |
|
| 66 |
func TestDynamodbTagsCreateTableInput(t *testing.T) { |
| 67 |
input := middleware.InitializeInput{ |
| 68 |
Parameters: &dynamodb.CreateTableInput{ |
| 69 |
AttributeDefinitions: []dtypes.AttributeDefinition{ |
| 70 |
{ |
| 71 |
AttributeName: aws.String("id"), |
| 72 |
AttributeType: dtypes.ScalarAttributeTypeS, |
| 73 |
}, |
| 74 |
}, |
| 75 |
KeySchema: []dtypes.KeySchemaElement{ |
| 76 |
{ |
| 77 |
AttributeName: aws.String("id"), |
| 78 |
KeyType: dtypes.KeyTypeHash, |
| 79 |
}, |
| 80 |
}, |
| 81 |
TableName: aws.String("table1"), |
| 82 |
BillingMode: dtypes.BillingModePayPerRequest, |
| 83 |
ProvisionedThroughput: &dtypes.ProvisionedThroughput{ |
| 84 |
ReadCapacityUnits: aws.Int64(123), |
| 85 |
WriteCapacityUnits: aws.Int64(456), |
| 86 |
}, |
| 87 |
GlobalSecondaryIndexes: []dtypes.GlobalSecondaryIndex{ |
| 88 |
{ |
| 89 |
IndexName: aws.String("index1"), |
| 90 |
KeySchema: []dtypes.KeySchemaElement{ |
| 91 |
{ |
| 92 |
AttributeName: aws.String("attributename"), |
| 93 |
KeyType: dtypes.KeyTypeHash, |
| 94 |
}, |
| 95 |
}, |
| 96 |
Projection: &dtypes.Projection{ |
| 97 |
NonKeyAttributes: []string{"non-key-attributes"}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
LocalSecondaryIndexes: []dtypes.LocalSecondaryIndex{ |
| 102 |
{ |
| 103 |
IndexName: aws.String("index2"), |
| 104 |
KeySchema: []dtypes.KeySchemaElement{ |
| 105 |
{ |
| 106 |
AttributeName: aws.String("attributename"), |
| 107 |
KeyType: dtypes.KeyTypeHash, |
| 108 |
}, |
| 109 |
}, |
| 110 |
}, |
| 111 |
}, |
| 112 |
}, |
| 113 |
} |
| 114 |
|
| 115 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 116 |
|
| 117 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 118 |
"aws.dynamodb.table_names", []string{"table1"}, |
| 119 |
)) |
| 120 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 121 |
"aws.dynamodb.global_secondary_indexes", |
| 122 |
[]string{ |
| 123 |
`{"IndexName":"index1","KeySchema":[{"AttributeName":"attributename","KeyType":"HASH"}],"Projection":{"NonKeyAttributes":["non-key-attributes"],"ProjectionType":""},"OnDemandThroughput":null,"ProvisionedThroughput":null,"WarmThroughput":null}`, |
| 124 |
}, |
| 125 |
)) |
| 126 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 127 |
"aws.dynamodb.local_secondary_indexes", |
| 128 |
[]string{ |
| 129 |
`{"IndexName":"index2","KeySchema":[{"AttributeName":"attributename","KeyType":"HASH"}],"Projection":null}`, |
| 130 |
}, |
| 131 |
)) |
| 132 |
assert.Contains(t, attributes, attribute.Float64("aws.dynamodb.provisioned_read_capacity", 123)) |
| 133 |
assert.Contains(t, attributes, attribute.Float64("aws.dynamodb.provisioned_write_capacity", 456)) |
| 134 |
} |
| 135 |
|
| 136 |
func TestDynamodbTagsDeleteItemInput(t *testing.T) { |
| 137 |
input := middleware.InitializeInput{ |
| 138 |
Parameters: &dynamodb.DeleteItemInput{ |
| 139 |
Key: map[string]dtypes.AttributeValue{ |
| 140 |
"id": &dtypes.AttributeValueMemberS{Value: "123"}, |
| 141 |
}, |
| 142 |
TableName: aws.String("table1"), |
| 143 |
}, |
| 144 |
} |
| 145 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 146 |
|
| 147 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 148 |
"aws.dynamodb.table_names", []string{"table1"}, |
| 149 |
)) |
| 150 |
} |
| 151 |
|
| 152 |
func TestDynamodbTagsDeleteTableInput(t *testing.T) { |
| 153 |
input := middleware.InitializeInput{ |
| 154 |
Parameters: &dynamodb.DeleteTableInput{ |
| 155 |
TableName: aws.String("table1"), |
| 156 |
}, |
| 157 |
} |
| 158 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 159 |
|
| 160 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 161 |
"aws.dynamodb.table_names", []string{"table1"}, |
| 162 |
)) |
| 163 |
} |
| 164 |
|
| 165 |
func TestDynamodbTagsDescribeTableInput(t *testing.T) { |
| 166 |
input := middleware.InitializeInput{ |
| 167 |
Parameters: &dynamodb.DescribeTableInput{ |
| 168 |
TableName: aws.String("table1"), |
| 169 |
}, |
| 170 |
} |
| 171 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 172 |
|
| 173 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 174 |
"aws.dynamodb.table_names", []string{"table1"}, |
| 175 |
)) |
| 176 |
} |
| 177 |
|
| 178 |
func TestDynamodbTagsListTablesInput(t *testing.T) { |
| 179 |
input := middleware.InitializeInput{ |
| 180 |
Parameters: &dynamodb.ListTablesInput{ |
| 181 |
ExclusiveStartTableName: aws.String("table1"), |
| 182 |
Limit: aws.Int32(10), |
| 183 |
}, |
| 184 |
} |
| 185 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 186 |
|
| 187 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.exclusive_start_table", "table1")) |
| 188 |
assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) |
| 189 |
} |
| 190 |
|
| 191 |
func TestDynamodbTagsPutItemInput(t *testing.T) { |
| 192 |
input := middleware.InitializeInput{ |
| 193 |
Parameters: &dynamodb.PutItemInput{ |
| 194 |
TableName: aws.String("table1"), |
| 195 |
Item: map[string]dtypes.AttributeValue{ |
| 196 |
"id": &dtypes.AttributeValueMemberS{Value: "12346"}, |
| 197 |
"name": &dtypes.AttributeValueMemberS{Value: "John Doe"}, |
| 198 |
"email": &dtypes.AttributeValueMemberS{Value: "[email protected]"}, |
| 199 |
}, |
| 200 |
}, |
| 201 |
} |
| 202 |
|
| 203 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 204 |
|
| 205 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 206 |
"aws.dynamodb.table_names", []string{"table1"}, |
| 207 |
)) |
| 208 |
} |
| 209 |
|
| 210 |
func TestDynamodbTagsQueryInput(t *testing.T) { |
| 211 |
input := middleware.InitializeInput{ |
| 212 |
Parameters: &dynamodb.QueryInput{ |
| 213 |
TableName: aws.String("table1"), |
| 214 |
IndexName: aws.String("index1"), |
| 215 |
ConsistentRead: aws.Bool(true), |
| 216 |
Limit: aws.Int32(10), |
| 217 |
ScanIndexForward: aws.Bool(true), |
| 218 |
ProjectionExpression: aws.String("projectionexpression"), |
| 219 |
Select: dtypes.SelectAllAttributes, |
| 220 |
KeyConditionExpression: aws.String("id = :hashKey and #date > :rangeKey"), |
| 221 |
ExpressionAttributeNames: map[string]string{ |
| 222 |
"#date": "date", |
| 223 |
}, |
| 224 |
ExpressionAttributeValues: map[string]dtypes.AttributeValue{ |
| 225 |
":hashKey": &dtypes.AttributeValueMemberS{Value: "123"}, |
| 226 |
":rangeKey": &dtypes.AttributeValueMemberN{Value: "20150101"}, |
| 227 |
}, |
| 228 |
}, |
| 229 |
} |
| 230 |
|
| 231 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 232 |
|
| 233 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 234 |
"aws.dynamodb.table_names", []string{"table1"}, |
| 235 |
)) |
| 236 |
assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) |
| 237 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) |
| 238 |
assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) |
| 239 |
assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.scan_forward", true)) |
| 240 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "projectionexpression")) |
| 241 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) |
| 242 |
} |
| 243 |
|
| 244 |
func TestDynamodbTagsScanInput(t *testing.T) { |
| 245 |
input := middleware.InitializeInput{ |
| 246 |
Parameters: &dynamodb.ScanInput{ |
| 247 |
TableName: aws.String("my-table"), |
| 248 |
ConsistentRead: aws.Bool(true), |
| 249 |
IndexName: aws.String("index1"), |
| 250 |
Limit: aws.Int32(10), |
| 251 |
ProjectionExpression: aws.String("Artist, Genre"), |
| 252 |
Segment: aws.Int32(10), |
| 253 |
TotalSegments: aws.Int32(100), |
| 254 |
Select: dtypes.SelectAllAttributes, |
| 255 |
}, |
| 256 |
} |
| 257 |
|
| 258 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 259 |
|
| 260 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 261 |
"aws.dynamodb.table_names", []string{"my-table"}, |
| 262 |
)) |
| 263 |
assert.Contains(t, attributes, attribute.Bool("aws.dynamodb.consistent_read", true)) |
| 264 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.index_name", "index1")) |
| 265 |
assert.Contains(t, attributes, attribute.Int("aws.dynamodb.limit", 10)) |
| 266 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.select", "ALL_ATTRIBUTES")) |
| 267 |
assert.Contains(t, attributes, attribute.Int("aws.dynamodb.total_segments", 100)) |
| 268 |
assert.Contains(t, attributes, attribute.Int("aws.dynamodb.segment", 10)) |
| 269 |
assert.Contains(t, attributes, attribute.String("aws.dynamodb.projection", "Artist, Genre")) |
| 270 |
} |
| 271 |
|
| 272 |
func TestDynamodbTagsUpdateItemInput(t *testing.T) { |
| 273 |
input := middleware.InitializeInput{ |
| 274 |
Parameters: &dynamodb.UpdateItemInput{ |
| 275 |
TableName: aws.String("my-table"), |
| 276 |
Key: map[string]dtypes.AttributeValue{ |
| 277 |
"id": &dtypes.AttributeValueMemberS{Value: "123"}, |
| 278 |
}, |
| 279 |
UpdateExpression: aws.String("set firstName = :firstName"), |
| 280 |
ExpressionAttributeValues: map[string]dtypes.AttributeValue{ |
| 281 |
":firstName": &dtypes.AttributeValueMemberS{Value: "John McNewname"}, |
| 282 |
}, |
| 283 |
}, |
| 284 |
} |
| 285 |
|
| 286 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 287 |
|
| 288 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 289 |
"aws.dynamodb.table_names", []string{"my-table"}, |
| 290 |
)) |
| 291 |
} |
| 292 |
|
| 293 |
func TestDynamodbTagsUpdateTableInput(t *testing.T) { |
| 294 |
input := middleware.InitializeInput{ |
| 295 |
Parameters: &dynamodb.UpdateTableInput{ |
| 296 |
TableName: aws.String("my-table"), |
| 297 |
AttributeDefinitions: []dtypes.AttributeDefinition{ |
| 298 |
{ |
| 299 |
AttributeName: aws.String("id"), |
| 300 |
AttributeType: dtypes.ScalarAttributeTypeS, |
| 301 |
}, |
| 302 |
}, |
| 303 |
GlobalSecondaryIndexUpdates: []dtypes.GlobalSecondaryIndexUpdate{ |
| 304 |
{ |
| 305 |
Create: &dtypes.CreateGlobalSecondaryIndexAction{ |
| 306 |
IndexName: aws.String("index1"), |
| 307 |
KeySchema: []dtypes.KeySchemaElement{ |
| 308 |
{ |
| 309 |
AttributeName: aws.String("attribute"), |
| 310 |
KeyType: dtypes.KeyTypeHash, |
| 311 |
}, |
| 312 |
}, |
| 313 |
Projection: &dtypes.Projection{ |
| 314 |
NonKeyAttributes: []string{"attribute1", "attribute2"}, |
| 315 |
ProjectionType: dtypes.ProjectionTypeAll, |
| 316 |
}, |
| 317 |
}, |
| 318 |
}, |
| 319 |
}, |
| 320 |
ProvisionedThroughput: &dtypes.ProvisionedThroughput{ |
| 321 |
ReadCapacityUnits: aws.Int64(123), |
| 322 |
WriteCapacityUnits: aws.Int64(456), |
| 323 |
}, |
| 324 |
}, |
| 325 |
} |
| 326 |
|
| 327 |
attributes := DynamoDBAttributeBuilder(t.Context(), input, middleware.InitializeOutput{}) |
| 328 |
|
| 329 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 330 |
"aws.dynamodb.table_names", []string{"my-table"}, |
| 331 |
)) |
| 332 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 333 |
"aws.dynamodb.attribute_definitions", |
| 334 |
[]string{`{"AttributeName":"id","AttributeType":"S"}`}, |
| 335 |
)) |
| 336 |
assert.Contains(t, attributes, attribute.StringSlice( |
| 337 |
"aws.dynamodb.global_secondary_index_updates", |
| 338 |
[]string{ |
| 339 |
`{"Create":{"IndexName":"index1","KeySchema":[{"AttributeName":"attribute","KeyType":"HASH"}],"Projection":{"NonKeyAttributes":["attribute1","attribute2"],"ProjectionType":"ALL"},"OnDemandThroughput":null,"ProvisionedThroughput":null,"WarmThroughput":null},"Delete":null,"Update":null}`, |
| 340 |
}, |
| 341 |
)) |
| 342 |
assert.Contains(t, attributes, attribute.Float64("aws.dynamodb.provisioned_read_capacity", 123)) |
| 343 |
assert.Contains(t, attributes, attribute.Float64("aws.dynamodb.provisioned_write_capacity", 456)) |
| 344 |
} |