refactor
This commit is contained in:
@@ -40,16 +40,16 @@ async fn main() -> MainResult {
|
|||||||
Err(e) => return e.into(),
|
Err(e) => return e.into(),
|
||||||
};
|
};
|
||||||
|
|
||||||
telemetry::init_tracing(
|
telemetry::tracing::init_tracing(
|
||||||
if args.enable_opentelemetry.into() {
|
if args.enable_opentelemetry.into() {
|
||||||
telemetry::OpenTelemetryConfig::Enabled
|
telemetry::tracing::OpenTelemetryConfig::Enabled
|
||||||
} else {
|
} else {
|
||||||
telemetry::OpenTelemetryConfig::Disabled
|
telemetry::tracing::OpenTelemetryConfig::Disabled
|
||||||
},
|
},
|
||||||
if args.enable_tokio_console.into() {
|
if args.enable_tokio_console.into() {
|
||||||
telemetry::TokioConsoleConfig::Enabled
|
telemetry::tracing::TokioConsoleConfig::Enabled
|
||||||
} else {
|
} else {
|
||||||
telemetry::TokioConsoleConfig::Disabled
|
telemetry::tracing::TokioConsoleConfig::Disabled
|
||||||
},
|
},
|
||||||
args,
|
args,
|
||||||
|args| -> Pin<Box<dyn std::future::Future<Output = MainResult>>> {
|
|args| -> Pin<Box<dyn std::future::Future<Output = MainResult>>> {
|
||||||
@@ -80,7 +80,7 @@ async fn main() -> MainResult {
|
|||||||
|
|
||||||
// build our application with a route
|
// build our application with a route
|
||||||
let app = routing::router(state);
|
let app = routing::router(state);
|
||||||
let app = telemetry::init_request_tracing(app);
|
let app = telemetry::tracing::init_request_tracing(app);
|
||||||
|
|
||||||
let mut join_set = tokio::task::JoinSet::new();
|
let mut join_set = tokio::task::JoinSet::new();
|
||||||
|
|
||||||
@@ -98,7 +98,7 @@ async fn main() -> MainResult {
|
|||||||
Ok(ip) => SocketAddr::from((ip, port)),
|
Ok(ip) => SocketAddr::from((ip, port)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let (app, task) = telemetry::prometheus_server(app, addr);
|
let (app, task) = telemetry::metrics::prometheus_server(app, addr);
|
||||||
join_set.spawn(task);
|
join_set.spawn(task);
|
||||||
app
|
app
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
38
rust/src/telemetry/metrics.rs
Normal file
38
rust/src/telemetry/metrics.rs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
use std::future::Future;
|
||||||
|
|
||||||
|
use axum::routing::get;
|
||||||
|
use axum::Router;
|
||||||
|
|
||||||
|
use axum_prometheus::{Handle, MakeDefaultHandle, PrometheusMetricLayerBuilder};
|
||||||
|
|
||||||
|
use crate::{Error, StartError};
|
||||||
|
|
||||||
|
pub fn prometheus_server(
|
||||||
|
router: Router,
|
||||||
|
addr: std::net::SocketAddr,
|
||||||
|
) -> (Router, impl Future<Output = Result<(), Error>>) {
|
||||||
|
let (prometheus_layer, metric_handle) = PrometheusMetricLayerBuilder::new()
|
||||||
|
.with_prefix(env!("CARGO_PKG_NAME"))
|
||||||
|
.with_metrics_from_fn(|| Handle::make_default_handle())
|
||||||
|
.build_pair();
|
||||||
|
|
||||||
|
let app = Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
|
||||||
|
|
||||||
|
let task = async move {
|
||||||
|
if let Err(e) = axum::Server::try_bind(&addr)
|
||||||
|
.map_err(|e| {
|
||||||
|
Error::Start(StartError::BindError {
|
||||||
|
message: e.to_string(),
|
||||||
|
addr,
|
||||||
|
})
|
||||||
|
})?
|
||||||
|
.serve(app.into_make_service())
|
||||||
|
.await
|
||||||
|
{
|
||||||
|
return Err(<hyper::Error as Into<Error>>::into(e));
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
};
|
||||||
|
|
||||||
|
(router.layer(prometheus_layer), task)
|
||||||
|
}
|
||||||
2
rust/src/telemetry/mod.rs
Normal file
2
rust/src/telemetry/mod.rs
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
pub mod metrics;
|
||||||
|
pub mod tracing;
|
||||||
@@ -4,15 +4,12 @@ use std::io;
|
|||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use axum::routing::get;
|
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
|
||||||
use http::Request;
|
use http::Request;
|
||||||
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
|
||||||
use tracing::Span;
|
use tracing::Span;
|
||||||
|
|
||||||
use axum_prometheus::{Handle, MakeDefaultHandle, PrometheusMetricLayerBuilder};
|
|
||||||
|
|
||||||
use tracing::Instrument;
|
|
||||||
use tracing_subscriber::{
|
use tracing_subscriber::{
|
||||||
filter::{LevelFilter, Targets},
|
filter::{LevelFilter, Targets},
|
||||||
fmt::{format::Format, Layer},
|
fmt::{format::Format, Layer},
|
||||||
@@ -20,9 +17,10 @@ use tracing_subscriber::{
|
|||||||
prelude::*,
|
prelude::*,
|
||||||
registry::Registry,
|
registry::Registry,
|
||||||
};
|
};
|
||||||
use uuid::Uuid;
|
|
||||||
|
|
||||||
use crate::{Error, StartError};
|
use tracing::Instrument;
|
||||||
|
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use opentelemetry::{global, runtime::Tokio};
|
use opentelemetry::{global, runtime::Tokio};
|
||||||
|
|
||||||
@@ -232,33 +230,3 @@ pub fn init_request_tracing(router: Router) -> Router {
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prometheus_server(
|
|
||||||
router: Router,
|
|
||||||
addr: std::net::SocketAddr,
|
|
||||||
) -> (Router, impl Future<Output = Result<(), Error>>) {
|
|
||||||
let (prometheus_layer, metric_handle) = PrometheusMetricLayerBuilder::new()
|
|
||||||
.with_prefix(env!("CARGO_PKG_NAME"))
|
|
||||||
.with_metrics_from_fn(|| Handle::make_default_handle())
|
|
||||||
.build_pair();
|
|
||||||
|
|
||||||
let app = Router::new().route("/metrics", get(|| async move { metric_handle.render() }));
|
|
||||||
|
|
||||||
let task = async move {
|
|
||||||
if let Err(e) = axum::Server::try_bind(&addr)
|
|
||||||
.map_err(|e| {
|
|
||||||
Error::Start(StartError::BindError {
|
|
||||||
message: e.to_string(),
|
|
||||||
addr,
|
|
||||||
})
|
|
||||||
})?
|
|
||||||
.serve(app.into_make_service())
|
|
||||||
.await
|
|
||||||
{
|
|
||||||
return Err(<hyper::Error as Into<Error>>::into(e));
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
};
|
|
||||||
|
|
||||||
(router.layer(prometheus_layer), task)
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user