forked from MystenLabs/sui
-
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.
metrics: implement metrics server with axum
- Loading branch information
Showing
5 changed files
with
63 additions
and
14 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
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,36 @@ | ||
// Copyright (c) 2022, Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use axum::{extract::Extension, http::StatusCode, routing::get, Router}; | ||
use prometheus::{Registry, TextEncoder}; | ||
use std::net::SocketAddr; | ||
|
||
const METRICS_ROUTE: &str = "/metrics"; | ||
|
||
pub fn start_prometheus_server(addr: SocketAddr) -> Registry { | ||
let registry = Registry::new(); | ||
|
||
let app = Router::new() | ||
.route(METRICS_ROUTE, get(metrics)) | ||
.layer(Extension(registry.clone())); | ||
|
||
tokio::spawn(async move { | ||
axum::Server::bind(&addr) | ||
.serve(app.into_make_service()) | ||
.await | ||
.unwrap(); | ||
}); | ||
|
||
registry | ||
} | ||
|
||
async fn metrics(Extension(registry): Extension<Registry>) -> (StatusCode, String) { | ||
let metrics_families = registry.gather(); | ||
match TextEncoder.encode_to_string(&metrics_families) { | ||
Ok(metrics) => (StatusCode::OK, metrics), | ||
Err(error) => ( | ||
StatusCode::INTERNAL_SERVER_ERROR, | ||
format!("unable to encode metrics: {error}"), | ||
), | ||
} | ||
} |