-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #905 from subspace/dsn-metrics
DSN. Add libp2p metrics.
- Loading branch information
Showing
10 changed files
with
456 additions
and
7 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
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
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
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,4 +1,5 @@ | ||
pub mod multihash; | ||
pub(crate) mod prometheus; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
|
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,53 @@ | ||
use actix_web::http::StatusCode; | ||
use actix_web::web::Data; | ||
use actix_web::{get, App, HttpResponse, HttpServer}; | ||
use parking_lot::Mutex; | ||
use prometheus_client::encoding::text::encode; | ||
use prometheus_client::registry::Registry; | ||
use std::error::Error; | ||
use std::net::SocketAddr; | ||
use std::sync::Arc; | ||
use std::thread; | ||
use tracing::{error, info}; | ||
|
||
type SharedRegistry = Arc<Mutex<Registry>>; | ||
|
||
#[get("/metrics")] | ||
async fn metrics(registry: Data<SharedRegistry>) -> Result<HttpResponse, Box<dyn Error>> { | ||
let mut encoded: Vec<u8> = Vec::new(); | ||
encode(&mut encoded, ®istry.lock())?; | ||
|
||
let resp = HttpResponse::build(StatusCode::OK).body(encoded); | ||
|
||
Ok(resp) | ||
} | ||
|
||
/// Start prometheus metrics server on the provided address. | ||
pub async fn start_prometheus_metrics_server( | ||
address: SocketAddr, | ||
registry: Registry, | ||
) -> std::io::Result<()> { | ||
let shared_registry = Arc::new(Mutex::new(registry)); | ||
let data = Data::new(shared_registry); | ||
|
||
info!("Starting metrics server on {} ...", address); | ||
|
||
let server = HttpServer::new(move || App::new().app_data(data.clone()).service(metrics)) | ||
.bind(address)? | ||
.run(); | ||
|
||
// Actix-web will reuse existing tokio runtime. | ||
let runtime = tokio::runtime::Runtime::new()?; | ||
|
||
// We need a dedicated thread because actix-web App is !Send and won't work with tokio. | ||
thread::spawn(move || { | ||
if let Err(err) = runtime.block_on(server) { | ||
error!( | ||
?err, | ||
"block_on returns an error for prometheus metrics server" | ||
) | ||
} | ||
}); | ||
|
||
Ok(()) | ||
} |
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