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