From d459db80848318426d1b1c02e4fa05a8d296535a Mon Sep 17 00:00:00 2001 From: Toby Lawrence Date: Fri, 5 Jul 2019 21:14:08 -0400 Subject: [PATCH] Add label support + rename crates. (#27) --- COPYRIGHT | 4 +- Cargo.toml | 2 +- metrics-core/src/lib.rs | 250 ++++++- metrics-exporter-http/Cargo.toml | 2 +- metrics-exporter-http/src/lib.rs | 1 + metrics-exporter-log/Cargo.toml | 2 +- metrics-exporter-log/src/lib.rs | 1 + metrics-facade/Cargo.toml | 27 - metrics-facade/README.md | 22 - metrics-facade/examples/basic.rs | 45 -- metrics-facade/src/lib.rs | 334 --------- metrics-facade/src/macros.rs | 112 --- metrics-recorder-prometheus/Cargo.toml | 2 +- metrics-recorder-prometheus/src/lib.rs | 120 +++- metrics-recorder-text/Cargo.toml | 2 +- metrics-recorder-text/src/lib.rs | 66 +- .../.gitignore | 0 {metrics => metrics-runtime}/CHANGELOG.md | 0 .../CODE_OF_CONDUCT.md | 0 metrics-runtime/Cargo.toml | 55 ++ {metrics => metrics-runtime}/LICENSE | 0 metrics-runtime/README.md | 44 ++ .../benches/histogram.rs | 2 +- .../examples/benchmark.rs | 53 +- .../examples/facade.rs | 42 +- .../examples/stdbenchmark.rs | 0 {metrics => metrics-runtime}/src/builder.rs | 0 {metrics => metrics-runtime}/src/common.rs | 76 +- {metrics => metrics-runtime}/src/config.rs | 9 + {metrics => metrics-runtime}/src/control.rs | 0 metrics-runtime/src/data/counter.rs | 24 + .../src/data/gauge.rs | 8 +- .../src/data/histogram.rs | 8 +- {metrics => metrics-runtime}/src/data/mod.rs | 0 .../src/data/snapshot.rs | 57 +- {metrics => metrics-runtime}/src/exporters.rs | 0 {metrics => metrics-runtime}/src/helper.rs | 0 metrics-runtime/src/lib.rs | 262 +++++++ {metrics => metrics-runtime}/src/receiver.rs | 10 +- {metrics => metrics-runtime}/src/recorders.rs | 0 .../src/registry/metric.rs | 37 +- .../src/registry/mod.rs | 0 .../src/registry/scope.rs | 19 +- {metrics => metrics-runtime}/src/scopes.rs | 2 +- metrics-runtime/src/sink.rs | 656 ++++++++++++++++++ metrics-util/Cargo.toml | 2 +- metrics-util/src/lib.rs | 1 + metrics/Cargo.toml | 56 +- metrics/README.md | 26 +- metrics/benches/macros.rs | 26 + {metrics-facade => metrics}/build.rs | 0 metrics/examples/basic.rs | 62 ++ metrics/src/data/counter.rs | 19 - metrics/src/lib.rs | 514 ++++++++------ metrics/src/macros.rs | 211 ++++++ metrics/src/sink.rs | 237 ------- 56 files changed, 2201 insertions(+), 1309 deletions(-) delete mode 100644 metrics-facade/Cargo.toml delete mode 100644 metrics-facade/README.md delete mode 100644 metrics-facade/examples/basic.rs delete mode 100644 metrics-facade/src/lib.rs delete mode 100644 metrics-facade/src/macros.rs rename {metrics-facade => metrics-runtime}/.gitignore (100%) rename {metrics => metrics-runtime}/CHANGELOG.md (100%) rename {metrics => metrics-runtime}/CODE_OF_CONDUCT.md (100%) create mode 100644 metrics-runtime/Cargo.toml rename {metrics => metrics-runtime}/LICENSE (100%) create mode 100644 metrics-runtime/README.md rename {metrics => metrics-runtime}/benches/histogram.rs (98%) rename {metrics => metrics-runtime}/examples/benchmark.rs (89%) rename {metrics => metrics-runtime}/examples/facade.rs (88%) rename {metrics => metrics-runtime}/examples/stdbenchmark.rs (100%) rename {metrics => metrics-runtime}/src/builder.rs (100%) rename {metrics => metrics-runtime}/src/common.rs (80%) rename {metrics => metrics-runtime}/src/config.rs (68%) rename {metrics => metrics-runtime}/src/control.rs (100%) create mode 100644 metrics-runtime/src/data/counter.rs rename {metrics => metrics-runtime}/src/data/gauge.rs (63%) rename {metrics => metrics-runtime}/src/data/histogram.rs (98%) rename {metrics => metrics-runtime}/src/data/mod.rs (100%) rename {metrics => metrics-runtime}/src/data/snapshot.rs (58%) rename {metrics => metrics-runtime}/src/exporters.rs (100%) rename {metrics => metrics-runtime}/src/helper.rs (100%) create mode 100644 metrics-runtime/src/lib.rs rename {metrics => metrics-runtime}/src/receiver.rs (94%) rename {metrics => metrics-runtime}/src/recorders.rs (100%) rename {metrics => metrics-runtime}/src/registry/metric.rs (64%) rename {metrics => metrics-runtime}/src/registry/mod.rs (100%) rename {metrics => metrics-runtime}/src/registry/scope.rs (72%) rename {metrics => metrics-runtime}/src/scopes.rs (97%) create mode 100644 metrics-runtime/src/sink.rs create mode 100644 metrics/benches/macros.rs rename {metrics-facade => metrics}/build.rs (100%) create mode 100644 metrics/examples/basic.rs delete mode 100644 metrics/src/data/counter.rs create mode 100644 metrics/src/macros.rs delete mode 100644 metrics/src/sink.rs diff --git a/COPYRIGHT b/COPYRIGHT index a938388..67846dd 100644 --- a/COPYRIGHT +++ b/COPYRIGHT @@ -45,8 +45,8 @@ their own copyright notices and license terms: CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -* metrics-facade is a fork of rust-lang-nursery/log which carries the - following license: +* metrics is a fork of rust-lang-nursery/log which carries the following + license: Copyright (c) 2014 The Rust Project Developers diff --git a/Cargo.toml b/Cargo.toml index a754cad..ade6a34 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [workspace] members = [ "metrics-core", - "metrics-facade", "metrics", + "metrics-runtime", "metrics-util", "metrics-exporter-log", "metrics-exporter-http", diff --git a/metrics-core/src/lib.rs b/metrics-core/src/lib.rs index 9ff3301..f0b2463 100644 --- a/metrics-core/src/lib.rs +++ b/metrics-core/src/lib.rs @@ -32,24 +32,214 @@ //! //! Histograms are a convenient way to measure behavior not only at the median, but at the edges of //! normal operating behavior. +#![deny(missing_docs)] use futures::future::Future; use std::borrow::Cow; +use std::fmt; +use std::slice::Iter; use std::time::Duration; -/// An optimized metric key. +/// An allocation-optimized string. /// -/// As some metrics might be sent at high frequency, it makes no sense to constantly allocate and -/// reallocate owned [`String`]s when a static [`str`] would suffice. As we don't want to limit -/// callers, though, we opt to use a copy-on-write pointer -- [`Cow`] -- to allow callers -/// flexiblity in how and what they pass. -pub type Key = Cow<'static, str>; +/// We specify `ScopedString` to attempt to get the best of both worlds: flexibility to provide a +/// static or dynamic (owned) string, while retaining the performance benefits of being able to +/// take ownership of owned strings and borrows of completely static strings. +pub type ScopedString = Cow<'static, str>; -/// A value which can be converted into a nanosecond representation. +/// A key/value pair used to further describe a metric. +#[derive(PartialEq, Eq, Hash, Clone, Debug)] +pub struct Label(ScopedString, ScopedString); + +impl Label { + /// Creates a `Label` from a key and value. + pub fn new(key: K, value: V) -> Self + where + K: Into, + V: Into, + { + Label(key.into(), value.into()) + } + + /// The key of this label. + pub fn key(&self) -> &str { + self.0.as_ref() + } + + /// The value of this label. + pub fn value(&self) -> &str { + self.1.as_ref() + } + + /// Consumes this `Label`, returning the key and value. + pub fn into_parts(self) -> (ScopedString, ScopedString) { + (self.0, self.1) + } +} + +/// A metric key. +/// +/// A key always includes a name, but can optional include multiple labels used to further describe +/// the metric. +#[derive(PartialEq, Eq, Hash, Clone, Debug)] +pub struct Key { + name: ScopedString, + labels: Vec