|
| 1 | +import type { Span } from '@sentry/core'; |
| 2 | +import { SPAN_KIND } from '@sentry/core'; |
| 3 | +import { DB_NAME, DB_OPERATION, DB_SYSTEM } from '@sentry/conventions/attributes'; |
| 4 | +import { |
| 5 | + ATTR_AWS_DYNAMODB_ATTRIBUTE_DEFINITIONS, |
| 6 | + ATTR_AWS_DYNAMODB_CONSISTENT_READ, |
| 7 | + ATTR_AWS_DYNAMODB_CONSUMED_CAPACITY, |
| 8 | + ATTR_AWS_DYNAMODB_COUNT, |
| 9 | + ATTR_AWS_DYNAMODB_EXCLUSIVE_START_TABLE, |
| 10 | + ATTR_AWS_DYNAMODB_GLOBAL_SECONDARY_INDEX_UPDATES, |
| 11 | + ATTR_AWS_DYNAMODB_GLOBAL_SECONDARY_INDEXES, |
| 12 | + ATTR_AWS_DYNAMODB_INDEX_NAME, |
| 13 | + ATTR_AWS_DYNAMODB_ITEM_COLLECTION_METRICS, |
| 14 | + ATTR_AWS_DYNAMODB_LIMIT, |
| 15 | + ATTR_AWS_DYNAMODB_LOCAL_SECONDARY_INDEXES, |
| 16 | + ATTR_AWS_DYNAMODB_PROJECTION, |
| 17 | + ATTR_AWS_DYNAMODB_PROVISIONED_READ_CAPACITY, |
| 18 | + ATTR_AWS_DYNAMODB_PROVISIONED_WRITE_CAPACITY, |
| 19 | + ATTR_AWS_DYNAMODB_SCAN_FORWARD, |
| 20 | + ATTR_AWS_DYNAMODB_SCANNED_COUNT, |
| 21 | + ATTR_AWS_DYNAMODB_SEGMENT, |
| 22 | + ATTR_AWS_DYNAMODB_SELECT, |
| 23 | + ATTR_AWS_DYNAMODB_TABLE_COUNT, |
| 24 | + ATTR_AWS_DYNAMODB_TABLE_NAMES, |
| 25 | + ATTR_AWS_DYNAMODB_TOTAL_SEGMENTS, |
| 26 | + DB_SYSTEM_VALUE_DYNAMODB, |
| 27 | +} from '../constants'; |
| 28 | +import type { NormalizedRequest, NormalizedResponse } from '../types'; |
| 29 | +import type { RequestMetadata, ServiceExtension } from './ServiceExtension'; |
| 30 | + |
| 31 | +function toArray<T>(values: T | T[]): T[] { |
| 32 | + return Array.isArray(values) ? values : [values]; |
| 33 | +} |
| 34 | + |
| 35 | +export class DynamodbServiceExtension implements ServiceExtension { |
| 36 | + public requestPreSpanHook(normalizedRequest: NormalizedRequest): RequestMetadata { |
| 37 | + const operation = normalizedRequest.commandName; |
| 38 | + const tableName = normalizedRequest.commandInput?.TableName; |
| 39 | + |
| 40 | + const spanAttributes: Record<string, unknown> = {}; |
| 41 | + |
| 42 | + /* oxlint-disable typescript/no-deprecated -- old-semconv db.* attributes, matched to the OTel aws-sdk integration */ |
| 43 | + spanAttributes[DB_SYSTEM] = DB_SYSTEM_VALUE_DYNAMODB; |
| 44 | + spanAttributes[DB_NAME] = tableName; |
| 45 | + spanAttributes[DB_OPERATION] = operation; |
| 46 | + /* oxlint-enable typescript/no-deprecated */ |
| 47 | + |
| 48 | + // `RequestItems` is undefined when no table names are returned; its keys are the table names. |
| 49 | + if (normalizedRequest.commandInput?.TableName) { |
| 50 | + // Necessary for commands with only 1 table name (e.g. CreateTable). Attribute is `TableName`, not keys of `RequestItems`. |
| 51 | + spanAttributes[ATTR_AWS_DYNAMODB_TABLE_NAMES] = [normalizedRequest.commandInput.TableName]; |
| 52 | + } else if (normalizedRequest.commandInput?.RequestItems) { |
| 53 | + spanAttributes[ATTR_AWS_DYNAMODB_TABLE_NAMES] = Object.keys(normalizedRequest.commandInput.RequestItems); |
| 54 | + } |
| 55 | + |
| 56 | + if (operation === 'CreateTable' || operation === 'UpdateTable') { |
| 57 | + // only check for ProvisionedThroughput since ReadCapacityUnits and WriteCapacityUnits are required attributes |
| 58 | + if (normalizedRequest.commandInput?.ProvisionedThroughput) { |
| 59 | + spanAttributes[ATTR_AWS_DYNAMODB_PROVISIONED_READ_CAPACITY] = |
| 60 | + normalizedRequest.commandInput.ProvisionedThroughput.ReadCapacityUnits; |
| 61 | + spanAttributes[ATTR_AWS_DYNAMODB_PROVISIONED_WRITE_CAPACITY] = |
| 62 | + normalizedRequest.commandInput.ProvisionedThroughput.WriteCapacityUnits; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if (operation === 'GetItem' || operation === 'Scan' || operation === 'Query') { |
| 67 | + if (normalizedRequest.commandInput?.ConsistentRead) { |
| 68 | + spanAttributes[ATTR_AWS_DYNAMODB_CONSISTENT_READ] = normalizedRequest.commandInput.ConsistentRead; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if (operation === 'Query' || operation === 'Scan') { |
| 73 | + if (normalizedRequest.commandInput?.ProjectionExpression) { |
| 74 | + spanAttributes[ATTR_AWS_DYNAMODB_PROJECTION] = normalizedRequest.commandInput.ProjectionExpression; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + if (operation === 'CreateTable') { |
| 79 | + if (normalizedRequest.commandInput?.GlobalSecondaryIndexes) { |
| 80 | + spanAttributes[ATTR_AWS_DYNAMODB_GLOBAL_SECONDARY_INDEXES] = toArray( |
| 81 | + normalizedRequest.commandInput.GlobalSecondaryIndexes, |
| 82 | + ).map((x: Record<string, any>) => JSON.stringify(x)); |
| 83 | + } |
| 84 | + |
| 85 | + if (normalizedRequest.commandInput?.LocalSecondaryIndexes) { |
| 86 | + spanAttributes[ATTR_AWS_DYNAMODB_LOCAL_SECONDARY_INDEXES] = toArray( |
| 87 | + normalizedRequest.commandInput.LocalSecondaryIndexes, |
| 88 | + ).map((x: Record<string, any>) => JSON.stringify(x)); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (operation === 'ListTables' || operation === 'Query' || operation === 'Scan') { |
| 93 | + if (normalizedRequest.commandInput?.Limit) { |
| 94 | + spanAttributes[ATTR_AWS_DYNAMODB_LIMIT] = normalizedRequest.commandInput.Limit; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + if (operation === 'ListTables') { |
| 99 | + if (normalizedRequest.commandInput?.ExclusiveStartTableName) { |
| 100 | + spanAttributes[ATTR_AWS_DYNAMODB_EXCLUSIVE_START_TABLE] = |
| 101 | + normalizedRequest.commandInput.ExclusiveStartTableName; |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if (operation === 'Query') { |
| 106 | + if (normalizedRequest.commandInput?.ScanIndexForward) { |
| 107 | + spanAttributes[ATTR_AWS_DYNAMODB_SCAN_FORWARD] = normalizedRequest.commandInput.ScanIndexForward; |
| 108 | + } |
| 109 | + |
| 110 | + if (normalizedRequest.commandInput?.IndexName) { |
| 111 | + spanAttributes[ATTR_AWS_DYNAMODB_INDEX_NAME] = normalizedRequest.commandInput.IndexName; |
| 112 | + } |
| 113 | + |
| 114 | + if (normalizedRequest.commandInput?.Select) { |
| 115 | + spanAttributes[ATTR_AWS_DYNAMODB_SELECT] = normalizedRequest.commandInput.Select; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if (operation === 'Scan') { |
| 120 | + if (normalizedRequest.commandInput?.Segment) { |
| 121 | + spanAttributes[ATTR_AWS_DYNAMODB_SEGMENT] = normalizedRequest.commandInput?.Segment; |
| 122 | + } |
| 123 | + |
| 124 | + if (normalizedRequest.commandInput?.TotalSegments) { |
| 125 | + spanAttributes[ATTR_AWS_DYNAMODB_TOTAL_SEGMENTS] = normalizedRequest.commandInput?.TotalSegments; |
| 126 | + } |
| 127 | + |
| 128 | + if (normalizedRequest.commandInput?.IndexName) { |
| 129 | + spanAttributes[ATTR_AWS_DYNAMODB_INDEX_NAME] = normalizedRequest.commandInput.IndexName; |
| 130 | + } |
| 131 | + |
| 132 | + if (normalizedRequest.commandInput?.Select) { |
| 133 | + spanAttributes[ATTR_AWS_DYNAMODB_SELECT] = normalizedRequest.commandInput.Select; |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if (operation === 'UpdateTable') { |
| 138 | + if (normalizedRequest.commandInput?.AttributeDefinitions) { |
| 139 | + spanAttributes[ATTR_AWS_DYNAMODB_ATTRIBUTE_DEFINITIONS] = toArray( |
| 140 | + normalizedRequest.commandInput.AttributeDefinitions, |
| 141 | + ).map((x: Record<string, any>) => JSON.stringify(x)); |
| 142 | + } |
| 143 | + |
| 144 | + if (normalizedRequest.commandInput?.GlobalSecondaryIndexUpdates) { |
| 145 | + spanAttributes[ATTR_AWS_DYNAMODB_GLOBAL_SECONDARY_INDEX_UPDATES] = toArray( |
| 146 | + normalizedRequest.commandInput.GlobalSecondaryIndexUpdates, |
| 147 | + ).map((x: Record<string, any>) => JSON.stringify(x)); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + return { |
| 152 | + spanAttributes, |
| 153 | + spanKind: SPAN_KIND.CLIENT, |
| 154 | + }; |
| 155 | + } |
| 156 | + |
| 157 | + public responseHook(response: NormalizedResponse, span: Span): void { |
| 158 | + if (response.data?.ConsumedCapacity) { |
| 159 | + span.setAttribute( |
| 160 | + ATTR_AWS_DYNAMODB_CONSUMED_CAPACITY, |
| 161 | + toArray(response.data.ConsumedCapacity).map((x: Record<string, any>) => JSON.stringify(x)), |
| 162 | + ); |
| 163 | + } |
| 164 | + |
| 165 | + if (response.data?.ItemCollectionMetrics) { |
| 166 | + span.setAttribute( |
| 167 | + ATTR_AWS_DYNAMODB_ITEM_COLLECTION_METRICS, |
| 168 | + toArray(response.data.ItemCollectionMetrics).map((x: Record<string, any>) => JSON.stringify(x)), |
| 169 | + ); |
| 170 | + } |
| 171 | + |
| 172 | + if (response.data?.TableNames) { |
| 173 | + span.setAttribute(ATTR_AWS_DYNAMODB_TABLE_COUNT, response.data?.TableNames.length); |
| 174 | + } |
| 175 | + |
| 176 | + if (response.data?.Count) { |
| 177 | + span.setAttribute(ATTR_AWS_DYNAMODB_COUNT, response.data?.Count); |
| 178 | + } |
| 179 | + |
| 180 | + if (response.data?.ScannedCount) { |
| 181 | + span.setAttribute(ATTR_AWS_DYNAMODB_SCANNED_COUNT, response.data?.ScannedCount); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
0 commit comments