-
Notifications
You must be signed in to change notification settings - Fork 600
fix: Report error using OTel convention #2808
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cijothomas
merged 19 commits into
open-telemetry:main
from
cijothomas:cijothomas/tracing-error
Mar 20, 2025
Merged
Changes from 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
6baf3cb
fix: Report error using OTel convention
cijothomas 44db6ec
fmt
cijothomas fe2d6e4
removed doc change
cijothomas 8d2016b
test: validate code based config is preferrred over env vars in trace…
gruebel 162d2f4
chore: remove deprecated functions/methods in trace::Config (#2810)
gruebel baf0462
Merge branch 'main' into cijothomas/tracing-error
cijothomas 77f9eca
Merge branch 'main' into cijothomas/tracing-error
cijothomas e671415
Merge branch 'main' into cijothomas/tracing-error
cijothomas ee0887d
Merge branch 'main' into cijothomas/tracing-error
cijothomas 0a9776f
Merge branch 'main' into cijothomas/tracing-error
cijothomas e892ffe
Merge branch 'main' into cijothomas/tracing-error
cijothomas a5fe25a
Merge branch 'main' into cijothomas/tracing-error
cijothomas 5ce73fc
Merge branch 'main' into cijothomas/tracing-error
cijothomas 9308e48
support rest of fields too
cijothomas fd3c173
lib doc
cijothomas 3334884
fmt
cijothomas 2f17706
Merge branch 'main' into cijothomas/tracing-error
cijothomas 327442b
Apply suggestions from code review
cijothomas fc310aa
Merge branch 'main' into cijothomas/tracing-error
cijothomas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -80,6 +80,18 @@ | |
| } | ||
| } | ||
|
|
||
| fn record_error( | ||
| &mut self, | ||
| _field: &tracing_core::Field, | ||
| value: &(dyn std::error::Error + 'static), | ||
| ) { | ||
| self.log_record.add_attribute( | ||
| Key::new("exception.message"), | ||
| AnyValue::from(value.to_string()), | ||
| ); | ||
| // No ability to get exception.stacktrace or exception.type from the error today. | ||
| } | ||
|
|
||
| fn record_bytes(&mut self, field: &tracing_core::Field, value: &[u8]) { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(value)); | ||
|
|
@@ -135,6 +147,34 @@ | |
| } | ||
| } | ||
|
|
||
| fn record_i128(&mut self, field: &tracing::field::Field, value: i128) { | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| if is_duplicated_metadata(field.name()) { | ||
| return; | ||
| } | ||
| if let Ok(signed) = i64::try_from(value) { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(signed)); | ||
| } else { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}"))); | ||
| } | ||
| } | ||
|
|
||
| fn record_u128(&mut self, field: &tracing::field::Field, value: u128) { | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| if is_duplicated_metadata(field.name()) { | ||
| return; | ||
| } | ||
| if let Ok(signed) = i64::try_from(value) { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(signed)); | ||
| } else { | ||
| self.log_record | ||
| .add_attribute(Key::new(field.name()), AnyValue::from(format!("{value:?}"))); | ||
| } | ||
| } | ||
|
|
||
| // TODO: Remaining field types from AnyValue : Bytes, ListAny, Boolean | ||
| } | ||
|
|
||
|
|
@@ -247,7 +287,7 @@ | |
| use opentelemetry::trace::{TraceContextExt, TraceFlags, Tracer}; | ||
| use opentelemetry::InstrumentationScope; | ||
| use opentelemetry::{logs::AnyValue, Key}; | ||
| use opentelemetry_sdk::error::OTelSdkResult; | ||
| use opentelemetry_sdk::error::{OTelSdkError, OTelSdkResult}; | ||
| use opentelemetry_sdk::logs::{InMemoryLogExporter, LogProcessor}; | ||
| use opentelemetry_sdk::logs::{LogBatch, LogExporter}; | ||
| use opentelemetry_sdk::logs::{SdkLogRecord, SdkLoggerProvider}; | ||
|
|
@@ -355,7 +395,11 @@ | |
| let big_u64value: u64 = u64::MAX; | ||
| let small_usizevalue: usize = 42; | ||
| let big_usizevalue: usize = usize::MAX; | ||
| error!(name: "my-event-name", target: "my-system", event_id = 20, bytes = &b"abc"[..], small_u64value, big_u64value, small_usizevalue, big_usizevalue, user_name = "otel", user_email = "[email protected]"); | ||
| let small_u128value: u128 = 42; | ||
| let big_u128value: u128 = u128::MAX; | ||
| let small_i128value: i128 = 42; | ||
| let big_i128value: i128 = i128::MAX; | ||
| error!(name: "my-event-name", target: "my-system", event_id = 20, bytes = &b"abc"[..], error = &OTelSdkError::AlreadyShutdown as &dyn std::error::Error, small_u64value, big_u64value, small_usizevalue, big_usizevalue, small_u128value, big_u128value, small_i128value, big_i128value, user_name = "otel", user_email = "[email protected]"); | ||
| assert!(logger_provider.force_flush().is_ok()); | ||
|
|
||
| // Assert TODO: move to helper methods | ||
|
|
@@ -386,9 +430,9 @@ | |
|
|
||
| // Validate attributes | ||
| #[cfg(not(feature = "experimental_metadata_attributes"))] | ||
| assert_eq!(log.record.attributes_iter().count(), 8); | ||
| assert_eq!(log.record.attributes_iter().count(), 13); | ||
| #[cfg(feature = "experimental_metadata_attributes")] | ||
| assert_eq!(log.record.attributes_iter().count(), 12); | ||
| assert_eq!(log.record.attributes_iter().count(), 17); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("event_id"), | ||
|
|
@@ -404,6 +448,11 @@ | |
| &Key::new("user_email"), | ||
| &AnyValue::String("[email protected]".into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("exception.message"), | ||
| &AnyValue::String(OTelSdkError::AlreadyShutdown.to_string().into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("small_u64value"), | ||
|
|
@@ -424,6 +473,26 @@ | |
| &Key::new("big_usizevalue"), | ||
| &AnyValue::String(format!("{}", u64::MAX).into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("small_u128value"), | ||
| &AnyValue::Int(42.into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("big_u128value"), | ||
| &AnyValue::String(format!("{}", u128::MAX).into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("small_i128value"), | ||
| &AnyValue::Int(42.into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("big_i128value"), | ||
| &AnyValue::String(format!("{}", i128::MAX).into()) | ||
| )); | ||
| assert!(attributes_contains( | ||
| &log.record, | ||
| &Key::new("bytes"), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.