Skip to content

Commit

Permalink
deps: update tiny_http
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bond <[email protected]>
  • Loading branch information
loshz committed Feb 23, 2023
1 parent da87648 commit 17a882c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 26 deletions.
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metrics_server"
version = "0.11.0"
version = "0.12.0"
authors = ["Dan Bond <[email protected]>"]
edition = "2021"
rust-version = "1.58"
Expand All @@ -19,11 +19,11 @@ doctest = false

[dependencies]
log = "0.4"
tiny_http = "0.11"
tiny_http = "0.12"

[dev-dependencies]
ctrlc = { version = "3.2", features = ["termination"] }
prometheus-client = "0.18"
prometheus-client = "0.19"
reqwest = { version = "0.11", features = ["blocking"] }

[features]
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ This crate provides a thread safe, minimalstic HTTP/S server used to buffer metr
Include the lib in your `Cargo.toml` dependencies:
```toml
[dependencies]
metrics_server = "0.11"
metrics_server = "0.12"
```

To enable TLS support, pass the optional feature flag:
```toml
[dependencies]
metrics_server = { version = "0.11", features = ["tls"] }
metrics_server = { version = "0.12", features = ["tls"] }
```

### HTTP
Expand All @@ -33,8 +33,8 @@ use metrics_server::MetricsServer;
let server = MetricsServer::http("localhost:8001");

// Publish your application metrics.
let bytes = server.update(Vec::from([1, 2, 3, 4]));
assert_eq!(4, bytes);
let bytes = server.update("my_awesome_metric = 10".into());
assert_eq!(22, bytes);

// Stop the server.
server.stop().unwrap();
Expand All @@ -53,8 +53,8 @@ let key = include_bytes!("/path/to/key.pem").to_vec();
let server = MetricsServer::https("localhost:8443", cert, key);

// Publish your application metrics.
let bytes = server.update(Vec::from([1, 2, 3, 4]));
assert_eq!(4, bytes);
let bytes = server.update("my_awesome_metric = 10".into());
assert_eq!(22, bytes);

// Stop the server.
server.stop().unwrap();
Expand All @@ -69,8 +69,8 @@ let mut server = MetricsServer::new("localhost:8001", None, None);
server.serve_url("/path/to/metrics");

// Publish your application metrics.
let bytes = server.update(Vec::from([1, 2, 3, 4]));
assert_eq!(4, bytes);
let bytes = server.update("my_awesome_metric = 10".into());
assert_eq!(22, bytes);

// Stop the server.
server.stop().unwrap();
Expand Down
5 changes: 3 additions & 2 deletions examples/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fn main() {
let stop = Arc::new(AtomicBool::new(false));
let s = stop.clone();
ctrlc::set_handler(move || {
println!("Stopping...");
s.store(true, Ordering::Relaxed);
})
.unwrap();
Expand All @@ -38,11 +39,11 @@ fn main() {
counter.inc();

// Encode the current Registry in Prometheus format.
let mut encoded = Vec::new();
let mut encoded = String::new();
encode(&mut encoded, &registry).unwrap();

// Update the Metrics Server with the current encoded data.
server.update(encoded);
server.update(encoded.into());

thread::sleep(time::Duration::from_secs(5));
}
Expand Down
29 changes: 23 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//!
//! # Examples
//!
//! Start a HTTP server:
//! ## Start a HTTP server:
//!
//! ```rust
//! use metrics_server::MetricsServer;
Expand All @@ -18,14 +18,14 @@
//! let server = MetricsServer::http("localhost:8001");
//!
//! // Publish your application metrics.
//! let bytes = server.update(Vec::from([1, 2, 3, 4]));
//! assert_eq!(4, bytes);
//! let bytes = server.update("my_awesome_metric = 10".into());
//! assert_eq!(22, bytes);
//!
//! // Stop the server.
//! server.stop().unwrap();
//! ```
//!
//! Start a HTTPS server:
//! ## Start a HTTPS server:
//!
//! ```rust
//! use metrics_server::MetricsServer;
Expand All @@ -38,8 +38,25 @@
//! let server = MetricsServer::https("localhost:8443", cert, key);
//!
//! // Publish your application metrics.
//! let bytes = server.update(Vec::from([1, 2, 3, 4]));
//! assert_eq!(4, bytes);
//! let bytes = server.update("my_awesome_metric = 10".into());
//! assert_eq!(22, bytes);
//!
//! // Stop the server.
//! server.stop().unwrap();
//! ```
//!
//! ## Serve a custom URL
//!
//! ```rust
//! use metrics_server::MetricsServer;
//!
//! // Create a new server and specify the URL path to serve.
//! let mut server = MetricsServer::new("localhost:8001", None, None);
//! server.serve_url("/path/to/metrics");
//!
//! // Publish your application metrics.
//! let bytes = server.update("my_awesome_metric = 10".into());
//! assert_eq!(22, bytes);
//!
//! // Stop the server.
//! server.stop().unwrap();
Expand Down
23 changes: 16 additions & 7 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
use std::thread;

use log::{debug, error};
use tiny_http::{Method, Response, Server};
use tiny_http::{ConfigListenAddr, Method, Response, Server};

use crate::error::ServerError;

Expand Down Expand Up @@ -33,18 +33,25 @@ impl MetricsServer {
where
A: ToSocketAddrs,
{
// Construct listener from address.
let listener = ConfigListenAddr::from_socket_addrs(addr)
.map_err(|e| ServerError::Create(e.to_string()))?;

// Parse TLS config.
let config = match (certificate, private_key) {
#[cfg(feature = "tls")]
(Some(certificate), Some(private_key)) => tiny_http::ServerConfig {
addr,
addr: listener,
ssl: Some(tiny_http::SslConfig {
certificate,
private_key,
}),
},
// Default to no TLS.
_ => tiny_http::ServerConfig { addr, ssl: None },
_ => tiny_http::ServerConfig {
addr: listener,
ssl: None,
},
};

// Attempt to create a new server.
Expand Down Expand Up @@ -145,9 +152,9 @@ impl MetricsServer {
}

debug!(
"metrics_server: request received [url: {}, remote addr: {}, http version: {}]",
"metrics_server: request received [url: '{}', remote_addr: '{}', http_version: '{}']",
req.url(),
req.remote_addr(),
req.remote_addr().map_or("N/A".to_string(), |v| v.to_string()),
req.http_version(),
);

Expand Down Expand Up @@ -224,8 +231,10 @@ mod tests {

// No slash prefix.
assert_eq!(parse_url("metrics".to_string()), expected);
// Leading/trailing whitespace.
assert_eq!(parse_url(" metrics ".to_string()), expected);
// Leading slash prefix.
assert_eq!(parse_url("/metrics".to_string()), expected);
// Whitespace.
assert_eq!(parse_url(" metr ics ".to_string()), expected);
// Uppercase.
assert_eq!(parse_url("METRICS".to_string()), expected);
}
Expand Down

0 comments on commit 17a882c

Please sign in to comment.