telemetry_rust/middleware/aws/operations/
dynamodb.rs

1/// AWS DynamoDB operations
2///
3/// API Reference: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_Operations_Amazon_DynamoDB.html
4use crate::{KeyValue, StringValue, Value, semconv};
5
6use super::*;
7
8#[allow(deprecated)]
9pub const LEGACY_DB_NAME: &str = semconv::DB_NAME;
10
11#[allow(deprecated)]
12pub const LEGACY_DB_SYSTEM: &str = semconv::DB_SYSTEM;
13
14/// Builder for DynamoDB-specific OpenTelemetry spans.
15///
16/// This enum serves as a namespace for DynamoDB operation span builders.
17/// Each operation provides a specific method to create properly configured
18/// spans with DynamoDB-specific attributes.
19pub enum DynamodbSpanBuilder {}
20
21impl AwsSpanBuilder<'_> {
22    /// Creates a DynamoDB operation span builder.
23    ///
24    /// This method creates a span builder configured for DynamoDB operations with
25    /// appropriate semantic attributes according to OpenTelemetry conventions.
26    ///
27    /// # Arguments
28    ///
29    /// * `method` - The DynamoDB operation method name (e.g., "GetItem", "PutItem")
30    /// * `table_names` - Iterator of table names involved in the operation
31    ///
32    /// # Returns
33    ///
34    /// A configured AWS span builder for the DynamoDB operation
35    pub fn dynamodb(
36        method: impl Into<StringValue>,
37        table_names: impl IntoIterator<Item = impl Into<StringValue>>,
38    ) -> Self {
39        let method: StringValue = method.into();
40        let table_names: Vec<StringValue> =
41            table_names.into_iter().map(|item| item.into()).collect();
42        let mut attributes = vec![
43            KeyValue::new(LEGACY_DB_SYSTEM, "dynamodb"),
44            KeyValue::new(semconv::DB_OPERATION_NAME, method.clone()),
45        ];
46        match table_names.len() {
47            0 => {}
48            1 => {
49                attributes.extend([
50                    KeyValue::new(LEGACY_DB_NAME, table_names[0].clone()),
51                    KeyValue::new(semconv::DB_NAMESPACE, table_names[0].clone()),
52                    KeyValue::new(
53                        semconv::AWS_DYNAMODB_TABLE_NAMES,
54                        Value::Array(table_names.into()),
55                    ),
56                ]);
57            }
58            _ => {
59                attributes.push(KeyValue::new(
60                    semconv::AWS_DYNAMODB_TABLE_NAMES,
61                    Value::Array(table_names.into()),
62                ));
63            }
64        }
65        Self::client("DynamoDB", method, attributes)
66    }
67}
68
69macro_rules! dynamodb_global_operation {
70    ($op: ident) => {
71        impl DynamodbSpanBuilder {
72            #[doc = concat!("Creates a span builder for the DynamoDB ", stringify!($op), " operation.")]
73            ///
74            /// This operation does not require specific table names as it operates globally.
75            #[inline]
76            pub fn $op<'a>() -> AwsSpanBuilder<'a> {
77                AwsSpanBuilder::dynamodb(
78                    stringify_camel!($op),
79                    std::iter::empty::<StringValue>(),
80                )
81            }
82        }
83    };
84}
85
86macro_rules! dynamodb_table_operation {
87    ($op: ident) => {
88        impl DynamodbSpanBuilder {
89            #[doc = concat!("Creates a span builder for the DynamoDB ", stringify!($op), " operation on a specific table.")]
90            ///
91            /// # Arguments
92            ///
93            /// * `table_name` - The name of the DynamoDB table
94            pub fn $op<'a>(table_name: impl Into<StringValue>) -> AwsSpanBuilder<'a> {
95                AwsSpanBuilder::dynamodb(
96                    stringify_camel!($op),
97                    std::iter::once(table_name),
98                )
99            }
100        }
101    };
102}
103
104macro_rules! dynamodb_table_arn_operation {
105    ($op: ident) => {
106        impl DynamodbSpanBuilder {
107            #[doc = concat!("Creates a span builder for the DynamoDB ", stringify!($op), " operation using a table ARN.")]
108            ///
109            /// # Arguments
110            ///
111            /// * `table_arn` - The ARN of the DynamoDB table
112            pub fn $op<'a>(table_arn: impl Into<StringValue>) -> AwsSpanBuilder<'a> {
113                let table_arn = table_arn.into();
114                AwsSpanBuilder::dynamodb(
115                    stringify_camel!($op),
116                    std::iter::empty::<StringValue>(),
117                )
118                .attributes(vec![
119                    KeyValue::new(LEGACY_DB_NAME, table_arn.clone()),
120                    KeyValue::new(semconv::DB_NAMESPACE, table_arn),
121                ])
122            }
123        }
124    };
125}
126
127macro_rules! dynamodb_batch_operation {
128    ($op: ident) => {
129        impl DynamodbSpanBuilder {
130            #[doc = concat!("Creates a span builder for the DynamoDB ", stringify!($op), " batch operation.")]
131            ///
132            /// # Arguments
133            ///
134            /// * `table_names` - Iterator of table names involved in the batch operation
135            pub fn $op<'a>(
136                table_names: impl IntoIterator<Item = impl Into<StringValue>>,
137            ) -> AwsSpanBuilder<'a> {
138                AwsSpanBuilder::dynamodb(stringify_camel!($op), table_names)
139            }
140        }
141    };
142}
143
144// global operations
145dynamodb_global_operation!(describe_endpoints);
146dynamodb_global_operation!(describe_limits);
147dynamodb_global_operation!(list_global_tables);
148dynamodb_global_operation!(list_tables);
149
150// operations on custom resources
151dynamodb_global_operation!(delete_backup);
152dynamodb_global_operation!(describe_backup);
153dynamodb_global_operation!(describe_export);
154dynamodb_global_operation!(describe_import);
155
156// table operations (by name)
157dynamodb_table_operation!(create_backup);
158dynamodb_table_operation!(create_table);
159dynamodb_table_operation!(delete_item);
160dynamodb_table_operation!(delete_resource_policy);
161dynamodb_table_operation!(delete_table);
162dynamodb_table_operation!(describe_continuous_backups);
163dynamodb_table_operation!(describe_contributor_insights);
164dynamodb_table_operation!(describe_kinesis_streaming_destination);
165dynamodb_table_operation!(describe_table);
166dynamodb_table_operation!(describe_table_replica_auto_scaling);
167dynamodb_table_operation!(describe_time_to_live);
168dynamodb_table_operation!(disable_kinesis_streaming_destination);
169dynamodb_table_operation!(enable_kinesis_streaming_destination);
170dynamodb_table_operation!(execute_statement);
171dynamodb_table_operation!(get_item);
172dynamodb_table_operation!(get_resource_policy);
173dynamodb_table_operation!(import_table);
174dynamodb_table_operation!(list_backups);
175dynamodb_table_operation!(list_contributor_insights);
176dynamodb_table_operation!(list_tags_of_resource);
177dynamodb_table_operation!(put_item);
178dynamodb_table_operation!(put_resource_policy);
179dynamodb_table_operation!(query);
180dynamodb_table_operation!(restore_table_from_backup);
181dynamodb_table_operation!(restore_table_to_point_in_time);
182dynamodb_table_operation!(scan);
183dynamodb_table_operation!(tag_resource);
184dynamodb_table_operation!(untag_resource);
185dynamodb_table_operation!(update_continuous_backups);
186dynamodb_table_operation!(update_contributor_insights);
187dynamodb_table_operation!(update_item);
188dynamodb_table_operation!(update_kinesis_streaming_destination);
189dynamodb_table_operation!(update_table);
190dynamodb_table_operation!(update_table_replica_auto_scaling);
191dynamodb_table_operation!(update_time_to_live);
192
193// table operations (by arn)
194dynamodb_table_arn_operation!(export_table_to_point_in_time);
195dynamodb_table_arn_operation!(list_exports);
196dynamodb_table_arn_operation!(list_imports);
197
198// global table operations
199dynamodb_table_operation!(create_global_table);
200dynamodb_table_operation!(describe_global_table);
201dynamodb_table_operation!(describe_global_table_settings);
202dynamodb_table_operation!(update_global_table);
203dynamodb_table_operation!(update_global_table_settings);
204
205// batch operations
206dynamodb_batch_operation!(batch_execute_statement);
207dynamodb_batch_operation!(batch_get_item);
208dynamodb_batch_operation!(batch_write_item);
209dynamodb_batch_operation!(execute_transaction);
210dynamodb_batch_operation!(transact_get_items);
211dynamodb_batch_operation!(transact_write_items);