Skip to content

Commit

Permalink
ringlog: make metrics optional (#118)
Browse files Browse the repository at this point in the history
Updates ringlog to make metrics optional.
  • Loading branch information
brayniac authored Jun 26, 2024
1 parent 11f8aef commit 09e8616
Show file tree
Hide file tree
Showing 7 changed files with 173 additions and 113 deletions.
7 changes: 5 additions & 2 deletions ringlog/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ringlog"
version = "0.7.0"
version = "0.8.0"
edition = "2021"
license = "Apache-2.0"
authors = ["Brian Martin <brian@pelikan.io>"]
Expand All @@ -11,6 +11,9 @@ repository = "https://github.com/pelikan-io/rustcommon"
[dependencies]
ahash = "0.8.0"
clocksource = { version = "0.8.0", path = "../clocksource" }
metriken = "0.7.0"
log = { version = "0.4.17", features = ["std"] }
metriken = { version = "0.7.0", optional = true }
mpmc = "0.1.6"

[features]
metrics = ["metriken"]
101 changes: 8 additions & 93 deletions ringlog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
pub use log::*;

mod format;
#[macro_use]
mod macros;
mod multi;
mod nop;
mod outputs;
Expand All @@ -52,88 +54,17 @@ pub use sampling::*;
pub use single::*;
pub use traits::*;

#[cfg(feature = "metrics")]
mod metrics;

#[cfg(feature = "metrics")]
use metrics::*;

use clocksource::datetime::DateTime;
use mpmc::Queue;

pub(crate) type LogBuffer = Vec<u8>;

use metriken::{metric, Counter, Gauge};

#[metric(name = "log_create", description = "logging targets initialized")]
pub static LOG_CREATE: Counter = Counter::new();

#[metric(
name = "log_create_ex",
description = "number of exceptions while initializing logging targets"
)]
pub static LOG_CREATE_EX: Counter = Counter::new();

#[metric(name = "log_destroy", description = "logging targets destroyed")]
pub static LOG_DESTROY: Counter = Counter::new();

#[metric(name = "log_curr", description = "current number of logging targets")]
pub static LOG_CURR: Gauge = Gauge::new();

#[metric(
name = "log_open",
description = "number of logging destinations which have been opened"
)]
pub static LOG_OPEN: Counter = Counter::new();

#[metric(
name = "log_open_ex",
description = "number of exceptions while opening logging destinations"
)]
pub static LOG_OPEN_EX: Counter = Counter::new();

#[metric(
name = "log_write",
description = "number of writes to all logging destinations"
)]
pub static LOG_WRITE: Counter = Counter::new();

#[metric(
name = "log_write_byte",
description = "number of bytes written to all logging destinations"
)]
pub static LOG_WRITE_BYTE: Counter = Counter::new();

#[metric(
name = "log_write_ex",
description = "number of exceptions while writing to logging destinations"
)]
pub static LOG_WRITE_EX: Counter = Counter::new();

#[metric(
name = "log_skip",
description = "number of log messages skipped due to sampling policy"
)]
pub static LOG_SKIP: Counter = Counter::new();

#[metric(
name = "log_drop",
description = "number of log messages dropped due to full queues"
)]
pub static LOG_DROP: Counter = Counter::new();

#[metric(
name = "log_drop_byte",
description = "number of bytes dropped due to full queues"
)]
pub static LOG_DROP_BYTE: Counter = Counter::new();

#[metric(
name = "log_flush",
description = "number of times logging destinations have been flushed"
)]
pub static LOG_FLUSH: Counter = Counter::new();

#[metric(
name = "log_flush_ex",
description = "number of times logging destinations have been flushed"
)]
pub static LOG_FLUSH_EX: Counter = Counter::new();

/// A type which implements an asynchronous logging backend.
pub struct RingLog {
pub(crate) logger: Box<dyn Log>,
Expand All @@ -152,19 +83,3 @@ impl RingLog {
self.drain
}
}

#[macro_export]
macro_rules! fatal {
() => (
error!();
std::process::exit(1);
);
($fmt:expr) => (
error!($fmt);
std::process::exit(1);
);
($fmt:expr, $($arg:tt)*) => (
error!($fmt, $($arg)*);
std::process::exit(1);
);
}
26 changes: 26 additions & 0 deletions ringlog/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#[macro_export]
/// Logs a fatal error and terminates the program.
macro_rules! fatal {
() => (
error!();
std::process::exit(1);
);
($fmt:expr) => (
error!($fmt);
std::process::exit(1);
);
($fmt:expr, $($arg:tt)*) => (
error!($fmt, $($arg)*);
std::process::exit(1);
);
}

