-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
publish req logs in an Apache-like format
Signed-off-by: Dan Bond <[email protected]>
- Loading branch information
Showing
5 changed files
with
66 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
[package] | ||
name = "metrics_server" | ||
version = "0.13.0" | ||
version = "0.14.0" | ||
authors = ["Dan Bond <[email protected]>"] | ||
edition = "2021" | ||
rust-version = "1.58" | ||
rust-version = "1.63" | ||
description = "A hassle-free, single-responsibility, safe HTTP/S server used to easily expose metrics in an application." | ||
documentation = "https://docs.rs/metrics_server" | ||
readme = "README.md" | ||
|
@@ -18,14 +18,16 @@ include = ["src/**/*", "tests", "examples", "Cargo.toml", "LICENSE", "README.md" | |
doctest = false | ||
|
||
[dependencies] | ||
http = "1.1" | ||
log = "0.4" | ||
tiny_http = "0.12" | ||
http = "0.2" | ||
time = { version = "0.3", features = ["formatting"] } | ||
|
||
[dev-dependencies] | ||
ctrlc = { version = "3.4", features = ["termination"] } | ||
prometheus-client = "0.21" | ||
reqwest = { version = "0.11", features = ["blocking"] } | ||
env_logger = "0.11" | ||
prometheus-client = "0.22" | ||
reqwest = { version = "0.12", features = ["blocking"] } | ||
|
||
[features] | ||
default = [] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
use std::sync::atomic::{AtomicBool, Ordering}; | ||
use std::sync::Arc; | ||
use std::{thread, time}; | ||
use std::sync::mpsc; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use log::info; | ||
use prometheus_client::encoding::text::encode; | ||
use prometheus_client::metrics::counter::Counter; | ||
use prometheus_client::registry::Registry; | ||
|
||
use metrics_server::MetricsServer; | ||
|
||
fn main() { | ||
env_logger::init(); | ||
|
||
// Register stop handler. | ||
let stop = Arc::new(AtomicBool::new(false)); | ||
let s = stop.clone(); | ||
let (send, recv) = mpsc::channel(); | ||
ctrlc::set_handler(move || { | ||
println!("Stopping..."); | ||
s.store(true, Ordering::Relaxed); | ||
info!("Stopping metrics server"); | ||
send.send(()).unwrap(); | ||
}) | ||
.unwrap(); | ||
|
||
// Expose the Prometheus metrics. | ||
let server = MetricsServer::http("localhost:8001"); | ||
println!("Starting metrics server: http://localhost:8001/metrics"); | ||
info!("Starting metrics server: http://localhost:8001/metrics"); | ||
|
||
std::thread::scope(|s| { | ||
let handle = s.spawn(|| { | ||
thread::scope(|s| { | ||
let handle = s.spawn(move || { | ||
// Create a metrics registry and counter that represents a single monotonically | ||
// increasing counter. | ||
let mut registry = Registry::default(); | ||
let counter: Counter = Counter::default(); | ||
registry.register("some_count", "Number of random counts", counter.clone()); | ||
|
||
// Increment the counter every 5 seconds. | ||
// Increment the counter periodically. | ||
loop { | ||
if stop.load(Ordering::Relaxed) { | ||
break; | ||
} | ||
|
||
counter.inc(); | ||
|
||
// Encode the current Registry in Prometheus format. | ||
// Encode the current Registry in Prometheus format | ||
let mut encoded = String::new(); | ||
encode(&mut encoded, ®istry).unwrap(); | ||
|
||
// Update the Metrics Server with the current encoded data. | ||
server.update(encoded.into()); | ||
|
||
thread::sleep(time::Duration::from_secs(5)); | ||
// Sleep for 5 seconds or exit. | ||
if recv.recv_timeout(Duration::from_secs(5)).is_ok() { | ||
// Stop server. | ||
server.stop().unwrap(); | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
handle.join().unwrap(); | ||
}); | ||
|
||
// Stop server. | ||
server.stop().unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters