Spaces:
Runtime error
Runtime error
fix: remove redundant tracing configurations
Browse files- src/main.rs +16 -41
src/main.rs
CHANGED
|
@@ -1,19 +1,16 @@
|
|
| 1 |
-
use std::
|
| 2 |
|
| 3 |
use axum::{
|
| 4 |
-
body::Bytes,
|
| 5 |
extract::MatchedPath,
|
| 6 |
-
http::{
|
| 7 |
-
response::Response,
|
| 8 |
};
|
| 9 |
use tokio::net::TcpListener;
|
| 10 |
use tower::ServiceBuilder;
|
| 11 |
use tower_http::{
|
| 12 |
-
classify::ServerErrorsFailureClass,
|
| 13 |
cors::{Any, CorsLayer},
|
| 14 |
trace::TraceLayer,
|
| 15 |
};
|
| 16 |
-
use tracing::
|
| 17 |
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
| 18 |
use utoipa::OpenApi;
|
| 19 |
use utoipa_axum::router::OpenApiRouter;
|
|
@@ -53,43 +50,21 @@ async fn main() -> anyhow::Result<()> {
|
|
| 53 |
.nest("/api", controllers::router())
|
| 54 |
.split_for_parts();
|
| 55 |
|
| 56 |
-
let trace_layer = TraceLayer::new_for_http()
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
.map(MatchedPath::as_str);
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
)
|
| 71 |
-
})
|
| 72 |
-
.on_request(|_request: &Request<_>, _span: &Span| {
|
| 73 |
-
// You can use `_span.record("some_other_field", value)` in one of these
|
| 74 |
-
// closures to attach a value to the initially empty field in the info_span
|
| 75 |
-
// created above.
|
| 76 |
-
})
|
| 77 |
-
.on_response(|_response: &Response, _latency: Duration, _span: &Span| {
|
| 78 |
-
// ...
|
| 79 |
-
})
|
| 80 |
-
.on_body_chunk(|_chunk: &Bytes, _latency: Duration, _span: &Span| {
|
| 81 |
-
// ...
|
| 82 |
-
})
|
| 83 |
-
.on_eos(
|
| 84 |
-
|_trailers: Option<&HeaderMap>, _stream_duration: Duration, _span: &Span| {
|
| 85 |
-
// ...
|
| 86 |
-
},
|
| 87 |
)
|
| 88 |
-
|
| 89 |
-
|_error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {
|
| 90 |
-
// ...
|
| 91 |
-
},
|
| 92 |
-
);
|
| 93 |
|
| 94 |
let app = router
|
| 95 |
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", api.clone()))
|
|
|
|
| 1 |
+
use std::net::SocketAddr;
|
| 2 |
|
| 3 |
use axum::{
|
|
|
|
| 4 |
extract::MatchedPath,
|
| 5 |
+
http::{Method, Request},
|
|
|
|
| 6 |
};
|
| 7 |
use tokio::net::TcpListener;
|
| 8 |
use tower::ServiceBuilder;
|
| 9 |
use tower_http::{
|
|
|
|
| 10 |
cors::{Any, CorsLayer},
|
| 11 |
trace::TraceLayer,
|
| 12 |
};
|
| 13 |
+
use tracing::info_span;
|
| 14 |
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
| 15 |
use utoipa::OpenApi;
|
| 16 |
use utoipa_axum::router::OpenApiRouter;
|
|
|
|
| 50 |
.nest("/api", controllers::router())
|
| 51 |
.split_for_parts();
|
| 52 |
|
| 53 |
+
let trace_layer = TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
|
| 54 |
+
// Log the matched route's path (with placeholders not filled in).
|
| 55 |
+
// Use request.uri() or OriginalUri if you want the real path.
|
| 56 |
+
let matched_path = request
|
| 57 |
+
.extensions()
|
| 58 |
+
.get::<MatchedPath>()
|
| 59 |
+
.map(MatchedPath::as_str);
|
|
|
|
| 60 |
|
| 61 |
+
info_span!(
|
| 62 |
+
"http_request",
|
| 63 |
+
method = ?request.method(),
|
| 64 |
+
matched_path,
|
| 65 |
+
some_other_field = tracing::field::Empty,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
)
|
| 67 |
+
});
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
let app = router
|
| 70 |
.merge(SwaggerUi::new("/swagger-ui").url("/api-docs/openapi.json", api.clone()))
|