-
Notifications
You must be signed in to change notification settings - Fork 11
feat(agent-data-plane): emit liveness signals #2213
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
39b21c3
3dcda7b
cd938ee
41c9389
c90a1a2
d94bbb2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,206 @@ | ||
| use std::{sync::LazyLock, time::Duration}; | ||
|
|
||
| use async_trait::async_trait; | ||
| use saluki_context::{ | ||
| tags::{Tag, TagSet}, | ||
| Context, | ||
| }; | ||
| use saluki_core::{ | ||
| accounting::{MemoryBounds, MemoryBoundsBuilder}, | ||
| components::{sources::*, ComponentContext}, | ||
| data_model::event::{ | ||
| metric::Metric, | ||
| service_check::{CheckStatus, ServiceCheck}, | ||
| Event, EventType, | ||
| }, | ||
| topology::OutputDefinition, | ||
| }; | ||
| use saluki_env::{EnvironmentProvider as _, HostProvider as _}; | ||
| use saluki_error::{ErrorContext as _, GenericError}; | ||
| use stringtheory::MetaString; | ||
| use tokio::{ | ||
| pin, select, | ||
| time::{interval, MissedTickBehavior}, | ||
| }; | ||
| use tracing::{debug, warn}; | ||
|
|
||
| use crate::internal::env::ADPEnvironmentProvider; | ||
|
|
||
| const LIVENESS_INTERVAL: Duration = Duration::from_secs(15); | ||
| const RUNNING_METRIC_NAME: &str = "datadog.agent_data_plane.running"; | ||
| const UP_SERVICE_CHECK_NAME: &str = "datadog.agent_data_plane.up"; | ||
|
|
||
| /// Periodically emits Agent Data Plane liveness signals. | ||
| pub struct LivenessConfiguration { | ||
| hostname: MetaString, | ||
| version: MetaString, | ||
| } | ||
|
|
||
| impl LivenessConfiguration { | ||
| /// Creates a liveness source configuration using the configured environment. | ||
| pub async fn from_environment_provider(env_provider: &ADPEnvironmentProvider) -> Result<Self, GenericError> { | ||
| let hostname = env_provider | ||
| .host() | ||
| .get_hostname() | ||
| .await | ||
| .error_context("Failed to get hostname for liveness source.")?; | ||
| Ok(Self::from_hostname(hostname)) | ||
| } | ||
|
|
||
| fn from_hostname(hostname: String) -> Self { | ||
| Self { | ||
| hostname: hostname.into(), | ||
| version: saluki_metadata::get_app_details().version().raw().into(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl SourceBuilder for LivenessConfiguration { | ||
| async fn build(&self, _context: ComponentContext) -> Result<Box<dyn Source + Send>, GenericError> { | ||
| Ok(Box::new(Liveness::new(self.hostname.clone(), self.version.clone()))) | ||
| } | ||
|
|
||
| fn outputs(&self) -> &[OutputDefinition<EventType>] { | ||
| static OUTPUTS: LazyLock<Vec<OutputDefinition<EventType>>> = LazyLock::new(|| { | ||
| vec![ | ||
| OutputDefinition::named_output("metrics", EventType::Metric), | ||
| OutputDefinition::named_output("service_checks", EventType::ServiceCheck), | ||
| ] | ||
| }); | ||
| &OUTPUTS | ||
| } | ||
| } | ||
|
|
||
| impl MemoryBounds for LivenessConfiguration { | ||
| fn specify_bounds(&self, builder: &mut MemoryBoundsBuilder) { | ||
| builder.minimum().with_single_value::<Liveness>("liveness source"); | ||
| } | ||
| } | ||
|
|
||
| struct Liveness { | ||
| metric: Event, | ||
| service_check: Event, | ||
| } | ||
|
|
||
| impl Liveness { | ||
| fn new(hostname: MetaString, version: MetaString) -> Self { | ||
| let (metric, service_check) = create_liveness_events(hostname, version); | ||
| Self { metric, service_check } | ||
| } | ||
|
|
||
| fn events(&self) -> (Event, Event) { | ||
| (self.metric.clone(), self.service_check.clone()) | ||
| } | ||
| } | ||
|
|
||
| #[async_trait] | ||
| impl Source for Liveness { | ||
| async fn run(self: Box<Self>, mut context: SourceContext) -> Result<(), GenericError> { | ||
| let global_shutdown = context.take_shutdown_handle(); | ||
| pin!(global_shutdown); | ||
|
|
||
| let mut health = context.take_health_handle(); | ||
| let mut tick_interval = interval(LIVENESS_INTERVAL); | ||
| tick_interval.set_missed_tick_behavior(MissedTickBehavior::Skip); | ||
|
|
||
| health.mark_ready(); | ||
| debug!("Liveness source started."); | ||
|
|
||
| loop { | ||
| select! { | ||
| _ = &mut global_shutdown => { | ||
| debug!("Received shutdown signal."); | ||
| break; | ||
| }, | ||
| _ = health.live() => continue, | ||
| _ = tick_interval.tick() => { | ||
| let (metric, service_check) = self.events(); | ||
|
|
||
| if let Err(error) = context.dispatcher().dispatch_one_named("metrics", metric).await { | ||
| warn!(error = %error, "Failed to dispatch liveness metric."); | ||
| } | ||
|
|
||
| if let Err(error) = context.dispatcher().dispatch_one_named("service_checks", service_check).await { | ||
| warn!(error = %error, "Failed to dispatch liveness service check."); | ||
| } | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| debug!("Liveness source stopped."); | ||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| fn create_liveness_events(hostname: MetaString, version: MetaString) -> (Event, Event) { | ||
| let metric_context = Context::from_static_name(RUNNING_METRIC_NAME) | ||
| .with_host(hostname.clone()) | ||
| .with_tags(TagSet::from_iter([Tag::from(format!("version:{version}"))])); | ||
| let metric = Event::Metric(Metric::gauge(metric_context, 1.0)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
|
|
||
| let service_check = | ||
| Event::ServiceCheck(ServiceCheck::new(UP_SERVICE_CHECK_NAME, CheckStatus::Ok).with_hostname(hostname)); | ||
|
|
||
| (metric, service_check) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are going to be around for the lifetime of the process, can we just make them once and then have them be static or something? It's not a lot, but we should minimize allocations here
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [GPT-5.6 Terra (OpenAI)] Fixed in |
||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use saluki_core::{ | ||
| components::sources::SourceBuilder as _, | ||
| data_model::event::{metric::MetricValues, service_check::CheckStatus, Event, EventType}, | ||
| }; | ||
|
|
||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn declares_named_metric_and_service_check_outputs() { | ||
| let configuration = LivenessConfiguration::from_hostname("host-a".to_string()); | ||
| let outputs = configuration.outputs(); | ||
|
|
||
| assert_eq!(outputs.len(), 2); | ||
| assert_eq!(outputs[0].output_name(), Some("metrics")); | ||
| assert_eq!(outputs[0].data_ty(), EventType::Metric); | ||
| assert_eq!(outputs[1].output_name(), Some("service_checks")); | ||
| assert_eq!(outputs[1].data_ty(), EventType::ServiceCheck); | ||
| } | ||
|
|
||
| #[test] | ||
| fn source_configuration_requires_only_a_hostname_and_version() { | ||
| let configuration = LivenessConfiguration::from_hostname("host-a".to_string()); | ||
|
|
||
| assert_eq!(configuration.hostname, "host-a"); | ||
| assert_eq!( | ||
| configuration.version, | ||
| saluki_metadata::get_app_details().version().raw() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn prebuilt_metric_payload_has_required_contract() { | ||
| let liveness = Liveness::new("host-a".into(), "1.2.3".into()); | ||
| let (metric, _) = liveness.events(); | ||
|
|
||
| let Event::Metric(metric) = metric else { | ||
| panic!("expected metric event"); | ||
| }; | ||
| assert_eq!(metric.context().name(), RUNNING_METRIC_NAME); | ||
| assert_eq!(metric.context().host(), Some("host-a")); | ||
| assert!(metric.context().tags().has_tag("version:1.2.3")); | ||
| assert_eq!(metric.values(), &MetricValues::gauge((0, 1.0))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn prebuilt_service_check_payload_has_required_contract() { | ||
| let liveness = Liveness::new("host-a".into(), "1.2.3".into()); | ||
| let (_, service_check) = liveness.events(); | ||
|
|
||
| let Event::ServiceCheck(service_check) = service_check else { | ||
| panic!("expected service check event"); | ||
| }; | ||
| assert_eq!(service_check.name(), UP_SERVICE_CHECK_NAME); | ||
| assert_eq!(service_check.status(), CheckStatus::Ok); | ||
| assert_eq!(service_check.hostname(), Some("host-a")); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is hostname here necessary? I would have thought this would be added by the downstream metrics/service_checks components
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[GPT-5.6 Terra (OpenAI)] Kept the hostname.
metrics_enrichintentionally does not add metric hosts, and this service check routes directly todd_service_checks_encodewithout a host-enrichment transform. Removing it would leave the metric without a host dimension and omithost_namefrom the service-check payload.