Skip to content

Commit 7c4545a

Browse files
authored
api: rename Span::record_exception to Span::record_error (#756)
This change aligns the error recording methods with Rust's naming conventions which prefer the term `Error` over `Exception`. This also removes `Span::record_exception_with_stacktrace` as Rust's backtrace functionality is not yet stable. Users who wish to record this data can use `Span::add_event` directly.
1 parent ce6113d commit 7c4545a

File tree

3 files changed

+22
-54
lines changed

3 files changed

+22
-54
lines changed

opentelemetry-api/src/trace/context.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,14 @@ impl SpanRef<'_> {
6060
self.with_inner_mut(|inner| inner.add_event(name, attributes))
6161
}
6262

63-
/// Record an exception event
64-
pub fn record_exception(&self, err: &dyn Error) {
65-
self.with_inner_mut(|inner| inner.record_exception(err))
66-
}
67-
68-
/// Record an exception event with stacktrace
69-
pub fn record_exception_with_stacktrace<T>(&self, err: &dyn Error, stacktrace: T)
70-
where
71-
T: Into<Cow<'static, str>>,
72-
{
73-
self.with_inner_mut(|inner| inner.record_exception_with_stacktrace(err, stacktrace))
63+
/// Record an error as an event for this span.
64+
///
65+
/// An additional call to [Span::set_status] is required if the status of the
66+
/// span should be set to error, as this method does not change the span status.
67+
///
68+
/// If this span is not being recorded then this method does nothing.
69+
pub fn record_error(&self, err: &dyn Error) {
70+
self.with_inner_mut(|inner| inner.record_error(err))
7471
}
7572

7673
/// Record an event with a timestamp in the context this span.

opentelemetry-api/src/trace/span.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,17 @@ pub trait Span {
6363
self.add_event_with_timestamp(name, crate::time::now(), attributes)
6464
}
6565

66-
/// Record an exception event
67-
fn record_exception(&mut self, err: &dyn Error) {
68-
let attributes = vec![KeyValue::new("exception.message", err.to_string())];
69-
self.add_event("exception", attributes);
70-
}
71-
72-
/// Record an exception event with stacktrace
73-
fn record_exception_with_stacktrace<T>(&mut self, err: &dyn Error, stacktrace: T)
74-
where
75-
T: Into<Cow<'static, str>>,
76-
{
77-
let attributes = vec![
78-
KeyValue::new("exception.message", err.to_string()),
79-
KeyValue::new("exception.stacktrace", stacktrace.into()),
80-
];
81-
82-
self.add_event("exception", attributes);
66+
/// Record an error as an event for this span.
67+
///
68+
/// An additional call to [Span::set_status] is required if the status of the
69+
/// span should be set to error, as this method does not change the span status.
70+
///
71+
/// If this span is not being recorded then this method does nothing.
72+
fn record_error(&mut self, err: &dyn Error) {
73+
if self.is_recording() {
74+
let attributes = vec![KeyValue::new("exception.message", err.to_string())];
75+
self.add_event("exception", attributes);
76+
}
8377
}
8478

8579
/// Record an event with a timestamp in the context this span.

opentelemetry-sdk/src/trace/span.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -359,10 +359,10 @@ mod tests {
359359
}
360360

361361
#[test]
362-
fn record_exception() {
362+
fn record_error() {
363363
let mut span = create_span();
364364
let err = std::io::Error::from(std::io::ErrorKind::Other);
365-
span.record_exception(&err);
365+
span.record_error(&err);
366366
span.with_data(|data| {
367367
if let Some(event) = data.events.iter().next() {
368368
assert_eq!(event.name, "exception");
@@ -376,28 +376,6 @@ mod tests {
376376
});
377377
}
378378

379-
#[test]
380-
fn record_exception_with_stacktrace() {
381-
let mut span = create_span();
382-
let err = std::io::Error::from(std::io::ErrorKind::Other);
383-
let stacktrace = "stacktrace...".to_string();
384-
span.record_exception_with_stacktrace(&err, stacktrace.clone());
385-
span.with_data(|data| {
386-
if let Some(event) = data.events.iter().next() {
387-
assert_eq!(event.name, "exception");
388-
assert_eq!(
389-
event.attributes,
390-
vec![
391-
KeyValue::new("exception.message", err.to_string()),
392-
KeyValue::new("exception.stacktrace", stacktrace),
393-
]
394-
);
395-
} else {
396-
panic!("no event");
397-
}
398-
});
399-
}
400-
401379
#[test]
402380
fn set_attribute() {
403381
let mut span = create_span();
@@ -517,8 +495,7 @@ mod tests {
517495
vec![KeyValue::new("k", "v")],
518496
);
519497
let err = std::io::Error::from(std::io::ErrorKind::Other);
520-
span.record_exception(&err);
521-
span.record_exception_with_stacktrace(&err, "stacktrace...".to_string());
498+
span.record_error(&err);
522499
span.set_attribute(KeyValue::new("k", "v"));
523500
span.set_status(StatusCode::Error, "ERROR".to_string());
524501
span.update_name("new_name".to_string());

0 commit comments

Comments
 (0)