-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jerry Wang <[email protected]>
- Loading branch information
Showing
2 changed files
with
89 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use axum::body::Body; | ||
use axum::extract::State; | ||
use axum::http::header::CONTENT_TYPE; | ||
use axum::http::StatusCode; | ||
use axum::response::{IntoResponse, Response}; | ||
use axum::routing::get; | ||
use axum::Router; | ||
use prometheus_client::encoding::text::encode; | ||
use prometheus_client::metrics::counter::Counter; | ||
use prometheus_client::metrics::family::Family; | ||
use prometheus_client::registry::Registry; | ||
use prometheus_client_derive_encode::{EncodeLabelSet, EncodeLabelValue}; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelValue)] | ||
pub enum Method { | ||
Get, | ||
Post, | ||
} | ||
|
||
#[derive(Clone, Debug, Hash, PartialEq, Eq, EncodeLabelSet)] | ||
pub struct MethodLabels { | ||
pub method: Method, | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct Metrics { | ||
requests: Family<MethodLabels, Counter>, | ||
} | ||
|
||
impl Metrics { | ||
pub fn inc_requests(&self, method: Method) { | ||
self.requests.get_or_create(&MethodLabels { method }).inc(); | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct AppState { | ||
pub registry: Registry, | ||
} | ||
|
||
pub async fn metrics_handler(State(state): State<Arc<Mutex<AppState>>>) -> impl IntoResponse { | ||
let state = state.lock().await; | ||
let mut buffer = String::new(); | ||
encode(&mut buffer, &state.registry).unwrap(); | ||
|
||
Response::builder() | ||
.status(StatusCode::OK) | ||
.header( | ||
CONTENT_TYPE, | ||
"application/openmetrics-text; version=1.0.0; charset=utf-8", | ||
) | ||
.body(Body::from(buffer)) | ||
.unwrap() | ||
} | ||
|
||
pub async fn some_handler(State(metrics): State<Arc<Mutex<Metrics>>>) -> impl IntoResponse { | ||
metrics.lock().await.inc_requests(Method::Get); | ||
"okay".to_string() | ||
} | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let metrics = Metrics { | ||
requests: Family::default(), | ||
}; | ||
let mut state = AppState { | ||
registry: Registry::default(), | ||
}; | ||
state | ||
.registry | ||
.register("requests", "Count of requests", metrics.requests.clone()); | ||
let metrics = Arc::new(Mutex::new(metrics)); | ||
let state = Arc::new(Mutex::new(state)); | ||
|
||
let router = Router::new() | ||
.route("/metrics", get(metrics_handler)) | ||
.with_state(state) | ||
.route("/handler", get(some_handler)) | ||
.with_state(metrics); | ||
let port = 8080; | ||
let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)) | ||
.await | ||
.unwrap(); | ||
|
||
axum::serve(listener, router).await.unwrap(); | ||
} |