From d62b837246e1279ff00af27b1a59bbbb7975a508 Mon Sep 17 00:00:00 2001 From: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com> Date: Sun, 15 Nov 2026 01:28:54 +0000 Subject: [PATCH 1/2] fix(sdk): record the latest error status whatever its description Span::set_status compared statuses with the derived PartialOrd, which orders two Error values by their description. A later error therefore replaced an earlier one only when its description sorted after it, so "timeout" followed by "connection refused" kept "timeout". Ok stays final and Unset is still ignored; otherwise the latest status is recorded, as the Status docs describe. --- opentelemetry-sdk/CHANGELOG.md | 3 +++ opentelemetry-sdk/src/trace/span.rs | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 09c0ee80cc..f60b55c497 100644 --- a/opentelemetry-sdk/CHANGELOG.md +++ b/opentelemetry-sdk/CHANGELOG.md @@ -2,6 +2,9 @@ ## vNext +- Fix `Span::set_status` keeping an earlier `Status::Error` when a later one + has a description that sorts first alphabetically. The latest error is now + recorded; `Ok` stays final and `Unset` is still ignored. - Improved metrics measurement performance by speeding up attribute set lookups (faster `KeyValue` equality and boxed attribute keys). ([#3787](https://github.com/open-telemetry/opentelemetry-rust/pull/3787)) diff --git a/opentelemetry-sdk/src/trace/span.rs b/opentelemetry-sdk/src/trace/span.rs index 49da51ac98..b504511d6b 100644 --- a/opentelemetry-sdk/src/trace/span.rs +++ b/opentelemetry-sdk/src/trace/span.rs @@ -286,9 +286,9 @@ impl opentelemetry::trace::Span for Span { /// If used, this will override the default span status, which is [`Status::Unset`]. fn set_status(&mut self, status: Status) { self.with_data(|data| { - // check if we should update the status - // These values form a total order: Ok > Error > Unset. - if status > data.status { + // Ok is final and Unset is ignored. Otherwise the latest status wins, + // so a later Error replaces an earlier one whatever its description. + if data.status != Status::Ok && status != Status::Unset { data.status = status; } }); @@ -694,6 +694,20 @@ mod tests { span.set_status(Status::error("error")); span.with_data(|data| assert_ne!(data.status, Status::Ok)); } + { + let mut span = create_span(); + // the last error is recorded, whatever the order of the descriptions + span.set_status(Status::error("timeout")); + span.set_status(Status::error("connection refused")); + span.with_data(|data| assert_eq!(data.status, Status::error("connection refused"))); + } + { + let mut span = create_span(); + // unset does not clear an error + span.set_status(Status::error("error")); + span.set_status(Status::Unset); + span.with_data(|data| assert_eq!(data.status, Status::error("error"))); + } } #[test] From 81e6ad4bbbac7a4467ff06423851e39c2c408dc3 Mon Sep 17 00:00:00 2001 From: WatchTree-19 <119982314+WatchTree-19@users.noreply.github.com> Date: Mon, 16 Nov 2026 15:07:22 +0000 Subject: [PATCH 2/2] docs: describe the new Error status rule in ObjectSafeSpan::set_status --- opentelemetry/src/global/trace.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/opentelemetry/src/global/trace.rs b/opentelemetry/src/global/trace.rs index 7aae010d87..62933a7e40 100644 --- a/opentelemetry/src/global/trace.rs +++ b/opentelemetry/src/global/trace.rs @@ -64,10 +64,10 @@ pub trait ObjectSafeSpan { /// Sets the status of the `Span`. `message` MUST be ignored when the status is `OK` or /// `Unset`. /// - /// The order of status is `Ok` > `Error` > `Unset`. That's means set the status - /// to `Unset` will always be ignore, set the status to `Error` only works when current - /// status is `Unset`, set the status to `Ok` will be consider final and any further call - /// to this function will be ignore. + /// The order of status is `Ok` > `Error` > `Unset`. Setting the status to `Unset` is + /// always ignored. Setting it to `Error` replaces the current status unless that status + /// is `Ok`, so the latest error is kept. Setting it to `Ok` is final, and any further + /// call to this function is ignored. fn set_status(&mut self, status: Status); /// Updates the `Span`'s name. After this update, any sampling behavior based on the