Move telemetry::metrics into dedicated crate (#84)

The `metrics!` macro is currently local to the telemetry module.
Furthermore, the `telemetry::metrics` module no longer has
proxy-specific logic.

This change moves the `telemetry::metrics` module into a new crate,
`linkerd2_metrics`.

This will enable unifying `telemetry::http` and `telemetry::transport`
into `http` and `transport`, respectively.
This commit is contained in:
Oliver Gould 2018-08-24 14:53:53 -07:00 committed by GitHub
parent d38f686c5e
commit 8a9a9bf26b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
15 changed files with 84 additions and 42 deletions

View File

@ -469,12 +469,23 @@ name = "linked-hash-map"
version = "0.4.2" version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "linkerd2-metrics"
version = "0.1.0"
dependencies = [
"deflate 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
"http 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
"hyper 0.12.8 (registry+https://github.com/rust-lang/crates.io-index)",
"indexmap 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]] [[package]]
name = "linkerd2-proxy" name = "linkerd2-proxy"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"bytes 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "bytes 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)",
"deflate 0.7.18 (registry+https://github.com/rust-lang/crates.io-index)",
"env_logger 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)", "env_logger 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
"flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "flate2 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.23 (registry+https://github.com/rust-lang/crates.io-index)",
@ -488,6 +499,7 @@ dependencies = [
"inotify 0.5.2-dev (git+https://github.com/inotify-rs/inotify)", "inotify 0.5.2-dev (git+https://github.com/inotify-rs/inotify)",
"ipnet 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", "ipnet 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.40 (registry+https://github.com/rust-lang/crates.io-index)",
"linkerd2-metrics 0.1.0",
"linkerd2-proxy-api 0.1.1 (git+https://github.com/linkerd/linkerd2-proxy-api?tag=v0.1.1)", "linkerd2-proxy-api 0.1.1 (git+https://github.com/linkerd/linkerd2-proxy-api?tag=v0.1.1)",
"linkerd2-proxy-router 0.1.0", "linkerd2-proxy-router 0.1.0",
"log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -2,6 +2,7 @@
members = [ members = [
".", ".",
"futures-mpsc-lossy", "futures-mpsc-lossy",
"metrics",
"router", "router",
] ]
@ -18,12 +19,12 @@ flaky_tests = []
[dependencies] [dependencies]
futures-mpsc-lossy = { path = "./futures-mpsc-lossy" } futures-mpsc-lossy = { path = "./futures-mpsc-lossy" }
linkerd2-metrics = { path = "./metrics" }
linkerd2-proxy-router = { path = "./router" } linkerd2-proxy-router = { path = "./router" }
linkerd2-proxy-api = { git = "https://github.com/linkerd/linkerd2-proxy-api", tag = "v0.1.1", version = "0.1.1" } linkerd2-proxy-api = { git = "https://github.com/linkerd/linkerd2-proxy-api", tag = "v0.1.1", version = "0.1.1" }
bytes = "0.4" bytes = "0.4"
deflate = {version = "0.7.18", features = ["gzip"] }
env_logger = { version = "0.5", default-features = false } env_logger = { version = "0.5", default-features = false }
futures = "0.1" futures = "0.1"
futures-watch = { git = "https://github.com/carllerche/better-future" } futures-watch = { git = "https://github.com/carllerche/better-future" }
@ -77,6 +78,7 @@ procinfo = "0.4.2"
[dev-dependencies] [dev-dependencies]
net2 = "0.2" net2 = "0.2"
quickcheck = { version = "0.6", default-features = false } quickcheck = { version = "0.6", default-features = false }
linkerd2-metrics = { path = "./metrics", features = ["test_util"] }
linkerd2-proxy-api = { git = "https://github.com/linkerd/linkerd2-proxy-api", tag = "v0.1.1", version = "0.1.1", features = ["arbitrary"] } linkerd2-proxy-api = { git = "https://github.com/linkerd/linkerd2-proxy-api", tag = "v0.1.1", version = "0.1.1", features = ["arbitrary"] }
flate2 = { version = "1.0.1", default-features = false, features = ["rust_backend"] } flate2 = { version = "1.0.1", default-features = false, features = ["rust_backend"] }
# `tokio-io` is needed for TCP tests, because `tokio::io` doesn't re-export # `tokio-io` is needed for TCP tests, because `tokio::io` doesn't re-export

View File

@ -19,17 +19,19 @@ WORKDIR /usr/src/linkerd2-proxy
# #
# Mock out all local code and fetch external dependencies to ensure that # Mock out all local code and fetch external dependencies to ensure that
# external sources are primarily cached on Cargo.lock. # external sources are primarily cached on Cargo.lock.
RUN for d in . futures-mpsc-lossy router ; \ RUN for d in . futures-mpsc-lossy router metrics ; \
do mkdir -p "${d}/src" && touch "${d}/src/lib.rs" ; \ do mkdir -p "${d}/src" && touch "${d}/src/lib.rs" ; \
done done
COPY Cargo.toml Cargo.lock ./ COPY Cargo.toml Cargo.lock ./
COPY futures-mpsc-lossy/Cargo.toml futures-mpsc-lossy/Cargo.toml COPY futures-mpsc-lossy/Cargo.toml futures-mpsc-lossy/Cargo.toml
COPY router/Cargo.toml router/Cargo.toml COPY router/Cargo.toml router/Cargo.toml
COPY metrics/Cargo.toml metrics/Cargo.toml
RUN cargo fetch --locked RUN cargo fetch --locked
# Build libraries, leaving the proxy mocked out. # Build libraries, leaving the proxy mocked out.
COPY futures-mpsc-lossy futures-mpsc-lossy COPY futures-mpsc-lossy futures-mpsc-lossy
COPY router router COPY router router
COPY metrics metrics
ARG PROXY_UNOPTIMIZED ARG PROXY_UNOPTIMIZED
RUN if [ -n "$PROXY_UNOPTIMIZED" ]; \ RUN if [ -n "$PROXY_UNOPTIMIZED" ]; \
then cargo build --frozen ; \ then cargo build --frozen ; \

17
metrics/Cargo.toml Normal file
View File

@ -0,0 +1,17 @@
[package]
name = "linkerd2-metrics"
version = "0.1.0"
authors = ["Oliver Gould <ver@buoyant.io>"]
publish = false
[features]
default = []
test_util = []
[dependencies]
deflate = { version = "0.7.18", features = ["gzip"] }
futures = "0.1"
http = "0.1"
hyper = "0.12.3"
indexmap = "1.0"
log = "0.4"

View File

@ -85,11 +85,11 @@ impl<V: Into<u64>> Histogram<V> {
self.buckets[idx].incr(); self.buckets[idx].incr();
self.sum += value; self.sum += value;
} }
}
// ===== Test-only methods to help with assertions about histograms. ===== #[cfg(any(test, feature = "test_util"))]
impl<V: Into<u64>> Histogram<V> {
/// Assert the bucket containing `le` has a count of at least `at_least`. /// Assert the bucket containing `le` has a count of at least `at_least`.
#[cfg(test)]
pub fn assert_bucket_at_least(&self, le: u64, at_least: u64) { pub fn assert_bucket_at_least(&self, le: u64, at_least: u64) {
for (&bucket, &count) in self { for (&bucket, &count) in self {
if bucket >= le { if bucket >= le {
@ -104,7 +104,6 @@ impl<V: Into<u64>> Histogram<V> {
} }
/// Assert the bucket containing `le` has a count of exactly `exactly`. /// Assert the bucket containing `le` has a count of exactly `exactly`.
#[cfg(test)]
pub fn assert_bucket_exactly(&self, le: u64, exactly: u64) -> &Self { pub fn assert_bucket_exactly(&self, le: u64, exactly: u64) -> &Self {
for (&bucket, &count) in self { for (&bucket, &count) in self {
if bucket >= le { if bucket >= le {
@ -122,7 +121,6 @@ impl<V: Into<u64>> Histogram<V> {
/// Assert all buckets less than the one containing `value` have /// Assert all buckets less than the one containing `value` have
/// counts of exactly `exactly`. /// counts of exactly `exactly`.
#[cfg(test)]
pub fn assert_lt_exactly(&self, value: u64, exactly: u64) -> &Self { pub fn assert_lt_exactly(&self, value: u64, exactly: u64) -> &Self {
for (i, &bucket) in self.bounds.0.iter().enumerate() { for (i, &bucket) in self.bounds.0.iter().enumerate() {
let ceiling = match bucket { let ceiling = match bucket {
@ -148,7 +146,6 @@ impl<V: Into<u64>> Histogram<V> {
/// Assert all buckets greater than the one containing `value` have /// Assert all buckets greater than the one containing `value` have
/// counts of exactly `exactly`. /// counts of exactly `exactly`.
#[cfg(test)]
pub fn assert_gt_exactly(&self, value: u64, exactly: u64) -> &Self { pub fn assert_gt_exactly(&self, value: u64, exactly: u64) -> &Self {
// We set this to true after we've iterated past the first bucket // We set this to true after we've iterated past the first bucket
// whose upper bound is >= `value`. // whose upper bound is >= `value`.

39
metrics/src/lib.rs Normal file
View File

@ -0,0 +1,39 @@
//! Utilties for exposing metrics to Prometheus.
extern crate deflate;
extern crate indexmap;
extern crate futures;
extern crate http;
extern crate hyper;
#[macro_use]
extern crate log;
mod counter;
mod gauge;
mod histogram;
pub mod latency;
mod prom;
mod scopes;
mod serve;
pub use self::counter::Counter;
pub use self::gauge::Gauge;
pub use self::histogram::Histogram;
pub use self::prom::{FmtMetrics, FmtLabels, FmtMetric, Metric};
pub use self::scopes::Scopes;
pub use self::serve::Serve;
#[macro_export]
macro_rules! metrics {
{ $( $name:ident : $kind:ty { $help:expr } ),+ } => {
$(
#[allow(non_upper_case_globals)]
const $name: ::linkerd2_metrics::Metric<'static, $kind> =
::linkerd2_metrics::Metric {
name: stringify!($name),
help: $help,
_p: ::std::marker::PhantomData,
};
)+
}
}

View File

@ -3,9 +3,7 @@
#![deny(warnings)] #![deny(warnings)]
extern crate bytes; extern crate bytes;
extern crate linkerd2_proxy_api;
extern crate env_logger; extern crate env_logger;
extern crate deflate;
#[macro_use] #[macro_use]
extern crate futures; extern crate futures;
extern crate futures_mpsc_lossy; extern crate futures_mpsc_lossy;
@ -47,12 +45,16 @@ extern crate tower_h2;
extern crate tower_h2_balance; extern crate tower_h2_balance;
extern crate tower_reconnect; extern crate tower_reconnect;
extern crate tower_service; extern crate tower_service;
extern crate linkerd2_proxy_router;
extern crate tower_util; extern crate tower_util;
extern crate tower_in_flight_limit; extern crate tower_in_flight_limit;
extern crate trust_dns_resolver; extern crate trust_dns_resolver;
extern crate try_lock; extern crate try_lock;
#[macro_use]
extern crate linkerd2_metrics;
extern crate linkerd2_proxy_api;
extern crate linkerd2_proxy_router;
use futures::*; use futures::*;
use std::error::Error; use std::error::Error;

View File

@ -1,16 +0,0 @@
//! Utilties for exposing metrics to Prometheus.
mod counter;
mod gauge;
mod histogram;
pub mod latency;
mod prom;
mod scopes;
mod serve;
pub use self::counter::Counter;
pub use self::gauge::Gauge;
pub use self::histogram::Histogram;
pub use self::prom::{FmtMetrics, FmtLabels, FmtMetric, Metric};
pub use self::scopes::Scopes;
pub use self::serve::Serve;

View File

@ -1,23 +1,10 @@
use std::sync::{Arc, Mutex}; use std::sync::{Arc, Mutex};
use std::time::{Duration, SystemTime}; use std::time::{Duration, SystemTime};
macro_rules! metrics { use linkerd2_metrics as metrics;
{ $( $name:ident : $kind:ty { $help:expr } ),+ } => {
$(
#[allow(non_upper_case_globals)]
const $name: ::telemetry::metrics::Metric<'static, $kind> =
::telemetry::metrics::Metric {
name: stringify!($name),
help: $help,
_p: ::std::marker::PhantomData,
};
)+
}
}
mod errno; mod errno;
pub mod http; pub mod http;
mod metrics;
mod process; mod process;
mod report; mod report;
pub mod tap; pub mod tap;