-
-
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.
- Loading branch information
Showing
9 changed files
with
192 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,3 +1,4 @@ | ||
pub mod metrics; | ||
pub mod notifier; | ||
pub mod server; | ||
pub mod state; |
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 |
---|---|---|
@@ -0,0 +1,81 @@ | ||
//! Prometheus (OpenMetrics) metrics server. | ||
//! | ||
//! It is listening on its own address | ||
//! to allow exposting it on a private network only | ||
//! independently of the main service. | ||
use std::sync::atomic::AtomicI64; | ||
use std::sync::Arc; | ||
|
||
use prometheus_client::encoding::text::encode; | ||
use prometheus_client::metrics::counter::Counter; | ||
use prometheus_client::metrics::gauge::Gauge; | ||
use prometheus_client::registry::Registry; | ||
|
||
use anyhow::Result; | ||
|
||
#[derive(Debug, Default)] | ||
pub struct Metrics { | ||
pub registry: Registry, | ||
|
||
/// Number of successfully sent visible notifications. | ||
pub direct_notifications_total: Counter, | ||
|
||
/// Number of tokens registered for heartbeat notifications. | ||
pub heartbeat_token_count: Gauge<i64, AtomicI64>, | ||
} | ||
|
||
impl Metrics { | ||
pub fn new() -> Self { | ||
let mut registry = Registry::default(); | ||
|
||
let direct_notifications_total = Counter::default(); | ||
registry.register( | ||
"direct_notifications", | ||
"Number of direct notifications", | ||
direct_notifications_total.clone(), | ||
); | ||
|
||
let heartbeat_token_count = Gauge::<i64, AtomicI64>::default(); | ||
registry.register( | ||
"heartbeat_token_count", | ||
"Number of tokens registered for heartbeat notifications", | ||
heartbeat_token_count.clone(), | ||
); | ||
|
||
Self { | ||
registry, | ||
direct_notifications_total, | ||
heartbeat_token_count, | ||
} | ||
} | ||
|
||
/// Counts direct notification. | ||
pub fn inc_direct_notification(&self) { | ||
self.direct_notifications_total.inc(); | ||
} | ||
|
||
/// Sets number of tokens registered for heartbeat notifications. | ||
pub fn set_heartbeat_token_count(&self, value: usize) { | ||
self.heartbeat_token_count.set(value as i64); | ||
} | ||
} | ||
|
||
type State = Arc<Metrics>; | ||
|
||
pub async fn start(state: State, server: String) -> Result<()> { | ||
let mut app = tide::with_state(state); | ||
app.at("/metrics").get(metrics); | ||
app.listen(server).await?; | ||
Ok(()) | ||
} | ||
|
||
async fn metrics(req: tide::Request<State>) -> tide::Result<tide::Response> { | ||
let mut encoded = String::new(); | ||
encode(&mut encoded, &req.state().registry).unwrap(); | ||
let response = tide::Response::builder(tide::StatusCode::Ok) | ||
.body(encoded) | ||
.content_type("application/openmetrics-text; version=1.0.0; charset=utf-8") | ||
.build(); | ||
Ok(response) | ||
} |
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
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