Skip to content

Commit

Permalink
Add build(), build_freezable() and build_frozen() methods to `r…
Browse files Browse the repository at this point in the history
…ecorder::Builder` (#4)

Co-authored-by: Kai Ren <tyranron@gmail.com>
  • Loading branch information
ilslv and tyranron authored Jan 24, 2023
1 parent 1a3222f commit c2e0e73
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@ All user visible changes to this project will be documented in this file. This p



## [0.3.1] · 2023-01-??
[0.3.1]: /../../tree/v0.3.1

[Diff](/../../compare/v0.3.0...v0.3.1)

### Added

- `build()`, `build_freezable()` and `build_frozen()` methods to `recorder::Builder`, allowing to build the resulting `metrics::Recorder` without installing it as `metrics::recorder()`. ([#4])

[#3]: /../../pull/4




## [0.3.0] · 2022-12-11
[0.3.0]: /../../tree/v0.3.0

Expand Down
101 changes: 101 additions & 0 deletions src/recorder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,107 @@ impl<S, L> Builder<S, L> {
})
}

/// Builds a [`Recorder`] out of this [`Builder`] and returns it being
/// wrapped into all the provided [`metrics::Layer`]s.
///
/// # Usage
///
/// Use this method if you want to:
/// - either install the built [`Recorder`] as [`metrics::recorder()`]
/// manually;
/// - or to compose the built [`Recorder`] with some other
/// [`metrics::Recorder`]s (like being able to write into multiple
/// [`prometheus::Registry`]s via [`metrics::layer::Fanout`], for
/// example).
///
/// Otherwise, consider using the [`build_and_install()`] method instead.
///
/// [`build_and_install()`]: Builder::build_and_install
/// [`metrics::layer::Fanout`]: metrics_util::layers::Fanout
/// [`metrics::Layer`]: Layer
pub fn build(self) -> <L as Layer<Recorder<S>>>::Output
where
S: failure::Strategy,
L: Layer<Recorder<S>>,
{
let Self { storage, failure_strategy, layers } = self;
let rec = Recorder {
metrics: Arc::new(metrics_util::registry::Registry::new(
storage.clone(),
)),
storage,
failure_strategy,
};
layers.layer(rec)
}

/// Builds a [`FreezableRecorder`] out of this [`Builder`] and returns it
/// being wrapped into all the provided [`metrics::Layer`]s.
///
/// # Usage
///
/// Use this method if you want to:
/// - either install the built [`FreezableRecorder`] as
/// [`metrics::recorder()`] manually;
/// - or to compose the built [`FreezableRecorder`] with some other
/// [`metrics::Recorder`]s (like being able to write into multiple
/// [`prometheus::Registry`]s via [`metrics::layer::Fanout`], for
/// example).
///
/// Otherwise, consider using the [`build_freezable_and_install()`] method
/// instead.
///
/// [`build_freezable_and_install()`]: Builder::build_freezable_and_install
/// [`metrics::layer::Fanout`]: metrics_util::layers::Fanout
/// [`metrics::Layer`]: Layer
/// [`FreezableRecorder`]: Freezable
pub fn build_freezable(self) -> <L as Layer<freezable::Recorder<S>>>::Output
where
S: failure::Strategy,
L: Layer<freezable::Recorder<S>>,
{
let Self { storage, failure_strategy, layers } = self;
let rec = freezable::Recorder::wrap(Recorder {
metrics: Arc::new(metrics_util::registry::Registry::new(
storage.clone(),
)),
storage,
failure_strategy,
});
layers.layer(rec)
}

/// Builds a [`FrozenRecorder`] out of this [`Builder`] and returns it being
/// wrapped into all the provided [`metrics::Layer`]s.
///
/// # Usage
///
/// Use this method if you want to:
/// - either install the built [`FrozenRecorder`] as [`metrics::recorder()`]
/// manually;
/// - or to compose the built [`FrozenRecorder`] with some other
/// [`metrics::Recorder`]s (like being able to write into multiple
/// [`prometheus::Registry`]s via [`metrics::layer::Fanout`], for
/// example).
///
/// Otherwise, consider using the [`build_frozen_and_install()`] method
/// instead.
///
/// [`build_frozen_and_install()`]: Builder::build_frozen_and_install
/// [`metrics::layer::Fanout`]: metrics_util::layers::Fanout
/// [`metrics::Layer`]: Layer
/// [`FrozenRecorder`]: Frozen
pub fn build_frozen(self) -> <L as Layer<frozen::Recorder<S>>>::Output
where
S: failure::Strategy,
L: Layer<frozen::Recorder<S>>,
{
let Self { storage, failure_strategy, layers } = self;
let rec =
frozen::Recorder { storage: (&storage).into(), failure_strategy };
layers.layer(rec)
}

/// Builds a [`Recorder`] out of this [`Builder`] and tries to install it as
/// [`metrics::recorder()`].
///
Expand Down

0 comments on commit c2e0e73

Please sign in to comment.