telemetry_rust/middleware/aws/operations/
dynamodb.rs

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