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