Skip to main content

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