Skip to content

Commit

Permalink
enhancement(metrics): add support for scoped (local) recorders (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobz committed Dec 23, 2023
1 parent 49d96b1 commit 0e5874d
Show file tree
Hide file tree
Showing 35 changed files with 832 additions and 857 deletions.
40 changes: 25 additions & 15 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Rust Nightly
run: rustup default nightly
- name: Install rustfmt
run: rustup component add rustfmt
- name: Check Formatting
run: cargo fmt --all -- --check
feature-check:
Expand All @@ -30,19 +30,30 @@ jobs:
- name: Check Feature Matrix
run: cargo hack check --all --all-targets --feature-powerset --release
test:
name: Test (rust-toolchain.toml)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Install Protobuf Compiler
run: sudo apt-get install protobuf-compiler
- name: Install Rust Stable
run: rustup default stable
- name: Run Tests
run: cargo test --all-features --workspace
test-matrixed:
name: Test ${{ matrix.rust_version }}
runs-on: ubuntu-latest
strategy:
matrix:
rust_version: ['1.61.0', 'stable', 'nightly']
rust_version: ['stable', 'nightly']
steps:
- uses: actions/checkout@v3
- name: Install Protobuf Compiler
run: sudo apt-get install protobuf-compiler
- name: Install Rust ${{ matrix.rust_version }}
run: rustup default ${{ matrix.rust_version }}
run: rustup install ${{ matrix.rust_version }}
- name: Run Tests
run: cargo test --all-features --workspace --exclude=metrics-observer -- --test-threads=1
run: cargo +${{ matrix.rust_version }} test --all-features --workspace
docs:
runs-on: ubuntu-latest
env:
Expand All @@ -52,9 +63,9 @@ jobs:
- name: Install Protobuf Compiler
run: sudo apt-get install protobuf-compiler
- name: Install Rust Nightly
run: rustup default nightly
run: rustup install nightly
- name: Check Docs
run: cargo doc --all-features --workspace --exclude=metrics-observer --no-deps
run: cargo +nightly doc --all-features --workspace --no-deps
bench:
name: Bench
runs-on: ubuntu-latest
Expand All @@ -65,18 +76,17 @@ jobs:
- name: Install Rust Stable
run: rustup default stable
- name: Run Benchmarks
run: cargo bench --all-features --workspace --exclude=metrics-observer
run: cargo bench --all-features --workspace --exclude=metrics-observer --exclude=metrics-benchmark
clippy:
name: Clippy ${{ matrix.rust_version }}
name: Clippy
runs-on: ubuntu-latest
strategy:
matrix:
rust_version: ['1.61.0', 'stable', 'nightly']
steps:
- uses: actions/checkout@v3
- name: Install Protobuf Compiler
run: sudo apt-get install protobuf-compiler
- name: Install Rust ${{ matrix.rust_version }}
run: rustup default ${{ matrix.rust_version }}
- name: Install Rust Stable
run: rustup default stable
- name: Install Clippy
run: rustup component add clippy
- name: Run Clippy
run: cargo clippy --all-features --workspace --exclude=metrics-observer
run: cargo clippy --all-features --workspace --exclude=metrics-benchmark
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ As well, there are also some community-maintained exporters and other integratio

## MSRV and MSRV policy

Minimum supported Rust version (MSRV) is currently **1.61.0**, enforced by CI.
Minimum supported Rust version (MSRV) is currently **1.65.0**, enforced by CI.

`metrics` will always support _at least_ the latest four versions of stable Rust, based on minor
version releases, and excluding patch versions. Overall, we strive to support older versions where
Expand Down
2 changes: 1 addition & 1 deletion metrics-benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "metrics-benchmark"
version = "0.1.1-alpha.5"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2018"
rust-version = "1.61.0"
rust-version = "1.65.0"
publish = false

[dependencies]
Expand Down
6 changes: 3 additions & 3 deletions metrics-benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use hdrhistogram::Histogram as HdrHistogram;
use log::{error, info};
use metrics::{
counter, gauge, histogram, Counter, Gauge, Histogram, Key, KeyName, Metadata, Recorder,
SharedString, Unit,
SetRecorderError, SharedString, Unit,
};
use metrics_util::registry::{AtomicStorage, Registry};
use portable_atomic::AtomicU64;
Expand Down Expand Up @@ -56,8 +56,8 @@ impl BenchmarkingRecorder {
}

/// Installs this recorder as the global recorder.
pub fn install(self) -> Result<(), metrics::SetRecorderError> {
metrics::set_boxed_recorder(Box::new(self))
pub fn install(self) -> Result<(), SetRecorderError<Self>> {
metrics::set_global_recorder(self)
}
}

Expand Down
7 changes: 6 additions & 1 deletion metrics-exporter-prometheus/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Support for using HTTPS in Push Gateway mode. ([#392](https://github.com/metrics-rs/metrics/pull/392))
- Support for using HTTPS in Push Gateway mode.
([#392](https://github.com/metrics-rs/metrics/pull/392))

### Changed

- Bump MSRV to 1.65.0.

## [0.12.2] - 2023-12-13

Expand Down
2 changes: 1 addition & 1 deletion metrics-exporter-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "metrics-exporter-prometheus"
version = "0.12.2"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2018"
rust-version = "1.61.0"
rust-version = "1.65.0"

license = "MIT"

Expand Down
8 changes: 4 additions & 4 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl PrometheusBuilder {
recorder
};

metrics::set_boxed_recorder(Box::new(recorder))?;
metrics::set_global_recorder(recorder)?;

Ok(())
}
Expand All @@ -384,7 +384,7 @@ impl PrometheusBuilder {
let recorder = self.build_recorder();
let handle = recorder.handle();

metrics::set_boxed_recorder(Box::new(recorder))?;
metrics::set_global_recorder(recorder)?;

Ok(handle)
}
Expand Down Expand Up @@ -560,9 +560,9 @@ fn basic_auth(username: &str, password: Option<&str>) -> HeaderValue {
let mut buf = b"Basic ".to_vec();
{
let mut encoder = EncoderWriter::new(&mut buf, &BASE64_STANDARD);
let _ = write!(encoder, "{username}:");
write!(encoder, "{username}:").expect("should not fail to encode username");
if let Some(password) = password {
let _ = write!(encoder, "{password}");
write!(encoder, "{password}").expect("should not fail to encode password");
}
}
let mut header = HeaderValue::from_bytes(&buf).expect("base64 is always valid HeaderValue");
Expand Down
4 changes: 2 additions & 2 deletions metrics-exporter-prometheus/src/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashMap;

use crate::distribution::Distribution;
use crate::{distribution::Distribution, PrometheusRecorder};

use crate::formatting::sanitize_metric_name;
use indexmap::IndexMap;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub enum BuildError {

/// Installing the recorder did not succeed.
#[error("failed to install exporter as global recorder: {0}")]
FailedToSetGlobalRecorder(#[from] SetRecorderError),
FailedToSetGlobalRecorder(#[from] SetRecorderError<PrometheusRecorder>),

/// The given address could not be parsed successfully as an IP address/subnet.
#[error("failed to parse address as a valid IP address/subnet: {0}")]
Expand Down
3 changes: 0 additions & 3 deletions metrics-exporter-prometheus/src/recorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,6 @@ impl Inner {

/// A Prometheus recorder.
///
/// This recorder should be composed with other recorders or installed globally via
/// [`metrics::set_boxed_recorder`].
///
/// Most users will not need to interact directly with the recorder, and can simply deal with the
/// builder methods on [`PrometheusBuilder`](crate::PrometheusBuilder) for building and installing
/// the recorder/exporter.
Expand Down
4 changes: 4 additions & 0 deletions metrics-exporter-tcp/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased] - ReleaseDate

### Changed

- Bump MSRV to 1.65.0.

## [0.8.0] - 2023-04-16

### Changed
Expand Down
3 changes: 2 additions & 1 deletion metrics-exporter-tcp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "metrics-exporter-tcp"
version = "0.8.0"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2018"
rust-version = "1.61.0"
rust-version = "1.65.0"

license = "MIT"

Expand All @@ -27,6 +27,7 @@ tracing = { version = "0.1", default-features = false, features = ["attributes"]

[build-dependencies]
prost-build = "0.11"
home = "=0.5.5"

[dev-dependencies]
quanta = "0.12"
Expand Down
9 changes: 4 additions & 5 deletions metrics-exporter-tcp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub enum Error {
Io(io::Error),

/// Installing the recorder did not succeed.
Recorder(SetRecorderError),
Recorder(SetRecorderError<TcpRecorder>),
}

impl From<io::Error> for Error {
Expand All @@ -113,8 +113,8 @@ impl From<io::Error> for Error {
}
}

impl From<SetRecorderError> for Error {
fn from(e: SetRecorderError) -> Self {
impl From<SetRecorderError<TcpRecorder>> for Error {
fn from(e: SetRecorderError<TcpRecorder>) -> Self {
Error::Recorder(e)
}
}
Expand Down Expand Up @@ -284,8 +284,7 @@ impl TcpBuilder {
/// installing the recorder as the global recorder.
pub fn install(self) -> Result<(), Error> {
let recorder = self.build()?;
metrics::set_boxed_recorder(Box::new(recorder))?;
Ok(())
metrics::set_global_recorder(recorder).map_err(Into::into)
}

/// Builds and installs the exporter, but returns the recorder.
Expand Down
4 changes: 4 additions & 0 deletions metrics-observer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
order, instead of only trying the first and then giving up.
([#429](https://github.com/metrics-rs/metrics/pull/429))

### Changed

- Bump MSRV to 1.65.0.

## [0.2.0] - 2023-04-16

### Added
Expand Down
3 changes: 2 additions & 1 deletion metrics-observer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "metrics-observer"
version = "0.2.0"
authors = ["Toby Lawrence <toby@nuclearfurnace.com>"]
edition = "2018"
rust-version = "1.61.0"
rust-version = "1.65.0"

license = "MIT"

Expand All @@ -29,3 +29,4 @@ chrono = { version = "0.4", default-features = false, features = ["clock"] }

[build-dependencies]
prost-build = "0.11"
home = "=0.5.5"
7 changes: 6 additions & 1 deletion metrics-tracing-context/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Support for dynamism using `tracing::Span::record` to add label values. ([#408](https://github.com/metrics-rs/metrics/pull/408))
- Support for dynamism using `tracing::Span::record` to add label values.
([#408](https://github.com/metrics-rs/metrics/pull/408))

### Changed

- Bump MSRV to 1.65.0.

## [0.14.0] - 2023-04-16

Expand Down
2 changes: 1 addition & 1 deletion metrics-tracing-context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "metrics-tracing-context"
version = "0.14.0"
authors = ["MOZGIII <mike-n@narod.ru>"]
edition = "2018"
rust-version = "1.61.0"
rust-version = "1.65.0"

license = "MIT"

Expand Down
4 changes: 2 additions & 2 deletions metrics-tracing-context/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
//! // Prepare metrics.
//! # let my_recorder = DebuggingRecorder::new();
//! let recorder = TracingContextLayer::all().layer(my_recorder);
//! metrics::set_boxed_recorder(Box::new(recorder)).unwrap();
//! metrics::set_global_recorder(recorder).unwrap();
//! ```
//!
//! Then emit some metrics within spans and see the labels being injected!
Expand All @@ -37,7 +37,7 @@
//! # tracing::subscriber::set_global_default(subscriber).unwrap();
//! # let myrecorder = DebuggingRecorder::new();
//! # let recorder = TracingContextLayer::all().layer(myrecorder);
//! # metrics::set_boxed_recorder(Box::new(recorder)).unwrap();
//! # metrics::set_global_recorder(recorder).unwrap();
//! use tracing::{span, Level};
//! use metrics::counter;
//!
Expand Down
Loading

0 comments on commit 0e5874d

Please sign in to comment.