Skip to content

Commit

Permalink
Make tls optional and add rustls support (metrics-rs#418)
Browse files Browse the repository at this point in the history
BREAKING: tls isn't enabled by default
  • Loading branch information
nstinus committed Nov 29, 2023
1 parent e3d54c2 commit d38b94e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
6 changes: 5 additions & 1 deletion metrics-exporter-prometheus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ 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", "hyper-tls", "tracing"]
push-gateway = ["async-runtime", "hyper/client", "tracing"]
native-tls = ["hyper-tls"]
rustls-tls = ["hyper-rustls"]

[dependencies]
metrics = { version = "^0.21", path = "../metrics" }
Expand All @@ -36,6 +38,8 @@ 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 }
hyper-rustls = { version = "0.24.2", optional = true }
cfg-if = "1.0.0"

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

use indexmap::IndexMap;
#[cfg(feature = "http-listener")]
Expand Down Expand Up @@ -462,8 +461,16 @@ impl PrometheusBuilder {
#[cfg(feature = "push-gateway")]
ExporterConfig::PushGateway { endpoint, interval, username, password } => {
let exporter = async move {
let https = HttpsConnector::new();
let client = Client::builder().build::<_, hyper::Body>(https);
cfg_if::cfg_if! {
if #[cfg(feature = "native-tls")] {
let client = Client::builder().build::<_, hyper::Body>(hyper_tls::HttpsConnector::new());
} else if #[cfg(feature = "rustls-tls")] {
let client = Client::builder().build::<_, hyper::Body>(hyper_rustls::HttpsConnectorBuilder::new());
} else {
let client = Client::builder().build_http();
}
}

let auth = username.as_ref().map(|name| basic_auth(name, password.as_deref()));

loop {
Expand Down

0 comments on commit d38b94e

Please sign in to comment.