|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package otelaws |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + |
| 21 | + "github.com/aws/aws-sdk-go-v2/service/dynamodb" |
| 22 | + "github.com/aws/smithy-go/middleware" |
| 23 | + |
| 24 | + "go.opentelemetry.io/otel/attribute" |
| 25 | + semconv "go.opentelemetry.io/otel/semconv/v1.7.0" |
| 26 | +) |
| 27 | + |
| 28 | +// DynamoDBAttributeSetter sets DynamoDB specific attributes depending on the the DynamoDB operation being performed. |
| 29 | +func DynamoDBAttributeSetter(ctx context.Context, in middleware.InitializeInput) []attribute.KeyValue { |
| 30 | + |
| 31 | + dynamodbAttributes := []attribute.KeyValue{semconv.DBSystemKey.String("dynamodb")} |
| 32 | + |
| 33 | + switch v := in.Parameters.(type) { |
| 34 | + case *dynamodb.GetItemInput: |
| 35 | + |
| 36 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 37 | + |
| 38 | + if v.ConsistentRead != nil { |
| 39 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentReadKey.Bool(*v.ConsistentRead)) |
| 40 | + } |
| 41 | + |
| 42 | + if v.ProjectionExpression != nil { |
| 43 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) |
| 44 | + } |
| 45 | + |
| 46 | + case *dynamodb.BatchGetItemInput: |
| 47 | + var tableNames []string |
| 48 | + for k := range v.RequestItems { |
| 49 | + tableNames = append(tableNames, k) |
| 50 | + } |
| 51 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) |
| 52 | + |
| 53 | + case *dynamodb.BatchWriteItemInput: |
| 54 | + var tableNames []string |
| 55 | + for k := range v.RequestItems { |
| 56 | + tableNames = append(tableNames, k) |
| 57 | + } |
| 58 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.StringSlice(tableNames)) |
| 59 | + |
| 60 | + case *dynamodb.CreateTableInput: |
| 61 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 62 | + |
| 63 | + if v.GlobalSecondaryIndexes != nil { |
| 64 | + globalindexes, _ := json.Marshal(v.GlobalSecondaryIndexes) |
| 65 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBGlobalSecondaryIndexesKey.String(string(globalindexes))) |
| 66 | + } |
| 67 | + |
| 68 | + if v.LocalSecondaryIndexes != nil { |
| 69 | + localindexes, _ := json.Marshal(v.LocalSecondaryIndexes) |
| 70 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLocalSecondaryIndexesKey.String(string(localindexes))) |
| 71 | + } |
| 72 | + |
| 73 | + if v.ProvisionedThroughput != nil { |
| 74 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedReadCapacityKey.Int64(*v.ProvisionedThroughput.ReadCapacityUnits)) |
| 75 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacityKey.Int64(*v.ProvisionedThroughput.WriteCapacityUnits)) |
| 76 | + } |
| 77 | + |
| 78 | + case *dynamodb.DeleteItemInput: |
| 79 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 80 | + |
| 81 | + case *dynamodb.DeleteTableInput: |
| 82 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 83 | + |
| 84 | + case *dynamodb.DescribeTableInput: |
| 85 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 86 | + |
| 87 | + case *dynamodb.ListTablesInput: |
| 88 | + |
| 89 | + if v.ExclusiveStartTableName != nil { |
| 90 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBExclusiveStartTableKey.String(*v.ExclusiveStartTableName)) |
| 91 | + } |
| 92 | + |
| 93 | + if v.Limit != nil { |
| 94 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) |
| 95 | + } |
| 96 | + |
| 97 | + case *dynamodb.PutItemInput: |
| 98 | + |
| 99 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 100 | + |
| 101 | + case *dynamodb.QueryInput: |
| 102 | + |
| 103 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 104 | + |
| 105 | + if v.ConsistentRead != nil { |
| 106 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentReadKey.Bool(*v.ConsistentRead)) |
| 107 | + } |
| 108 | + |
| 109 | + if v.IndexName != nil { |
| 110 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBIndexNameKey.String(*v.IndexName)) |
| 111 | + } |
| 112 | + |
| 113 | + if v.Limit != nil { |
| 114 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) |
| 115 | + } |
| 116 | + |
| 117 | + if v.ScanIndexForward != nil { |
| 118 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBScanForwardKey.Bool(*v.ScanIndexForward)) |
| 119 | + } |
| 120 | + |
| 121 | + if v.ProjectionExpression != nil { |
| 122 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) |
| 123 | + } |
| 124 | + |
| 125 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSelectKey.String(string(v.Select))) |
| 126 | + |
| 127 | + case *dynamodb.ScanInput: |
| 128 | + |
| 129 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 130 | + |
| 131 | + if v.ConsistentRead != nil { |
| 132 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBConsistentReadKey.Bool(*v.ConsistentRead)) |
| 133 | + } |
| 134 | + |
| 135 | + if v.IndexName != nil { |
| 136 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBIndexNameKey.String(*v.IndexName)) |
| 137 | + } |
| 138 | + |
| 139 | + if v.Limit != nil { |
| 140 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBLimitKey.Int(int(*v.Limit))) |
| 141 | + } |
| 142 | + |
| 143 | + if v.ProjectionExpression != nil { |
| 144 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProjectionKey.String(*v.ProjectionExpression)) |
| 145 | + } |
| 146 | + |
| 147 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSelectKey.String(string(v.Select))) |
| 148 | + |
| 149 | + if v.Segment != nil { |
| 150 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBSegmentKey.Int(int(*v.Segment))) |
| 151 | + } |
| 152 | + |
| 153 | + if v.TotalSegments != nil { |
| 154 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTotalSegmentsKey.Int(int(*v.TotalSegments))) |
| 155 | + } |
| 156 | + |
| 157 | + case *dynamodb.UpdateItemInput: |
| 158 | + |
| 159 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 160 | + |
| 161 | + case *dynamodb.UpdateTableInput: |
| 162 | + |
| 163 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBTableNamesKey.String(*v.TableName)) |
| 164 | + |
| 165 | + if v.AttributeDefinitions != nil { |
| 166 | + attributedefinitions, _ := json.Marshal(v.AttributeDefinitions) |
| 167 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBAttributeDefinitionsKey.String(string(attributedefinitions))) |
| 168 | + } |
| 169 | + |
| 170 | + if v.GlobalSecondaryIndexUpdates != nil { |
| 171 | + globalsecondaryindexupdates, _ := json.Marshal(v.GlobalSecondaryIndexUpdates) |
| 172 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBGlobalSecondaryIndexUpdatesKey.String(string(globalsecondaryindexupdates))) |
| 173 | + } |
| 174 | + |
| 175 | + if v.ProvisionedThroughput != nil { |
| 176 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedReadCapacityKey.Int64(*v.ProvisionedThroughput.ReadCapacityUnits)) |
| 177 | + dynamodbAttributes = append(dynamodbAttributes, semconv.AWSDynamoDBProvisionedWriteCapacityKey.Int64(*v.ProvisionedThroughput.WriteCapacityUnits)) |
| 178 | + } |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + return dynamodbAttributes |
| 183 | + |
| 184 | +} |
0 commit comments