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

add https support in Prometheus gateway #392

Merged
merged 2 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion metrics-exporter-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ keywords = ["metrics", "telemetry", "prometheus"]
default = ["http-listener", "push-gateway"]
async-runtime = ["tokio", "hyper"]
http-listener = ["async-runtime", "hyper/server", "ipnet"]
push-gateway = ["async-runtime", "hyper/client", "tracing"]
push-gateway = ["async-runtime", "hyper/client", "hyper-tls", "tracing"]

[dependencies]
metrics = { version = "^0.21", path = "../metrics" }
Expand All @@ -35,6 +35,7 @@ hyper = { version = "0.14", default-features = false, features = ["tcp", "http1"
ipnet = { version = "2", optional = true }
tokio = { version = "1", features = ["rt", "net", "time"], optional = true }
tracing = { version = "0.1.26", optional = true }
hyper-tls = { version = "0.5.0", optional = true }

[dev-dependencies]
tracing = "0.1"
Expand Down
60 changes: 57 additions & 3 deletions metrics-exporter-prometheus/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use hyper::{
http::HeaderValue,
Method, Request, Uri,
};
use hyper::client::{HttpConnector};
use hyper::http::uri::Scheme;
use hyper_tls::HttpsConnector;

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

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

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

match future.await {
Ok(response) => {
if !response.status().is_success() {
let status = response.status();
Expand Down Expand Up @@ -566,6 +574,20 @@ 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())
}
}

Copy link
Member

Choose a reason for hiding this comment

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

We should always use the HttpsConnector-based version, regardless of the scheme. HttpsConnector::new mentions that it will handle HTTP vs HTTPS properly, so we can avoid all this extra code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tobz You're right. I've updated the code after double-checked the implementation in hyper-tls.

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

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

#[test]
pub fn test_basic_auth() {
Expand Down Expand Up @@ -1066,4 +1090,34 @@ mod push_gateway_tests {
assert_eq!(b"metrics:123!_@ABC", &result[..]);
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
}
}