aws_smithy_observability/
attributes.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Attributes (also referred to as tags or annotations in other telemetry systems) are structured
7//! key-value pairs that annotate a span or event. Structured data allows observability backends
8//! to index and process telemetry data in ways that simple log messages lack.
9
10use std::collections::HashMap;
11
12/// The valid types of values accepted by [Attributes].
13#[non_exhaustive]
14#[derive(Clone, Debug, PartialEq)]
15pub enum AttributeValue {
16    /// Holds an [i64]
17    I64(i64),
18    /// Holds an [f64]
19    F64(f64),
20    /// Holds a [String]
21    String(String),
22    /// Holds a [bool]
23    Bool(bool),
24}
25
26/// Structured telemetry metadata.
27#[non_exhaustive]
28#[derive(Clone, Debug, Default)]
29pub struct Attributes {
30    attrs: HashMap<String, AttributeValue>,
31}
32
33impl Attributes {
34    /// Create a new empty instance of [Attributes].
35    pub fn new() -> Self {
36        Self::default()
37    }
38
39    /// Set an attribute.
40    pub fn set(&mut self, key: impl Into<String>, value: impl Into<AttributeValue>) {
41        self.attrs.insert(key.into(), value.into());
42    }
43
44    /// Get an attribute.
45    pub fn get(&self, key: impl Into<String>) -> Option<&AttributeValue> {
46        self.attrs.get(&key.into())
47    }
48
49    /// Get all of the attribute key value pairs.
50    pub fn attributes(&self) -> &HashMap<String, AttributeValue> {
51        &self.attrs
52    }
53
54    /// Get an owned [Iterator] of ([String], [AttributeValue]).
55    pub fn into_attributes(self) -> impl Iterator<Item = (String, AttributeValue)> {
56        self.attrs.into_iter()
57    }
58}