Skip to main content

saluki_env/workload/
mod.rs

1//! Workload provider.
2//!
3//! This modules provides the `WorkloadProvider` trait, which deals with providing information about workloads running
4//! on the process host.
5//!
6//! A number of building blocks are included: generic entity identifiers, tag storage, metadata collection and
7//! aggregation.
8
9use saluki_context::{
10    origin::{OriginTagCardinality, RawOrigin},
11    tags::SharedTagSet,
12};
13
14pub mod aggregator;
15pub mod collectors;
16
17pub mod entity;
18pub use self::entity::EntityId;
19
20mod helpers;
21mod metadata;
22pub use self::metadata::{MetadataAction, MetadataOperation};
23
24mod on_demand_pid;
25pub use self::on_demand_pid::OnDemandPIDResolver;
26
27pub mod origin;
28use self::origin::ResolvedOrigin;
29
30pub mod providers;
31
32pub mod stores;
33
34/// Resolves live process IDs observed during traffic capture to workload entities.
35///
36/// This is intentionally narrower than [`WorkloadProvider`]: callers should only use it for PIDs observed from the
37/// local operating system while capturing traffic. It's not a general-purpose historical PID lookup API.
38pub trait CaptureEntityResolver {
39    /// Resolves a live process ID to the container entity that owns it, if known.
40    fn resolve_container_entity_for_live_pid(&self, process_id: u32) -> Option<EntityId>;
41}
42
43impl<T> CaptureEntityResolver for Option<T>
44where
45    T: CaptureEntityResolver,
46{
47    fn resolve_container_entity_for_live_pid(&self, process_id: u32) -> Option<EntityId> {
48        match self.as_ref() {
49            Some(resolver) => resolver.resolve_container_entity_for_live_pid(process_id),
50            None => None,
51        }
52    }
53}
54
55/// Provides information about workloads running on the process host.
56pub trait WorkloadProvider {
57    /// Gets the tags for an entity.
58    ///
59    /// Entities are workload resources running on the process host, such as containers or pods. The cardinality of the
60    /// tags to get can be controlled via `cardinality`.
61    ///
62    /// Returns `Some(SharedTagSet)` if the entity has tags, or `None` if the entity doesn't have any tags or if the
63    /// entity wasn't found.
64    fn get_tags_for_entity(&self, entity_id: &EntityId, cardinality: OriginTagCardinality) -> Option<SharedTagSet>;
65
66    /// Resolves a raw origin.
67    ///
68    ///  If the origin is empty, `None` is returned. Otherwise, `Some(ResolvedOrigin)` will be returned, which contains
69    ///  fully resolved versions of the raw origin components.
70    fn get_resolved_origin(&self, origin: RawOrigin<'_>) -> Option<ResolvedOrigin>;
71}
72
73impl<T> WorkloadProvider for Option<T>
74where
75    T: WorkloadProvider,
76{
77    fn get_tags_for_entity(&self, entity_id: &EntityId, cardinality: OriginTagCardinality) -> Option<SharedTagSet> {
78        match self.as_ref() {
79            Some(provider) => provider.get_tags_for_entity(entity_id, cardinality),
80            None => None,
81        }
82    }
83
84    fn get_resolved_origin(&self, origin: RawOrigin<'_>) -> Option<ResolvedOrigin> {
85        match self.as_ref() {
86            Some(provider) => provider.get_resolved_origin(origin),
87            None => None,
88        }
89    }
90}