Skip to content

Commit

Permalink
Factor out shared logic
Browse files Browse the repository at this point in the history
  • Loading branch information
maoertel committed Jun 23, 2024
1 parent e348121 commit c9465af
Showing 1 changed file with 33 additions and 43 deletions.
76 changes: 33 additions & 43 deletions src/prometheus_client_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ where
mod test {
use std::collections::HashMap;

use mockito::Mock;
use mockito::Server;
use mockito::ServerGuard;
use prometheus_client::encoding::text::encode;
use prometheus_client::encoding::EncodeLabelSet;
use prometheus_client::encoding::EncodeLabelValue;
Expand All @@ -138,10 +140,7 @@ mod test {
path: String,
}

#[cfg(feature = "with_reqwest_blocking")]
#[test]
fn test_push_all_blocking_reqwest_prometheus_client_crate() {
use reqwest::blocking::Client;
fn create_metrics() -> String {
// Given I have a counter metric
let mut registry = <Registry>::default();
let http_requests = Family::<Labels, Counter>::default();
Expand All @@ -157,27 +156,47 @@ mod test {
let mut metrics = String::new();
encode(&mut metrics, &registry).unwrap();

let expected = "# HELP http_requests Number of HTTP requests received.\n".to_owned()
+ "# TYPE http_requests counter\n"
+ "http_requests_total{method=\"GET\",path=\"/metrics\"} 1\n"
+ "# EOF\n";
metrics
}

let mut server = Server::new();
let push_gateway_address = Url::parse(&server.url()).unwrap();
fn create_push_gateway_mock(
server: &mut ServerGuard,
) -> (Mock, Url, &'static str, HashMap<&'static str, &'static str>) {
let pushgateway_address = Url::parse(&server.url()).unwrap();
let job = "prometheus_client_crate_job";
let label_name = "kind";
let label_value = "test";
let path = format!("/metrics/job/{job}/{label_name}/{label_value}");

let grouping: HashMap<&str, &str> = HashMap::from([(label_name, label_value)]);

let expected = "# HELP http_requests Number of HTTP requests received.\n".to_owned()
+ "# TYPE http_requests counter\n"
+ "http_requests_total{method=\"GET\",path=\"/metrics\"} 1\n"
+ "# EOF\n";

let pushgateway_mock = server
.mock("PUT", &*path)
.with_status(200)
.match_header("content-type", "text/plain")
.match_body(mockito::Matcher::from(&*expected))
.create();

(pushgateway_mock, pushgateway_address, job, grouping)
}

#[cfg(feature = "with_reqwest_blocking")]
#[test]
fn test_push_all_blocking_reqwest_prometheus_client_crate() {
use reqwest::blocking::Client;
// Given I have a counter metric
let metrics = create_metrics();

// And a push gateway and a job
let mut server = Server::new();
let (pushgateway_mock, push_gateway_address, job, grouping) =
create_push_gateway_mock(&mut server);

// And a nonblocking prometheus metrics pusher
let metrics_pusher =
PrometheusClientMetricsPusherBlocking::create(Client::new(), &push_gateway_address)
Expand All @@ -196,42 +215,13 @@ mod test {
#[tokio::test]
async fn test_push_all_non_blocking_reqwest_prometheus_client_crate() {
use reqwest::Client;

// Given I have a counter metric
let mut registry = <Registry>::default();
let http_requests = Family::<Labels, Counter>::default();
registry.register(
"http_requests",
"Number of HTTP requests received",
http_requests.clone(),
);
http_requests
.get_or_create(&Labels { method: Method::GET, path: "/metrics".to_string() })
.inc();

let mut metrics = String::new();
encode(&mut metrics, &registry).unwrap();

let expected = "# HELP http_requests Number of HTTP requests received.\n".to_owned()
+ "# TYPE http_requests counter\n"
+ "http_requests_total{method=\"GET\",path=\"/metrics\"} 1\n"
+ "# EOF\n";
let metrics = create_metrics();

// And a push gateway and a job
let mut server = Server::new_async().await;
let push_gateway_address = Url::parse(&server.url()).unwrap();
let job = "prometheus_client_crate_job";
let label_name = "kind";
let label_value = "test";
let path = format!("/metrics/job/{job}/{label_name}/{label_value}");

let grouping: HashMap<&str, &str> = HashMap::from([(label_name, label_value)]);

let pushgateway_mock = server
.mock("PUT", &*path)
.with_status(200)
.match_header("content-type", "text/plain")
.match_body(mockito::Matcher::from(&*expected))
.create();
let (pushgateway_mock, push_gateway_address, job, grouping) =
create_push_gateway_mock(&mut server);

// And a nonblocking prometheus metrics pusher
let metrics_pusher =
Expand Down

0 comments on commit c9465af

Please sign in to comment.