-
Notifications
You must be signed in to change notification settings - Fork 2
feat: cmk Linux agent data emitted from metrics-cache #32
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: master
Are you sure you want to change the base?
Changes from all commits
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,13 @@ | ||
| --- | ||
| source: metrics-cache/src/section/writeable.rs | ||
| assertion_line: 120 | ||
| expression: "String::from_utf8(out).unwrap()" | ||
| --- | ||
| <<<<json-host>>>> | ||
| <<<test_section_v1:sep(0)>>> | ||
| {"value":7} | ||
| <<<<>>>> | ||
| <<<<raw-host>>>> | ||
| <<<check_mk>>> | ||
| Version: 2.5.0 | ||
| <<<<>>>> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| use axum::body::Bytes; | ||
|
Member
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. I think it's fine to use |
||
| use std::collections::BTreeMap; | ||
| use std::io::Write; | ||
|
|
||
|
|
@@ -6,9 +7,13 @@ use crate::section::Section; | |
| #[derive(Debug)] | ||
| pub struct WriteableSection { | ||
| pub piggyback_hostname: String, | ||
| pub name: &'static str, | ||
| /// The JSON serialized body of the section | ||
| pub body: String, | ||
| pub body: SectionBody, | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
| pub enum SectionBody { | ||
| Json { name: &'static str, body: String }, | ||
| Raw(Bytes), | ||
| } | ||
|
|
||
| #[derive(Debug)] | ||
|
|
@@ -25,10 +30,19 @@ impl WriteableSection { | |
| })?; | ||
| Ok(Self { | ||
| piggyback_hostname: piggyback_hostname.to_string(), | ||
| name: S::NAME, | ||
| body, | ||
| body: SectionBody::Json { | ||
| name: S::NAME, | ||
| body, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| pub fn from_raw(piggyback_hostname: String, raw: Bytes) -> Self { | ||
|
Member
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. I'd match |
||
| Self { | ||
| piggyback_hostname, | ||
| body: SectionBody::Raw(raw), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Take a collection of [`WriteableSection`]s and render them into something | ||
|
|
@@ -47,9 +61,19 @@ pub fn frame<W: Write>(writer: &mut W, sections: Vec<WriteableSection>) -> std:: | |
| writeln!(writer, "<<<<{host}>>>>")?; | ||
| } | ||
| for section in host_sections { | ||
| writeln!(writer, "<<<{}:sep(0)>>>", section.name)?; | ||
| writer.write_all(section.body.as_bytes())?; | ||
| writeln!(writer)?; | ||
| match §ion.body { | ||
| SectionBody::Json { name, body } => { | ||
| writeln!(writer, "<<<{name}:sep(0)>>>")?; | ||
| writer.write_all(body.as_bytes())?; | ||
| writeln!(writer)?; | ||
| } | ||
| SectionBody::Raw(raw) => { | ||
| writer.write_all(raw)?; | ||
| if !raw.ends_with(b"\n") && !raw.is_empty() { | ||
| writeln!(writer)?; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if !bare { | ||
| writeln!(writer, "<<<<>>>>")?; | ||
|
|
@@ -81,4 +105,46 @@ mod tests { | |
| frame(&mut out, sections).unwrap(); | ||
| insta::assert_snapshot!(String::from_utf8(out).unwrap()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn frame_raw_and_json_mixed() { | ||
| let sections = vec![ | ||
| WriteableSection::of("json-host", &TestSectionV1 { value: 7 }).unwrap(), | ||
| WriteableSection::from_raw( | ||
| "raw-host".to_string(), | ||
| Bytes::from_static(b"<<<check_mk>>>\nVersion: 2.5.0\n"), | ||
| ), | ||
| ]; | ||
| let mut out = Vec::new(); | ||
| frame(&mut out, sections).unwrap(); | ||
| insta::assert_snapshot!(String::from_utf8(out).unwrap()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn frame_raw_without_trailing_newline_gets_one_added() { | ||
| let sections = vec![WriteableSection::from_raw( | ||
| "raw-host".to_string(), | ||
| Bytes::from_static(b"<<<check_mk>>>\nVersion: 2.5.0"), | ||
| )]; | ||
| let mut out = Vec::new(); | ||
| frame(&mut out, sections).unwrap(); | ||
| assert_eq!( | ||
| String::from_utf8(out).unwrap(), | ||
| "<<<<raw-host>>>>\n<<<check_mk>>>\nVersion: 2.5.0\n<<<<>>>>\n" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn frame_raw_empty_produces_no_spurious_blank_line() { | ||
| let sections = vec![WriteableSection::from_raw( | ||
| "raw-host".to_string(), | ||
| Bytes::new(), | ||
| )]; | ||
| let mut out = Vec::new(); | ||
| frame(&mut out, sections).unwrap(); | ||
| assert_eq!( | ||
| String::from_utf8(out).unwrap(), | ||
| "<<<<raw-host>>>>\n<<<<>>>>\n" | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,12 +3,15 @@ pub mod metric_tables; | |
| pub mod owner_graph; | ||
| pub mod self_health; | ||
|
|
||
| use axum::body::Bytes; | ||
| use moka::future::Cache; | ||
| use std::borrow::Borrow; | ||
| use std::collections::HashMap; | ||
| use std::sync::Arc; | ||
| use std::time::Instant; | ||
|
|
||
| use crate::ingest::MetricsFetcherIngestion; | ||
| use crate::ingest::SystemAgentOutput; | ||
| use crate::ingest::kubelet_stats::StatsSummary; | ||
| use crate::ingest::reflectors::{FrozenStores, Stores}; | ||
| use crate::snapshot::indexes::Indexes; | ||
|
|
@@ -38,6 +41,7 @@ pub struct Snapshot { | |
| pub metrics: MetricTables, | ||
| pub indexes: Indexes, | ||
| pub self_health: SelfHealth, | ||
| pub system_agent_snapshot: HashMap<String, Bytes>, | ||
|
Member
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. I'd store the actual arced
|
||
| } | ||
|
|
||
| impl Snapshot { | ||
|
|
@@ -46,6 +50,7 @@ impl Snapshot { | |
| pub fn new( | ||
| stores: Stores, | ||
| kubelet_stats_summary_cache: Cache<String, Arc<MetricsFetcherIngestion<StatsSummary>>>, | ||
| system_agent_cache: Cache<String, Arc<MetricsFetcherIngestion<SystemAgentOutput>>>, | ||
| ) -> Self { | ||
| let instant = Instant::now(); | ||
| let reflector_healths = stores.freeze_healths(); | ||
|
|
@@ -59,13 +64,18 @@ impl Snapshot { | |
| reflector_healths, | ||
| &kubelet_stats_summary_cache, | ||
| ); | ||
| let system_agent_snapshot: HashMap<String, Bytes> = system_agent_cache | ||
| .iter() | ||
| .map(|(name, ingestion)| (name.to_string(), ingestion.payload.0.clone())) | ||
| .collect(); | ||
| Snapshot { | ||
| instant, | ||
| stores, | ||
| owner_graph, | ||
| metrics, | ||
| indexes, | ||
| self_health, | ||
| system_agent_snapshot, | ||
| } | ||
| } | ||
| } | ||
|
|
||
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.
The
String::from_utf8_lossy()on 22 is incompatible with theByteswe store now (we could potentially change the agent output there by altering non-utf8-compliant bytes which would be non-ideal).I'd probably change the handler to return Bytes instead of String and change the last line to
Ok(out.into()).(A test here would be nice but is probably a bit difficult to set up, so you can ignore it for now)