Add application name metadata to metrics logs

This commit is contained in:
Olivier 'reivilibre' 2021-11-28 13:17:01 +00:00
parent 21e4cf042e
commit 4f45d2b96d
4 changed files with 17 additions and 5 deletions

View File

@ -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,
}

View File

@ -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 :).");

View File

@ -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 :).");

View File

@ -54,13 +54,16 @@ impl<W: Write + Send + 'static> BareMetricsRecorderCore<W> {
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<W: Write + Send + 'static> BareMetricsRecorderCore<W> {
Ok(())
}
fn run_forever(mut self, rx: Receiver<RecorderMessage>) -> anyhow::Result<()> {
fn run_forever(
mut self,
rx: Receiver<RecorderMessage>,
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)