From 193c02aa73e943f449195a364e79f3a1a016fcde Mon Sep 17 00:00:00 2001 From: asonix Date: Wed, 22 Mar 2023 22:25:19 -0500 Subject: [PATCH] Split metrics into crate --- Cargo.toml | 12 +++-- jobs-metrics/Cargo.toml | 15 ++++++ jobs-metrics/src/lib.rs | 55 ++++++++++++++++++++ {src => jobs-metrics/src}/recorder.rs | 7 +-- {src => jobs-metrics/src}/recorder/bucket.rs | 0 src/lib.rs | 39 ++------------ 6 files changed, 86 insertions(+), 42 deletions(-) create mode 100644 jobs-metrics/Cargo.toml create mode 100644 jobs-metrics/src/lib.rs rename {src => jobs-metrics/src}/recorder.rs (97%) rename {src => jobs-metrics/src}/recorder/bucket.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 502d90c..713b15c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "background-jobs" description = "Background Jobs implemented with actix and futures" -version = "0.14.0" +version = "0.14.1" license = "AGPL-3.0" authors = ["asonix "] repository = "https://git.asonix.dog/asonix/background-jobs" @@ -13,6 +13,7 @@ edition = "2021" members = [ "jobs-actix", "jobs-core", + "jobs-metrics", "jobs-sled", "examples/basic-example", "examples/long-example", @@ -22,7 +23,7 @@ members = [ ] [features] -default = ["background-jobs-actix"] +default = ["background-jobs-actix", "background-jobs-metrics"] completion-logging = [ "background-jobs-core/completion-logging", "error-logging", @@ -30,8 +31,6 @@ completion-logging = [ error-logging = ["background-jobs-core/error-logging"] [dependencies] -metrics = "0.20.1" -metrics-util = "0.14.0" [dependencies.background-jobs-core] version = "0.14.0" @@ -41,3 +40,8 @@ path = "jobs-core" version = "0.14.0" path = "jobs-actix" optional = true + +[dependencies.background-jobs-metrics] +version = "0.14.0" +path = "jobs-metrics" +optional = true diff --git a/jobs-metrics/Cargo.toml b/jobs-metrics/Cargo.toml new file mode 100644 index 0000000..a8e39d6 --- /dev/null +++ b/jobs-metrics/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "background-jobs-metrics" +description = "Background Jobs implemented with actix and futures - metrics subscriber" +version = "0.14.0" +license = "AGPL-3.0" +authors = ["asonix "] +repository = "https://git.asonix.dog/asonix/background-jobs" +keywords = ["jobs", "processor", "actix"] +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +metrics = "0.20.1" +metrics-util = "0.14.0" diff --git a/jobs-metrics/src/lib.rs b/jobs-metrics/src/lib.rs new file mode 100644 index 0000000..8d0805b --- /dev/null +++ b/jobs-metrics/src/lib.rs @@ -0,0 +1,55 @@ +/* + * This file is part of Background Jobs. + * + * Copyright © 2023 Riley Trautman + * + * Background Jobs is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Background Jobs is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Background Jobs. If not, see . + */ + +//! Types for collecting stats from background-jobs + +mod recorder; + +pub use metrics::SetRecorderError; + +pub use recorder::{JobStat, Stats, StatsHandle, StatsRecorder}; + +/// Install the stats recorder into the process +/// +/// ```rust +/// background_jobs_metrics::install().expect("Failed to install recorder"); +/// ``` +pub fn install() -> Result { + StatsRecorder::install() +} + +/// Build the stats recorder and fetch the handle. +/// +/// This can be used in conjunction with `metrics_util::layers::FanoutBuilder` to add it in +/// addition to another recorder +/// +/// ```rust +/// let (jobs_recorder, handle) = background_jobs_metrics::build(); +/// +/// let recorder = metrics_util::layers::FanoutBuilder::default() +/// .add_recorder(jobs_recorder) +/// .build(); +/// +/// metrics::set_boxed_recorder(Box::new(recorder)).expect("Failed to set recorder"); +/// +/// println!("{:?}", handle.get()); +/// ``` +pub fn build() -> (StatsRecorder, StatsHandle) { + StatsRecorder::build() +} diff --git a/src/recorder.rs b/jobs-metrics/src/recorder.rs similarity index 97% rename from src/recorder.rs rename to jobs-metrics/src/recorder.rs index 4171c61..2a76568 100644 --- a/src/recorder.rs +++ b/jobs-metrics/src/recorder.rs @@ -16,6 +16,7 @@ pub struct StatsHandle { storage: Arc, } +/// Recorder type for recording background jobs metrics pub struct StatsRecorder { registry: Registry, } @@ -88,7 +89,7 @@ impl StatsHandle { /// Get the current stats about the background jobs processing /// /// ```rust - /// # let (recorder, handle) = background_jobs::metrics::StatsRecorder::build(); + /// # let (recorder, handle) = background_jobs_metrics::StatsRecorder::build(); /// println!("{:?}", handle.get()); /// ``` pub fn get(&self) -> Stats { @@ -100,7 +101,7 @@ impl StatsRecorder { /// Install the stats recorder into the process /// /// ```rust - /// # use background_jobs::metrics::StatsRecorder; + /// # use background_jobs_metrics::StatsRecorder; /// StatsRecorder::install().expect("Failed to install recorder"); /// ``` pub fn install() -> Result { @@ -117,7 +118,7 @@ impl StatsRecorder { /// addition to another recorder /// /// ```rust - /// # use background_jobs::metrics::StatsRecorder; + /// # use background_jobs_metrics::StatsRecorder; /// let (jobs_recorder, handle) = StatsRecorder::build(); /// /// let recorder = metrics_util::layers::FanoutBuilder::default() diff --git a/src/recorder/bucket.rs b/jobs-metrics/src/recorder/bucket.rs similarity index 100% rename from src/recorder/bucket.rs rename to jobs-metrics/src/recorder/bucket.rs diff --git a/src/lib.rs b/src/lib.rs index 4a98797..c116d11 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -161,44 +161,13 @@ //! `background-jobs-core` crate, which provides the Job trait, as well as some //! other useful types for implementing a jobs processor and job store. -mod recorder; - pub use background_jobs_core::{Backoff, Job, MaxRetries}; +#[cfg(feature = "background-jobs-metrics")] pub mod metrics { - //! Types for collecting stats from background-jobs - pub use metrics::SetRecorderError; - - pub use super::recorder::{JobStat, Stats, StatsHandle, StatsRecorder}; - - /// Install the stats recorder into the process - /// - /// ```rust - /// background_jobs::metrics::install().expect("Failed to install recorder"); - /// ``` - pub fn install() -> Result { - StatsRecorder::install() - } - - /// Build the stats recorder and fetch the handle. - /// - /// This can be used in conjunction with `metrics_util::layers::FanoutBuilder` to add it in - /// addition to another recorder - /// - /// ```rust - /// let (jobs_recorder, handle) = background_jobs::metrics::build(); - /// - /// let recorder = metrics_util::layers::FanoutBuilder::default() - /// .add_recorder(jobs_recorder) - /// .build(); - /// - /// metrics::set_boxed_recorder(Box::new(recorder)).expect("Failed to set recorder"); - /// - /// println!("{:?}", handle.get()); - /// ``` - pub fn build() -> (StatsRecorder, StatsHandle) { - StatsRecorder::build() - } + pub use background_jobs_metrics::{ + build, install, JobStat, SetRecorderError, Stats, StatsHandle, StatsRecorder, + }; } pub mod dev {