diff --git a/opentelemetry-sdk/CHANGELOG.md b/opentelemetry-sdk/CHANGELOG.md index 0862f640be..f88fabb8cb 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. - Fixed the experimental async-runtime `PeriodicReader`, `BatchSpanProcessor` and `BatchLogProcessor` waiting two intervals before their first collection or flush. The ticker already delays before firing, so the extra skipped tick is 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] 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