saluki_env/workload/
metadata.rs

1use std::fmt;
2
3use saluki_context::{
4    origin::{ExternalData, OriginTagCardinality},
5    tags::TagSet,
6};
7
8use super::{entity::EntityId, helpers::OneOrMany};
9
10/// A metadata operation.
11///
12/// Operations involve a number of actions to perform in the context of the given entity. Such actions typically include
13/// setting tags or establishing ancestry links.
14#[derive(Clone, Debug)]
15pub struct MetadataOperation {
16    /// The entity ID this operation is associated with.
17    pub entity_id: EntityId,
18
19    /// The action(s) to perform.
20    pub actions: OneOrMany<MetadataAction>,
21}
22
23impl MetadataOperation {
24    /// Creates a new `MetadataOperation` that aliases an entity to another.
25    pub fn add_alias(entity_id: EntityId, target_entity_id: EntityId) -> Self {
26        Self {
27            entity_id,
28            actions: OneOrMany::One(MetadataAction::AddAlias { target_entity_id }),
29        }
30    }
31
32    /// Creates a new `MetadataOperation` that removes an alias to another entity.
33    pub fn remove_alias(entity_id: EntityId, target_entity_id: EntityId) -> Self {
34        Self {
35            entity_id,
36            actions: OneOrMany::One(MetadataAction::RemoveAlias { target_entity_id }),
37        }
38    }
39
40    /// Creates a new `MetadataOperation` that deletes all metadata for an entity.
41    pub fn delete(entity_id: EntityId) -> Self {
42        Self {
43            entity_id,
44            actions: OneOrMany::One(MetadataAction::Delete),
45        }
46    }
47
48    /// Creates a new `MetadataOperation` that attaches External Data to an entity.
49    pub fn attach_external_data(entity_id: EntityId, external_data: ExternalData) -> Self {
50        Self {
51            entity_id,
52            actions: OneOrMany::One(MetadataAction::AttachExternalData { external_data }),
53        }
54    }
55}
56
57/// A metadata action.
58#[derive(Clone)]
59pub enum MetadataAction {
60    /// Delete all metadata for the entity.
61    Delete,
62
63    /// Adds an alias to the entity.
64    ///
65    /// This can be used to allow for querying for information about an entity, where the information is not directly
66    /// associated with the entity itself. For example, process ID entities usually are aliased to an underlying
67    /// container ID entity, where the container ID entity itself is the one with associated tags and so on. When
68    /// querying for the tags of the process ID entity, an alias to the container ID entity can be established to
69    /// instead allow for getting the container ID entity's tags.
70    AddAlias {
71        /// Entity ID of the target to alias to.
72        target_entity_id: EntityId,
73    },
74
75    /// Removes an alias to the entity.
76    RemoveAlias {
77        /// Entity ID of the target to remove the alias to.
78        target_entity_id: EntityId,
79    },
80
81    /// Sets the tags for the entity.
82    ///
83    /// This overwrites any existing tags for the entity.
84    SetTags {
85        /// Cardinality to set the tags at.
86        cardinality: OriginTagCardinality,
87
88        /// Tags to set.
89        tags: TagSet,
90    },
91
92    /// Attaches External Data to the entity.
93    ///
94    /// External Data is free-form string data that is attached to entities from external systems to allow resolving the
95    /// entity's ID when it cannot be passed directly to the entity such that the entity can provide it in telemetry
96    /// payloads itself.
97    AttachExternalData {
98        /// External Data to attach.
99        external_data: ExternalData,
100    },
101}
102
103impl fmt::Debug for MetadataAction {
104    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
105        match self {
106            Self::Delete => write!(f, "Delete"),
107            Self::AddAlias { target_entity_id } => write!(f, "AddAlias({:?})", target_entity_id),
108            Self::RemoveAlias { target_entity_id } => write!(f, "RemoveAlias({:?})", target_entity_id),
109            Self::SetTags { cardinality, tags } => write!(f, "SetTags(cardinality={:?}, tags={:?})", cardinality, tags),
110            Self::AttachExternalData { external_data } => write!(f, "AttachExternalData({:?})", external_data),
111        }
112    }
113}