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;
21#[cfg(target_os = "linux")]
22pub use self::helpers::cgroups::CgroupsConfiguration;
23#[cfg(unix)]
24pub use self::helpers::containerd::ContainerdConfiguration;
25
26mod metadata;
27pub use self::metadata::{MetadataAction, MetadataOperation};
28
29mod on_demand_pid;
30pub use self::on_demand_pid::OnDemandPIDResolver;
31
32pub mod origin;
33use self::origin::ResolvedOrigin;
34
35pub mod providers;
36
37pub mod stores;
38
39/// Resolves live process IDs observed from local socket credentials to workload entities.
40///
41/// This is intentionally narrower than [`WorkloadProvider`]: callers should only use it for current PIDs obtained
42/// directly from the local operating system. Callers that defer processing should retain the returned entity ID rather
43/// than resolve the PID again later. This isn't a general-purpose historical PID lookup API.
44pub trait CaptureEntityResolver {
45    /// Resolves a live process ID to the container entity that owns it, if known.
46    fn resolve_container_entity_for_live_pid(&self, process_id: u32) -> Option<EntityId>;
47}
48
49impl<T> CaptureEntityResolver for Option<T>
50where
51    T: CaptureEntityResolver,
52{
53    fn resolve_container_entity_for_live_pid(&self, process_id: u32) -> Option<EntityId> {
54        match self.as_ref() {
55            Some(resolver) => resolver.resolve_container_entity_for_live_pid(process_id),
56            None => None,
57        }
58    }
59}
60
61/// Provides information about workloads running on the process host.
62pub trait WorkloadProvider {
63    /// Gets the tags for an entity.
64    ///
65    /// Entities are workload resources running on the process host, such as containers or pods. The cardinality of the
66    /// tags to get can be controlled via `cardinality`.
67    ///
68    /// Returns `Some(SharedTagSet)` if the entity has tags, or `None` if the entity doesn't have any tags or if the
69    /// entity wasn't found.
70    fn get_tags_for_entity(&self, entity_id: &EntityId, cardinality: OriginTagCardinality) -> Option<SharedTagSet>;
71
72    /// Gets low-cardinality tags for the container that runs this process.
73    ///
74    /// Providers that cannot determine the process container return `None`.
75    fn get_self_container_tags(&self) -> Option<SharedTagSet> {
76        None
77    }
78
79    /// Resolves a raw origin.
80    ///
81    ///  If the origin is empty, `None` is returned. Otherwise, `Some(ResolvedOrigin)` will be returned, which contains
82    ///  fully resolved versions of the raw origin components.
83    fn get_resolved_origin(&self, origin: RawOrigin<'_>) -> Option<ResolvedOrigin>;
84}
85
86impl<T> WorkloadProvider for Option<T>
87where
88    T: WorkloadProvider,
89{
90    fn get_tags_for_entity(&self, entity_id: &EntityId, cardinality: OriginTagCardinality) -> Option<SharedTagSet> {
91        match self.as_ref() {
92            Some(provider) => provider.get_tags_for_entity(entity_id, cardinality),
93            None => None,
94        }
95    }
96
97    fn get_self_container_tags(&self) -> Option<SharedTagSet> {
98        match self.as_ref() {
99            Some(provider) => provider.get_self_container_tags(),
100            None => None,
101        }
102    }
103
104    fn get_resolved_origin(&self, origin: RawOrigin<'_>) -> Option<ResolvedOrigin> {
105        match self.as_ref() {
106            Some(provider) => provider.get_resolved_origin(origin),
107            None => None,
108        }
109    }
110}