Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server logging by TraceLayer #3

Merged
merged 1 commit into from
Oct 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 143 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@ serde = "1.0.127"
tokio = { version = "1", features = ["full"] }
futures = "0.3"
nats = "0.13.0"
tower-http = { version = "0.1", features = ["trace"] }
tracing = "0.1"
tracing-subscriber = "0.2"
env_logger = "0.9.0"
3 changes: 2 additions & 1 deletion docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ services:
target: 'develop-stage'
dockerfile: Dockerfile
environment:
- NATS_HOST=nats://nats:4222
NATS_HOST: nats://nats:4222
RUST_LOG: server=trace,tower_http=trace
ports:
- "8088:8088"
volumes:
Expand Down
38 changes: 35 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
use axum::{handler::get, AddExtensionLayer, Router};
use std::{collections::HashMap, env, net::SocketAddr, sync::Arc};
use axum::{
body::Bytes,
handler::get,
http::{HeaderMap, Request, Response},
AddExtensionLayer, Router,
};
use std::{collections::HashMap, env, net::SocketAddr, sync::Arc, time::Duration};
use tokio::sync::{broadcast, Mutex};
use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer};
use tracing::Span;
mod handler;

pub struct AppState {
Expand All @@ -10,6 +17,8 @@ pub struct AppState {

#[tokio::main]
async fn main() {
env_logger::init();

let nats_host = env::var("NATS_HOST").expect("NATS_HOST is not defined");
let nc = match nats::asynk::connect(&nats_host).await {
Ok(nc) => nc,
Expand All @@ -25,9 +34,32 @@ async fn main() {
"/websocket/:group_id/:user_id",
get(handler::websocket::handler),
)
.layer(AddExtensionLayer::new(app_state.clone()));
.layer(AddExtensionLayer::new(app_state.clone()))
.layer(
TraceLayer::new_for_http()
.on_request(|request: &Request<_>, _span: &Span| {
tracing::debug!("started {} {}", request.method(), request.uri().path())
})
.on_response(|_response: &Response<_>, latency: Duration, _span: &Span| {
tracing::debug!("response generated in {:?}", latency)
})
.on_body_chunk(|chunk: &Bytes, _latency: Duration, _span: &Span| {
tracing::debug!("sending {} bytes", chunk.len())
})
.on_eos(
|_trailers: Option<&HeaderMap>, stream_duration: Duration, _span: &Span| {
tracing::debug!("stream closed after {:?}", stream_duration)
},
)
.on_failure(
|error: ServerErrorsFailureClass, _latency: Duration, _span: &Span| {
tracing::debug!("something went wrong: {:?}", error)
},
),
);

let addr = SocketAddr::from(([0, 0, 0, 0], 8088));
tracing::info!("listening on {}", addr);

let mut nc_task = tokio::spawn(async move {
let sub = match app_state.nc.subscribe("*").await {
Expand Down