From 4f45d2b96d9772111cdeb07afae5e49ad03f5694 Mon Sep 17 00:00:00 2001 From: Olivier 'reivilibre Date: Sun, 28 Nov 2021 13:17:01 +0000 Subject: [PATCH] Add application name metadata to metrics logs --- bare-metrics-core/src/structures.rs | 3 +++ bare-metrics-recorder/examples/counter.rs | 3 ++- bare-metrics-recorder/examples/histogram.rs | 2 +- bare-metrics-recorder/src/recording.rs | 14 +++++++++++--- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/bare-metrics-core/src/structures.rs b/bare-metrics-core/src/structures.rs index 933696b..5d124a0 100644 --- a/bare-metrics-core/src/structures.rs +++ b/bare-metrics-core/src/structures.rs @@ -27,6 +27,9 @@ impl UnixTimestampMilliseconds { pub struct LogHeader { /// String describing the version of this metrics log file. pub bare_metrics_version: String, + /// Name of the application that emitted this metrics log file. + /// Useful for auto-detection of rules for e.g. the GUI. + pub application_name: String, /// Unix timestamp (milliseconds) describing the start of the metrics. pub start_time: UnixTimestampMilliseconds, } diff --git a/bare-metrics-recorder/examples/counter.rs b/bare-metrics-recorder/examples/counter.rs index 683d273..5f260bc 100644 --- a/bare-metrics-recorder/examples/counter.rs +++ b/bare-metrics-recorder/examples/counter.rs @@ -5,7 +5,8 @@ use std::time::Duration; pub fn main() -> anyhow::Result<()> { let (shard, stopper) = - BareMetricsRecorderCore::new(File::create("/tmp/counter-example.baremetrics")?).start()?; + BareMetricsRecorderCore::new(File::create("/tmp/counter-example.baremetrics")?) + .start("counterdemo".to_string())?; shard.install_as_metrics_recorder()?; register_counter!("my_counter", Unit::BitsPerSecond, "Some counter thing :)."); diff --git a/bare-metrics-recorder/examples/histogram.rs b/bare-metrics-recorder/examples/histogram.rs index 91b99bc..0b86595 100644 --- a/bare-metrics-recorder/examples/histogram.rs +++ b/bare-metrics-recorder/examples/histogram.rs @@ -6,7 +6,7 @@ use std::time::Duration; pub fn main() -> anyhow::Result<()> { let (shard, stopper) = BareMetricsRecorderCore::new(File::create("/tmp/histogram-example.baremetrics")?) - .start()?; + .start("histogramdemo".to_string())?; shard.install_as_metrics_recorder()?; register_histogram!("my_hist", Unit::BitsPerSecond, "Some thing :)."); diff --git a/bare-metrics-recorder/src/recording.rs b/bare-metrics-recorder/src/recording.rs index ddd355f..a633804 100644 --- a/bare-metrics-recorder/src/recording.rs +++ b/bare-metrics-recorder/src/recording.rs @@ -54,13 +54,16 @@ impl BareMetricsRecorderCore { self } - pub fn start(self) -> anyhow::Result<(BareMetricsRecorderShard, BareMetricsRecorderStopper)> { + pub fn start( + self, + application_name: String, + ) -> anyhow::Result<(BareMetricsRecorderShard, BareMetricsRecorderStopper)> { let (tx, rx) = crossbeam_channel::bounded(self.queue_size); let handle = std::thread::Builder::new() .name("bare-metrics-recorder".to_string()) .spawn(move || { - if let Err(e) = self.run_forever(rx) { + if let Err(e) = self.run_forever(rx, application_name) { error!("Recorder erred: {:?}", e); } })?; @@ -97,13 +100,18 @@ impl BareMetricsRecorderCore { Ok(()) } - fn run_forever(mut self, rx: Receiver) -> anyhow::Result<()> { + fn run_forever( + mut self, + rx: Receiver, + application_name: String, + ) -> anyhow::Result<()> { // Write out the header. let start = SystemTime::now(); serde_bare::to_writer( &mut self.writer, &LogHeader { bare_metrics_version: get_supported_version().to_string(), + application_name, start_time: UnixTimestampMilliseconds( start .duration_since(std::time::UNIX_EPOCH)