Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo: update prometheus to 0.7 #125

Merged
merged 8 commits into from
Jun 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,600 changes: 853 additions & 747 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion commons/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@
name = "commons"
version = "0.1.0"
authors = ["Stefan Junker <mail@stefanjunker.de>"]
edition = "2018"

[dependencies]
actix-web = "^0.7.8"
env_logger = "^0.6.0"
failure = "^0.1.5"
lazy_static = "^1.2.0"
log = "^0.4.6"
prometheus = "^0.6.0"
prometheus = "^0.7.0"
serde = "^1.0.70"
serde_json = "^1.0.34"
tokio = "^0.1"
url = "^1.7.2"
2 changes: 2 additions & 0 deletions commons/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub mod de;
mod errors;
pub use crate::errors::{register_metrics, GraphError};

pub mod testing;

use actix_web::http::header;
use std::collections::HashSet;
use url::form_urlencoded;
Expand Down
18 changes: 18 additions & 0 deletions commons/src/testing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//! Test helpers.

use failure::Fallible;
use tokio::runtime::current_thread::Runtime;

/// Initialize a tokio runtime for tests, with logging.
pub fn init_runtime() -> Fallible<Runtime> {
let _ = env_logger::try_init_from_env(env_logger::Env::default());
Runtime::new().map_err(failure::Error::from)
}

/// Register a dummy gauge, with given value.
pub fn dummy_gauge(registry: &prometheus::Registry, value: f64) -> Fallible<()> {
let test_gauge = prometheus::Gauge::new("dummy_gauge", "dummy help")?;
test_gauge.set(value);
registry.register(Box::new(test_gauge))?;
Ok(())
}
5 changes: 4 additions & 1 deletion graph-builder/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ futures = "0.1"
itertools = "^0.7.8"
lazy_static = "^1.2.0"
log = "^0.4.3"
prometheus = "^0.6.0"
prometheus = "^0.7.0"
quay = { path = "../quay" }
regex = "^1.1.0"
reqwest = "^0.9.0"
Expand All @@ -34,6 +34,9 @@ toml = "^0.4.10"
url = "^1.7.2"
parking_lot = "^0.8.0"

[dev-dependencies]
twoway = "^0.2"

[features]
test-net = []
test-net-private = []
44 changes: 44 additions & 0 deletions graph-builder/src/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,47 @@ pub fn serve_readiness(
};
Box::new(future::ok(resp))
}

#[cfg(test)]
mod tests {
use crate::graph::State;
use crate::status;
use actix_web::test::TestRequest;
use commons::testing;
use failure::Fallible;
use parking_lot::RwLock;
use std::collections::HashSet;
use std::sync::Arc;

fn mock_state() -> State {
let json_graph = Arc::new(RwLock::new(String::new()));
let live = Arc::new(RwLock::new(false));
let ready = Arc::new(RwLock::new(false));

State::new(
json_graph.clone(),
HashSet::new(),
live.clone(),
ready.clone(),
)
}

#[test]
fn serve_metrics_basic() -> Fallible<()> {
let mut rt = testing::init_runtime()?;
testing::dummy_gauge(&status::PROM_REGISTRY, 42.0)?;

let http_req = TestRequest::with_state(mock_state()).finish();
let metrics_call = status::serve_metrics(http_req);
let resp = rt.block_on(metrics_call)?;

assert_eq!(resp.status().as_u16(), 200);
assert!(resp.body().is_binary());

if let actix_web::Body::Binary(body) = resp.body() {
assert!(!body.is_empty());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you're using failure in here you can use the ensure!() macro here and elsewhere instead of assert!()

Copy link
Contributor Author

@lucab lucab Jun 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert and ensure are not directly interchangeable, as the latter doesn't have fileline information and additional introspection details provided by the former.

I'm sticking to assert for enforcing invariants, but feel free to have a spike ticket to investigate/build a blended ensure+assert_* macro.

assert!(twoway::find_bytes(body.as_ref(), b"cincinnati_gb_dummy_gauge 42\n").is_some());
}
Ok(())
}
steveej marked this conversation as resolved.
Show resolved Hide resolved
}
3 changes: 2 additions & 1 deletion policy-engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ hyper = "^0.12.6"
lazy_static = "^1.2.0"
log = "^0.4.3"
openapiv3 = "0.1"
prometheus = "^0.6.0"
prometheus = "^0.7.0"
semver = "^0.9.0"
serde = "^1.0.70"
serde_derive = "^1.0.70"
Expand All @@ -28,3 +28,4 @@ url = "1.7.2"

[dev-dependencies]
tokio = "^0.1"
twoway = "^0.2"
27 changes: 27 additions & 0 deletions policy-engine/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,30 @@ pub(crate) fn serve(
.map(|content| HttpResponse::Ok().body(content));
Box::new(resp)
}

#[cfg(test)]
mod tests {
use crate::metrics;
use actix_web::test::TestRequest;
use commons::testing;
use failure::Fallible;

#[test]
fn serve_metrics_basic() -> Fallible<()> {
let mut rt = testing::init_runtime()?;
testing::dummy_gauge(&metrics::PROM_REGISTRY, 42.0)?;

let http_req = TestRequest::with_state(()).finish();
let metrics_call = metrics::serve(http_req);
let resp = rt.block_on(metrics_call)?;

assert_eq!(resp.status().as_u16(), 200);
assert!(resp.body().is_binary());

if let actix_web::Body::Binary(body) = resp.body() {
assert!(!body.is_empty());
assert!(twoway::find_bytes(body.as_ref(), b"cincinnati_pe_dummy_gauge 42\n").is_some());
};
Ok(())
}
}