Skip to content

Commit

Permalink
chore(otel): Treat status_code=none as 200(OK) (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
andeya committed Sep 24, 2024
1 parent 2c4e8c3 commit ec25bc2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions crates/otel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
5 changes: 4 additions & 1 deletion crates/otel/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion crates/otel/src/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ec25bc2

Please sign in to comment.