| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelaws // import "go.opentelemetry.io/contrib/instrumentation/github.com/aws/aws-sdk-go-v2/otelaws" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"encoding/json" |
| 9 |
|
| 10 |
"github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 11 |
"github.com/aws/smithy-go/middleware" |
| 12 |
"go.opentelemetry.io/otel/attribute" |
| 13 |
semconv "go.opentelemetry.io/otel/semconv/v1.40.0" |
| 14 |
) |
| 15 |
|
| 16 |
// DynamoDBAttributeBuilder sets DynamoDB specific attributes depending on the DynamoDB operation being performed. |
| 17 |
func DynamoDBAttributeBuilder(_ context.Context, in middleware.InitializeInput, _ middleware.InitializeOutput) []attribute.KeyValue { |
| 18 |
dynamodbAttributes := []attribute.KeyValue{semconv.DBSystemNameAWSDynamoDB} |
| 19 |
|
| 20 |
switch v := in.Parameters.(type) { |
| 21 |
case *dynamodb.GetItemInput: |
| 22 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 23 |
|
| 24 |
if v.ConsistentRead != nil { |
| 25 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentRead(*v.ConsistentRead)) |
| 26 |
} |
| 27 |
|
| 28 |
if v.ProjectionExpression != nil { |
| 29 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjection(*v.ProjectionExpression)) |
| 30 |
} |
| 31 |
|
| 32 |
case *dynamodb.BatchGetItemInput: |
| 33 |
tableNames := make([]string, 0, len(v.RequestItems)) |
| 34 |
for k := range v.RequestItems { |
| 35 |
tableNames = append(tableNames, k) |
| 36 |
} |
| 37 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(tableNames...)) |
| 38 |
|
| 39 |
case *dynamodb.BatchWriteItemInput: |
| 40 |
tableNames := make([]string, 0, len(v.RequestItems)) |
| 41 |
for k := range v.RequestItems { |
| 42 |
tableNames = append(tableNames, k) |
| 43 |
} |
| 44 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(tableNames...)) |
| 45 |
|
| 46 |
case *dynamodb.CreateTableInput: |
| 47 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 48 |
|
| 49 |
if v.GlobalSecondaryIndexes != nil { |
| 50 |
idx := make([]string, 0, len(v.GlobalSecondaryIndexes)) |
| 51 |
for _, gsi := range v.GlobalSecondaryIndexes { |
| 52 |
i, _ := json.Marshal(gsi) |
| 53 |
idx = append(idx, string(i)) |
| 54 |
} |
| 55 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBGlobalSecondaryIndexes(idx...)) |
| 56 |
} |
| 57 |
|
| 58 |
if v.LocalSecondaryIndexes != nil { |
| 59 |
idx := make([]string, 0, len(v.LocalSecondaryIndexes)) |
| 60 |
for _, lsi := range v.LocalSecondaryIndexes { |
| 61 |
i, _ := json.Marshal(lsi) |
| 62 |
idx = append(idx, string(i)) |
| 63 |
} |
| 64 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLocalSecondaryIndexes(idx...)) |
| 65 |
} |
| 66 |
|
| 67 |
if v.ProvisionedThroughput != nil { |
| 68 |
read := float64(*v.ProvisionedThroughput.ReadCapacityUnits) |
| 69 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedReadCapacity(read)) |
| 70 |
write := float64(*v.ProvisionedThroughput.WriteCapacityUnits) |
| 71 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacity(write)) |
| 72 |
} |
| 73 |
|
| 74 |
case *dynamodb.DeleteItemInput: |
| 75 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 76 |
|
| 77 |
case *dynamodb.DeleteTableInput: |
| 78 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 79 |
|
| 80 |
case *dynamodb.DescribeTableInput: |
| 81 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 82 |
|
| 83 |
case *dynamodb.ListTablesInput: |
| 84 |
if v.ExclusiveStartTableName != nil { |
| 85 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBExclusiveStartTable(*v.ExclusiveStartTableName)) |
| 86 |
} |
| 87 |
|
| 88 |
if v.Limit != nil { |
| 89 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimit(int(*v.Limit))) |
| 90 |
} |
| 91 |
|
| 92 |
case *dynamodb.PutItemInput: |
| 93 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 94 |
|
| 95 |
case *dynamodb.QueryInput: |
| 96 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 97 |
|
| 98 |
if v.ConsistentRead != nil { |
| 99 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentRead(*v.ConsistentRead)) |
| 100 |
} |
| 101 |
|
| 102 |
if v.IndexName != nil { |
| 103 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBIndexName(*v.IndexName)) |
| 104 |
} |
| 105 |
|
| 106 |
if v.Limit != nil { |
| 107 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimit(int(*v.Limit))) |
| 108 |
} |
| 109 |
|
| 110 |
if v.ScanIndexForward != nil { |
| 111 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBScanForward(*v.ScanIndexForward)) |
| 112 |
} |
| 113 |
|
| 114 |
if v.ProjectionExpression != nil { |
| 115 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjection(*v.ProjectionExpression)) |
| 116 |
} |
| 117 |
|
| 118 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSelect(string(v.Select))) |
| 119 |
|
| 120 |
case *dynamodb.ScanInput: |
| 121 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 122 |
|
| 123 |
if v.ConsistentRead != nil { |
| 124 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentRead(*v.ConsistentRead)) |
| 125 |
} |
| 126 |
|
| 127 |
if v.IndexName != nil { |
| 128 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBIndexName(*v.IndexName)) |
| 129 |
} |
| 130 |
|
| 131 |
if v.Limit != nil { |
| 132 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimit(int(*v.Limit))) |
| 133 |
} |
| 134 |
|
| 135 |
if v.ProjectionExpression != nil { |
| 136 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjection(*v.ProjectionExpression)) |
| 137 |
} |
| 138 |
|
| 139 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSelect(string(v.Select))) |
| 140 |
|
| 141 |
if v.Segment != nil { |
| 142 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSegment(int(*v.Segment))) |
| 143 |
} |
| 144 |
|
| 145 |
if v.TotalSegments != nil { |
| 146 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTotalSegments(int(*v.TotalSegments))) |
| 147 |
} |
| 148 |
|
| 149 |
case *dynamodb.UpdateItemInput: |
| 150 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 151 |
|
| 152 |
case *dynamodb.UpdateTableInput: |
| 153 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNames(*v.TableName)) |
| 154 |
|
| 155 |
if v.AttributeDefinitions != nil { |
| 156 |
def := make([]string, 0, len(v.AttributeDefinitions)) |
| 157 |
for _, ad := range v.AttributeDefinitions { |
| 158 |
d, _ := json.Marshal(ad) |
| 159 |
def = append(def, string(d)) |
| 160 |
} |
| 161 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBAttributeDefinitions(def...)) |
| 162 |
} |
| 163 |
|
| 164 |
if v.GlobalSecondaryIndexUpdates != nil { |
| 165 |
idx := make([]string, 0, len(v.GlobalSecondaryIndexUpdates)) |
| 166 |
for _, gsiu := range v.GlobalSecondaryIndexUpdates { |
| 167 |
i, _ := json.Marshal(gsiu) |
| 168 |
idx = append(idx, string(i)) |
| 169 |
} |
| 170 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBGlobalSecondaryIndexUpdates(idx...)) |
| 171 |
} |
| 172 |
|
| 173 |
if v.ProvisionedThroughput != nil { |
| 174 |
read := float64(*v.ProvisionedThroughput.ReadCapacityUnits) |
| 175 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedReadCapacity(read)) |
| 176 |
write := float64(*v.ProvisionedThroughput.WriteCapacityUnits) |
| 177 |
dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacity(write)) |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return dynamodbAttributes |
| 182 |
} |