Skip to content

Commit

Permalink
Rust metric instrumentation helper for system and current process.
Browse files Browse the repository at this point in the history
  • Loading branch information
zakstucke committed May 21, 2024
1 parent 79122ae commit a4dbe62
Show file tree
Hide file tree
Showing 7 changed files with 440 additions and 16 deletions.
23 changes: 12 additions & 11 deletions .zetch.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion opencollector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ exporters:
stream-name: default
# Writes all opentelemetry logs, traces, metrics to a file, useful for testing:
file/debug_file_writing:
path: /home/runner/work/bitbazaar/bitbazaar/logs/otlp_telemetry_out.log
path: /Users/zak/z/code/bitbazaar/logs/otlp_telemetry_out.log
rotation:
max_megabytes: 10
max_days: 3
Expand Down
64 changes: 64 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ log-filter = ["dep:regex"]
chrono = ['dep:chrono', 'dep:chrono-humanize']
timing = ['dep:comfy-table', 'chrono']
cli = ['dep:normpath', 'dep:conch-parser', 'dep:homedir', 'chrono', 'dep:strum']
system = ['dep:sysinfo']
redis = [
'dep:tokio',
'dep:deadpool-redis',
Expand Down Expand Up @@ -118,6 +119,9 @@ opentelemetry-otlp = { version = "0.14", default-features = false, features = [
], optional = true }
opentelemetry-semantic-conventions = { version = "0.13.0", optional = true }

# FEAT: system:
sysinfo = { version = "0.30", optional = true }

[target.'cfg(target_arch = "wasm32")'.dependencies]
tracing-subscriber-wasm = "0.1.0"

Expand Down
17 changes: 13 additions & 4 deletions rust/bitbazaar/log/global_log/global_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,19 @@ pub fn record_exception(message: impl Into<String>, stacktrace: impl Into<String
///
/// If the name is empty, then an implementation defined default name will
/// be used instead.
pub fn meter(
name: impl Into<std::borrow::Cow<'static, str>>,
) -> Result<opentelemetry::metrics::Meter, AnyErr> {
get_global()?.meter(name)
pub fn meter(name: impl Into<std::borrow::Cow<'static, str>>) -> opentelemetry::metrics::Meter {
opentelemetry::global::meter(name)
}

#[cfg(any(feature = "opentelemetry-grpc", feature = "opentelemetry-http"))]
/// Returns the default [`opentelemetry::metrics::Meter`] for the app, labelled "default".
pub fn global_meter() -> &'static opentelemetry::metrics::Meter {
use once_cell::sync::Lazy;

static GLOBAL_METER: Lazy<opentelemetry::metrics::Meter> =
Lazy::new(|| opentelemetry::global::meter("default"));

&GLOBAL_METER
}

#[cfg(any(feature = "opentelemetry-grpc", feature = "opentelemetry-http"))]
Expand Down
25 changes: 25 additions & 0 deletions rust/bitbazaar/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,32 @@ mod macros;
#[cfg(any(feature = "opentelemetry-grpc", feature = "opentelemetry-http"))]
mod ot_tracing_bridge;

#[cfg(all(
feature = "system",
any(feature = "opentelemetry-grpc", feature = "opentelemetry-http")
))]
mod system_and_process_metrics;
pub use global_log::{global_fns::*, GlobalLog, GlobalLogBuilder};
#[cfg(all(
feature = "system",
any(feature = "opentelemetry-grpc", feature = "opentelemetry-http")
))]
pub use system_and_process_metrics::*;

#[cfg(any(feature = "opentelemetry-grpc", feature = "opentelemetry-http"))]
/// Opentelemetry types that might be needed downstream.
/// The aim is to avoid the user having to depend on opentelemetry crates directly.
pub mod otlp {
pub use opentelemetry::{Key, KeyValue, StringValue, Value};

/// Otlp metric types.
pub mod metrics {
pub use opentelemetry::metrics::{
Counter, Histogram, ObservableCounter, ObservableGauge, ObservableUpDownCounter,
SyncCounter, SyncHistogram, SyncUpDownCounter, Unit, UpDownCounter,
};
}
}

#[cfg(test)]
mod tests {
Expand Down
Loading

0 comments on commit a4dbe62

Please sign in to comment.