Skip to content

Commit

Permalink
Fixes: #391
Browse files Browse the repository at this point in the history
  • Loading branch information
qingwen220 committed Sep 18, 2023
1 parent 2a2db23 commit 6861c66
Showing 1 changed file with 4 additions and 55 deletions.
59 changes: 4 additions & 55 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ use hyper::{
http::HeaderValue,
Method, Request, Uri,
};
use hyper::client::{HttpConnector};
use hyper::http::uri::Scheme;
use hyper_tls::HttpsConnector;

use indexmap::IndexMap;
Expand Down Expand Up @@ -463,7 +461,8 @@ impl PrometheusBuilder {
#[cfg(feature = "push-gateway")]
ExporterConfig::PushGateway { endpoint, interval, username, password } => {
let exporter = async move {
let http_client = create_gateway_client(&endpoint);
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
let auth = username.as_ref().map(|name| basic_auth(name, password.as_deref()));

loop {
Expand All @@ -488,12 +487,7 @@ impl PrometheusBuilder {
}
};

let future = match &http_client {
HttpCli::HttpsClient(client) => client.request(req),
HttpCli::HttpClient(client) => client.request(req)
};

match future.await {
match client.request(req).await {
Ok(response) => {
if !response.status().is_success() {
let status = response.status();
Expand Down Expand Up @@ -574,20 +568,6 @@ fn basic_auth(username: &str, password: Option<&str>) -> HeaderValue {
header
}

enum HttpCli {
HttpClient(Client<HttpConnector>),
HttpsClient(Client<HttpsConnector<HttpConnector>>),
}

fn create_gateway_client(endpoint: &Uri) -> HttpCli {
if endpoint.scheme() == Some(&Scheme::HTTPS) {
let https = HttpsConnector::new();
HttpCli::HttpsClient(Client::builder().build::<_, hyper::Body>(https))
} else {
HttpCli::HttpClient(Client::new())
}
}

#[cfg(test)]
mod tests {
use std::time::Duration;
Expand Down Expand Up @@ -1056,9 +1036,7 @@ mod tests {

#[cfg(all(test, feature = "push-gateway"))]
mod push_gateway_tests {
use std::convert::TryFrom;
use hyper::{Body, Request, Uri};
use crate::builder::{basic_auth, create_gateway_client, HttpCli};
use crate::builder::{basic_auth};

#[test]
pub fn test_basic_auth() {
Expand Down Expand Up @@ -1091,33 +1069,4 @@ mod push_gateway_tests {
assert!(header.is_sensitive());
}

#[test]
pub fn test_client_create() {
let uri = Uri::try_from( "http://localhost").unwrap();
assert!(!create_https_client(uri));

let uri = Uri::try_from( "https://localhost").unwrap();
assert!(create_https_client(uri));
}

fn create_https_client(uri: Uri) -> bool {
let client = create_gateway_client(&uri);
let req = Request::builder()
.uri(uri)
.body(Body::empty())
.unwrap();

let is_https: bool;
let _future = match &client {
HttpCli::HttpsClient(client) => {
is_https = true;
client.request(req)
}
HttpCli::HttpClient(client) => {
is_https = false;
client.request(req)
}
};
is_https
}
}

0 comments on commit 6861c66

Please sign in to comment.