saluki_env/host/providers/
remote_agent.rs

1use async_trait::async_trait;
2use memory_accounting::{MemoryBounds, MemoryBoundsBuilder};
3use saluki_config::GenericConfiguration;
4use saluki_error::GenericError;
5use tokio::sync::OnceCell;
6
7use crate::{helpers::remote_agent::RemoteAgentClient, HostProvider};
8
9/// Datadog Agent-based host provider.
10///
11/// This provider is based on the internal gRPC API exposed by the Datadog Agent which exposes a way to get the hostname
12/// as detected by the Datadog Agent.
13#[derive(Clone)]
14pub struct RemoteAgentHostProvider {
15    client: RemoteAgentClient,
16    cached_hostname: OnceCell<String>,
17}
18
19impl RemoteAgentHostProvider {
20    /// Creates a new `RemoteAgentHostProvider` from the given configuration.
21    ///
22    /// # Errors
23    ///
24    /// If the Agent gRPC client cannot be created (invalid API endpoint, missing authentication token, etc), or if the
25    /// authentication token is invalid, an error will be returned.
26    pub async fn from_configuration(config: &GenericConfiguration) -> Result<Self, GenericError> {
27        let client = RemoteAgentClient::from_configuration(config).await?;
28
29        Ok(Self {
30            client,
31            cached_hostname: OnceCell::new(),
32        })
33    }
34
35    async fn get_or_fetch_hostname(&self) -> Result<String, GenericError> {
36        self.cached_hostname
37            .get_or_try_init(|| {
38                let mut client = self.client.clone();
39                async move { client.get_hostname().await }
40            })
41            .await
42            .cloned()
43    }
44}
45
46#[async_trait]
47impl HostProvider for RemoteAgentHostProvider {
48    type Error = GenericError;
49
50    async fn get_hostname(&self) -> Result<String, Self::Error> {
51        self.get_or_fetch_hostname().await
52    }
53}
54
55impl MemoryBounds for RemoteAgentHostProvider {
56    fn specify_bounds(&self, builder: &mut MemoryBoundsBuilder) {
57        builder.minimum().with_single_value::<Self>("component struct");
58    }
59}