Skip to content

Commit

Permalink
add http method
Browse files Browse the repository at this point in the history
Signed-off-by: Dan Bond <danbond@protonmail.com>
  • Loading branch information
loshz committed Apr 19, 2022
1 parent 8bd1464 commit 135060b
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metrics_server"
version = "0.5.0"
version = "0.6.0"
authors = ["Dan Bond <danbond@protonmail.com>"]
edition = "2021"
rust-version = "1.58"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ 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.5"
metrics_server = "0.6"
```

### HTTP
```rust
use metrics_server::MetricsServer;

// Create a new HTTP server and start listening for requests in the background.
let server = MetricsServer::new("localhost:8001");
let server = MetricsServer::http("localhost:8001");

// Publish you application metrics.
let bytes = server.update(Vec::from([1, 2, 3, 4]));
Expand Down
2 changes: 1 addition & 1 deletion examples/prometheus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
registry.register("some_count", "Number of random counts", counter.clone());

// Expose the Prometheus metrics.
let server = MetricsServer::new("localhost:8001");
let server = MetricsServer::http("localhost:8001");

// Increment the counter every 5 seconds.
loop {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//! use metrics_server::MetricsServer;
//!
//! // Create a new HTTP server and start listening for requests in the background.
//! let server = MetricsServer::new("localhost:8001");
//! let server = MetricsServer::http("localhost:8001");
//!
//! // Publish your application metrics.
//! let bytes = server.update(Vec::from([1, 2, 3, 4]));
Expand Down
23 changes: 20 additions & 3 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,31 @@ struct MetricsServerShared {
}

impl MetricsServer {
/// Creates an empty `MetricsServer` and starts a HTTP server on a new thread at the given address.
/// Creates an empty `MetricsServer` and starts a HTTP/S server on a new thread at the given address.
///
/// This server will only respond synchronously as it blocks until receiving new requests.
///
/// # Panics
///
/// Panics if given an invalid address.
pub fn new<A>(addr: A) -> Self
pub fn new<A>(addr: A, certificate: Option<Vec<u8>>, private_key: Option<Vec<u8>>) -> Self
where
A: ToSocketAddrs,
{
match (certificate, private_key) {
(Some(cert), Some(key)) => MetricsServer::https(addr, cert, key),
_ => MetricsServer::http(addr),
}
}

/// Shortcut for creating an empty `MetricsServer` and starting a HTTP server on a new thread at the given address.
///
/// This server will only respond synchronously as it blocks until receiving new requests.
///
/// # Panics
///
/// Panics if given an invalid address.
pub fn http<A>(addr: A) -> Self
where
A: ToSocketAddrs,
{
Expand All @@ -35,7 +52,7 @@ impl MetricsServer {
MetricsServer::serve(config)
}

/// Creates an empty `MetricsServer` and starts a HTTPS server on a new thread at the given address.
/// Shortcut for creating an empty `MetricsServer` and starting a HTTPS server on a new thread at the given address.
///
/// This server will only respond synchronously as it blocks until receiving new requests.
///
Expand Down
24 changes: 22 additions & 2 deletions tests/server.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,34 @@
use metrics_server::MetricsServer;

#[test]
#[should_panic]
fn test_new_server_invalid_address() {
let _ = MetricsServer::new("invalid:99999999", None, None);
}

#[test]
fn test_new_http_server_serve() {
let _ = MetricsServer::new("localhost:8001", None, None);
}

#[test]
fn test_new_https_server_serve() {
// Load TLS config.
let cert = include_bytes!("./certs/certificate.pem").to_vec();
let key = include_bytes!("./certs/private_key.pem").to_vec();

let _ = MetricsServer::new("localhost:8443", Some(cert), Some(key));
}

#[test]
#[should_panic]
fn test_http_server_invalid_address() {
let _ = MetricsServer::new("invalid:99999999");
let _ = MetricsServer::http("invalid:99999999");
}

#[test]
fn test_http_server_serve() {
let server = MetricsServer::new("localhost:8001");
let server = MetricsServer::http("localhost:8001");

// Assert calls to non /metrics endpoint returns 404.
let res = reqwest::blocking::get("http://localhost:8001/invalid").unwrap();
Expand Down

0 comments on commit 135060b

Please sign in to comment.