#[cfg(feature = "metrics")]
macro_rules! metrics {
{ $( $tt:tt )* } => { $( $tt )* }
}

#[cfg(not(feature = "metrics"))]
macro_rules! metrics {
{ $( $tt:tt)* } => {}
}
76 changes: 76 additions & 0 deletions ringlog/src/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use metriken::{metric, Counter, Gauge};

#[metric(name = "log_create", description = "logging targets initialized")]
pub static LOG_CREATE: Counter = Counter::new();

#[metric(
name = "log_create_ex",
description = "number of exceptions while initializing logging targets"
)]
pub static LOG_CREATE_EX: Counter = Counter::new();

#[metric(name = "log_destroy", description = "logging targets destroyed")]
pub static LOG_DESTROY: Counter = Counter::new();

#[metric(name = "log_curr", description = "current number of logging targets")]
pub static LOG_CURR: Gauge = Gauge::new();

#[metric(
name = "log_open",
description = "number of logging destinations which have been opened"
)]
pub static LOG_OPEN: Counter = Counter::new();

#[metric(
name = "log_open_ex",
description = "number of exceptions while opening logging destinations"
)]
pub static LOG_OPEN_EX: Counter = Counter::new();

#[metric(
name = "log_write",
description = "number of writes to all logging destinations"
)]
pub static LOG_WRITE: Counter = Counter::new();

#[metric(
name = "log_write_byte",
description = "number of bytes written to all logging destinations"
)]
pub static LOG_WRITE_BYTE: Counter = Counter::new();

#[metric(
name = "log_write_ex",
description = "number of exceptions while writing to logging destinations"
)]
pub static LOG_WRITE_EX: Counter = Counter::new();

#[metric(
name = "log_skip",
description = "number of log messages skipped due to sampling policy"
)]
pub static LOG_SKIP: Counter = Counter::new();

#[metric(
name = "log_drop",
description = "number of log messages dropped due to full queues"
)]
pub static LOG_DROP: Counter = Counter::new();

#[metric(
name = "log_drop_byte",
description = "number of bytes dropped due to full queues"
)]
pub static LOG_DROP_BYTE: Counter = Counter::new();

#[metric(
name = "log_flush",
description = "number of times logging destinations have been flushed"
)]
pub static LOG_FLUSH: Counter = Counter::new();

#[metric(
name = "log_flush_ex",
description = "number of times logging destinations have been flushed"
)]
pub static LOG_FLUSH_EX: Counter = Counter::new();
21 changes: 17 additions & 4 deletions ringlog/src/outputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,17 @@ impl File {
/// file. When the size of the live log is exceeded, it will automatically
/// be rotated to the backup path.
pub fn new<T: AsRef<Path>>(active: T, backup: T, max_size: u64) -> Result<Self, Error> {
LOG_OPEN.increment();
metrics! {
LOG_OPEN.increment();
}

let file = match std::fs::File::create(active.as_ref()) {
Ok(f) => f,
Err(e) => {
LOG_OPEN_EX.increment();
metrics! {
LOG_OPEN_EX.increment();
}

return Err(e);
}
};
Expand All @@ -111,11 +117,18 @@ impl File {
std::fs::rename(&self.active, &self.backup)?;

// create a new file for the live log
LOG_OPEN.increment();

metrics! {
LOG_OPEN.increment();
}

let file = match std::fs::File::create(&self.active) {
Ok(f) => f,
Err(e) => {
LOG_OPEN_EX.increment();
metrics! {
LOG_OPEN_EX.increment();
}

return Err(e);
}
};
Expand Down
5 changes: 4 additions & 1 deletion ringlog/src/sampling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ impl Log for SamplingLogger {
let count = self.counter.fetch_add(1, Ordering::Relaxed);

// if this is the Nth message, we should log it
#[allow(clippy::needless_else)]
if (count % self.sample) == 0 {
self.logger.log(record)
} else {
LOG_SKIP.increment();
metrics! {
LOG_SKIP.increment();
}
}
}

Expand Down
Loading

0 comments on commit 09e8616

Please sign in to comment.