diff --git a/crates/otel/Cargo.toml b/crates/otel/Cargo.toml index 33caeaad1..adc1d109d 100644 --- a/crates/otel/Cargo.toml +++ b/crates/otel/Cargo.toml @@ -25,6 +25,7 @@ opentelemetry-http = { workspace = true } opentelemetry-semantic-conventions = { workspace = true } opentelemetry = { workspace = true, features = ["metrics"] } salvo_core = { workspace = true, default-features = false } +tracing = { workspace = true } [dev-dependencies] salvo_core = { workspace = true, features = ["test"] } diff --git a/crates/otel/src/metrics.rs b/crates/otel/src/metrics.rs index 18366d0e4..e2bd5ad97 100644 --- a/crates/otel/src/metrics.rs +++ b/crates/otel/src/metrics.rs @@ -63,7 +63,10 @@ impl Handler for Metrics { ctrl.call_next(req, depot, res).await; let elapsed = s.elapsed(); - let status = res.status_code.unwrap_or(StatusCode::NOT_FOUND); + let status = res.status_code.unwrap_or_else(|| { + tracing::info!("[otel::Metrics] Treat status_code=none as 200(OK)."); + StatusCode::OK + }); labels.push(KeyValue::new( trace::HTTP_RESPONSE_STATUS_CODE, status.as_u16() as i64, diff --git a/crates/otel/src/tracing.rs b/crates/otel/src/tracing.rs index 21eced0c4..7cd9cf5a4 100644 --- a/crates/otel/src/tracing.rs +++ b/crates/otel/src/tracing.rs @@ -78,7 +78,10 @@ where let cx = Context::current(); let span = cx.span(); - let status = res.status_code.unwrap_or(StatusCode::NOT_FOUND); + let status = res.status_code.unwrap_or_else(|| { + tracing::info!("[otel::Tracing] Treat status_code=none as 200(OK)."); + StatusCode::OK + }); let event = if status.is_client_error() || status.is_server_error() { "request.failure" } else {