Skip to content

Commit 4d0f582

Browse files
authored
feat(telemetry): add IP logging (#16)
* feat(telemetry): add IP logging
1 parent 7bcb232 commit 4d0f582

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "stoic-quotes"
3-
version = "0.3.8"
3+
version = "0.3.9"
44
edition = "2021"
55
authors = ["Jose Storopoli <[email protected]>"]
66
description = "Stoic quotes API backend"

src/app.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::pages::{plain_quote, quote, root};
44
use axum::{http::header::USER_AGENT, http::Request, response::IntoResponse, routing::get, Router};
5-
use std::{env::current_dir, path::PathBuf};
5+
use std::{env::current_dir, net::SocketAddr, path::PathBuf};
66
use tower_http::{services::ServeDir, trace::TraceLayer};
77
use tracing::info;
88

@@ -11,14 +11,19 @@ use tracing::info;
1111
/// return a plain quote.
1212
/// Otherwise, return the root page.
1313
async fn handle_user_agent<T>(req: Request<T>) -> impl IntoResponse {
14+
let peer_addr = req
15+
.extensions()
16+
.get::<SocketAddr>()
17+
.map(ToString::to_string)
18+
.unwrap_or_else(|| "unknown".to_string());
1419
let header = Request::headers(&req);
1520
let user_agent: String = if let Some(user_agent) = header.get(USER_AGENT) {
1621
user_agent.clone().to_str().unwrap().to_string()
1722
} else {
1823
"blank".to_string()
1924
};
2025

21-
info!("got user agent: {user_agent}");
26+
info!("Request from {peer_addr} with User-Agent: {user_agent}");
2227

2328
if user_agent.contains("curl") || user_agent.contains("Wget") {
2429
plain_quote().await.into_response()

0 commit comments

Comments
 (0)