Split metrics into crate

This commit is contained in:
asonix 2023-03-22 22:25:19 -05:00
parent 4d4a70d290
commit 193c02aa73
6 changed files with 86 additions and 42 deletions

View file

@ -1,7 +1,7 @@
[package] [package]
name = "background-jobs" name = "background-jobs"
description = "Background Jobs implemented with actix and futures" description = "Background Jobs implemented with actix and futures"
version = "0.14.0" version = "0.14.1"
license = "AGPL-3.0" license = "AGPL-3.0"
authors = ["asonix <asonix@asonix.dog>"] authors = ["asonix <asonix@asonix.dog>"]
repository = "https://git.asonix.dog/asonix/background-jobs" repository = "https://git.asonix.dog/asonix/background-jobs"
@ -13,6 +13,7 @@ edition = "2021"
members = [ members = [
"jobs-actix", "jobs-actix",
"jobs-core", "jobs-core",
"jobs-metrics",
"jobs-sled", "jobs-sled",
"examples/basic-example", "examples/basic-example",
"examples/long-example", "examples/long-example",
@ -22,7 +23,7 @@ members = [
] ]
[features] [features]
default = ["background-jobs-actix"] default = ["background-jobs-actix", "background-jobs-metrics"]
completion-logging = [ completion-logging = [
"background-jobs-core/completion-logging", "background-jobs-core/completion-logging",
"error-logging", "error-logging",
@ -30,8 +31,6 @@ completion-logging = [
error-logging = ["background-jobs-core/error-logging"] error-logging = ["background-jobs-core/error-logging"]
[dependencies] [dependencies]
metrics = "0.20.1"
metrics-util = "0.14.0"
[dependencies.background-jobs-core] [dependencies.background-jobs-core]
version = "0.14.0" version = "0.14.0"
@ -41,3 +40,8 @@ path = "jobs-core"
version = "0.14.0" version = "0.14.0"
path = "jobs-actix" path = "jobs-actix"
optional = true optional = true
[dependencies.background-jobs-metrics]
version = "0.14.0"
path = "jobs-metrics"
optional = true

15
jobs-metrics/Cargo.toml Normal file
View file

@ -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 <asonix@asonix.dog>"]
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"

55
jobs-metrics/src/lib.rs Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
//! 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<StatsHandle, SetRecorderError> {
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()
}

View file

@ -16,6 +16,7 @@ pub struct StatsHandle {
storage: Arc<StatsStorageInner>, storage: Arc<StatsStorageInner>,
} }
/// Recorder type for recording background jobs metrics
pub struct StatsRecorder { pub struct StatsRecorder {
registry: Registry<Key, StatsStorage>, registry: Registry<Key, StatsStorage>,
} }
@ -88,7 +89,7 @@ impl StatsHandle {
/// Get the current stats about the background jobs processing /// Get the current stats about the background jobs processing
/// ///
/// ```rust /// ```rust
/// # let (recorder, handle) = background_jobs::metrics::StatsRecorder::build(); /// # let (recorder, handle) = background_jobs_metrics::StatsRecorder::build();
/// println!("{:?}", handle.get()); /// println!("{:?}", handle.get());
/// ``` /// ```
pub fn get(&self) -> Stats { pub fn get(&self) -> Stats {
@ -100,7 +101,7 @@ impl StatsRecorder {
/// Install the stats recorder into the process /// Install the stats recorder into the process
/// ///
/// ```rust /// ```rust
/// # use background_jobs::metrics::StatsRecorder; /// # use background_jobs_metrics::StatsRecorder;
/// StatsRecorder::install().expect("Failed to install recorder"); /// StatsRecorder::install().expect("Failed to install recorder");
/// ``` /// ```
pub fn install() -> Result<StatsHandle, SetRecorderError> { pub fn install() -> Result<StatsHandle, SetRecorderError> {
@ -117,7 +118,7 @@ impl StatsRecorder {
/// addition to another recorder /// addition to another recorder
/// ///
/// ```rust /// ```rust
/// # use background_jobs::metrics::StatsRecorder; /// # use background_jobs_metrics::StatsRecorder;
/// let (jobs_recorder, handle) = StatsRecorder::build(); /// let (jobs_recorder, handle) = StatsRecorder::build();
/// ///
/// let recorder = metrics_util::layers::FanoutBuilder::default() /// let recorder = metrics_util::layers::FanoutBuilder::default()

View file

@ -161,44 +161,13 @@
//! `background-jobs-core` crate, which provides the Job trait, as well as some //! `background-jobs-core` crate, which provides the Job trait, as well as some
//! other useful types for implementing a jobs processor and job store. //! other useful types for implementing a jobs processor and job store.
mod recorder;
pub use background_jobs_core::{Backoff, Job, MaxRetries}; pub use background_jobs_core::{Backoff, Job, MaxRetries};
#[cfg(feature = "background-jobs-metrics")]
pub mod metrics { pub mod metrics {
//! Types for collecting stats from background-jobs pub use background_jobs_metrics::{
pub use metrics::SetRecorderError; build, install, JobStat, SetRecorderError, Stats, StatsHandle, StatsRecorder,
};
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<StatsHandle, SetRecorderError> {
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 mod dev { pub mod dev {