Skip to content

Commit 10366f4

Browse files
style(logging): improve logged output
1 parent cf7a12d commit 10366f4

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

cog.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
# [Cocogitto](https://docs.cocogitto.io/) Configuration
22

3+
tag_prefix = "v"
34
from_latest_tag = false
4-
ignore_merge_commits = false
5+
ignore_merge_commits = true
56
branch_whitelist = [ "main", "release/**" ]
67
pre_bump_hooks = [
78
"cargo build --release",
8-
"cargo doc",
99
"echo 'bumping from {{latest}} to {{version}}'",
1010
"cargo bump {{version}}",
1111
]

src/main.rs

+22-17
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,16 @@ use std::{collections::HashMap, env, fmt::Debug, net::SocketAddr};
99
// Third Party Imports
1010
use axum::{
1111
body::Bytes,
12-
extract::{Json, Path, Query},
12+
extract::{ConnectInfo, Json, Path, Query},
1313
http::{HeaderMap, Method},
1414
middleware, routing, Router,
1515
};
1616

1717
pub(crate) mod metrics;
1818

19-
#[derive(Debug, serde::Serialize, Clone)]
19+
#[derive(Clone, Debug, serde::Serialize)]
2020
struct Echo {
21+
client: String,
2122
method: String,
2223
path: String,
2324
headers: HashMap<String, String>,
@@ -28,6 +29,8 @@ struct Echo {
2829
#[derive(Debug, clap::Parser)]
2930
#[command(author, version, about)]
3031
struct Args {
32+
#[arg(short = 'h', long = "host", env = "ECHO_HOST", default_value = "[::]")]
33+
pub host: String,
3134
#[arg(short = 'p', long = "port", env = "ECHO_PORT", default_value_t = 8080)]
3235
pub port: usize,
3336
#[arg(
@@ -52,8 +55,9 @@ struct Args {
5255
pub log_level: tracing::Level,
5356
}
5457

55-
#[tracing::instrument]
58+
#[tracing::instrument(ret, skip_all, parent = None)]
5659
async fn serialize_request(
60+
ConnectInfo(client): ConnectInfo<SocketAddr>,
5761
method: Method,
5862
path: Option<Path<String>>,
5963
Query(params): Query<HashMap<String, String>>,
@@ -90,11 +94,10 @@ async fn serialize_request(
9094
})
9195
};
9296

93-
let method = method.to_string();
94-
95-
tracing::info!("{} {}", &method, &path);
97+
let (client, method) = (client.to_string(), method.to_string());
9698

9799
Json(Echo {
100+
client,
98101
method,
99102
path,
100103
headers,
@@ -130,26 +133,26 @@ async fn echo_router() -> anyhow::Result<Router> {
130133
.route_layer(middleware::from_fn(metrics::track_metrics)))
131134
}
132135

133-
#[tracing::instrument]
134-
async fn serve_app(port: usize) -> anyhow::Result<()> {
136+
#[tracing::instrument(skip_all)]
137+
async fn serve_app(host: &str, port: usize) -> anyhow::Result<()> {
135138
let app = echo_router().await?;
136139

137-
let addr: SocketAddr = format!("[::]:{port}").parse()?;
140+
let addr: SocketAddr = format!("{host}:{port}").parse()?;
138141

139142
tracing::info!("`echo-rs` server listening at: http://{addr}");
140143

141144
axum::Server::bind(&addr)
142-
.serve(app.into_make_service())
145+
.serve(app.into_make_service_with_connect_info::<SocketAddr>())
143146
.await?;
144147

145148
Ok(())
146149
}
147150

148-
#[tracing::instrument]
149-
async fn serve_metrics(port: usize) -> anyhow::Result<()> {
151+
#[tracing::instrument(skip_all)]
152+
async fn serve_metrics(host: &str, port: usize) -> anyhow::Result<()> {
150153
let app = metrics::router();
151154

152-
let addr: SocketAddr = format!("[::]:{port}").parse()?;
155+
let addr: SocketAddr = format!("{host}:{port}").parse()?;
153156

154157
tracing::info!("Serving Prometheus metrics at: http://{addr}");
155158

@@ -161,8 +164,8 @@ async fn serve_metrics(port: usize) -> anyhow::Result<()> {
161164
Ok(())
162165
}
163166

164-
#[tokio::main]
165167
#[tracing::instrument]
168+
#[tokio::main]
166169
async fn main() -> anyhow::Result<()> {
167170
let args = <Args as clap::Parser>::parse();
168171

@@ -188,10 +191,12 @@ async fn main() -> anyhow::Result<()> {
188191
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
189192

190193
if !args.metrics {
191-
serve_app(args.port).await
194+
serve_app(&args.host, args.port).await
192195
} else {
193-
let (echo_server, metrics_server) =
194-
tokio::join!(serve_app(args.port), serve_metrics(args.metrics_port));
196+
let (echo_server, metrics_server) = tokio::join!(
197+
serve_app(&args.host, args.port),
198+
serve_metrics(&args.host, args.metrics_port)
199+
);
195200
let (_, _) = (echo_server?, metrics_server?);
196201

197202
Ok(())

src/metrics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use axum::{
99
};
1010
use metrics_exporter_prometheus::{Matcher, PrometheusBuilder, PrometheusHandle};
1111

12+
#[tracing::instrument]
1213
pub(crate) fn router() -> Router {
1314
let recorder_handle = setup_metrics_recorder();
1415
Router::new().route(
@@ -17,6 +18,7 @@ pub(crate) fn router() -> Router {
1718
)
1819
}
1920

21+
#[tracing::instrument]
2022
pub(crate) fn setup_metrics_recorder() -> PrometheusHandle {
2123
const EXPONENTIAL_SECONDS: &[f64] = &[
2224
0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0,
@@ -32,6 +34,8 @@ pub(crate) fn setup_metrics_recorder() -> PrometheusHandle {
3234
.unwrap()
3335
}
3436

37+
#[tracing::instrument(skip_all)]
38+
#[allow(clippy::let_with_type_underscore)]
3539
pub(crate) async fn track_metrics<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
3640
let start = Instant::now();
3741
let path = if let Some(matched_path) = req.extensions().get::<MatchedPath>() {

0 commit comments

Comments
 (0)