-
Notifications
You must be signed in to change notification settings - Fork 761
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
Trial integrating spantrace extraction with tracing-subscriber via generic member access #1861
Closed
Closed
Changes from all commits
Commits
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 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 |
---|---|---|
@@ -0,0 +1,133 @@ | ||
//! This example demonstrates using the `tracing-error` crate's `SpanTrace` type | ||
//! to attach a trace context to a custom error type. | ||
#![deny(rust_2018_idioms)] | ||
#![feature(provide_any)] | ||
use std::any::Requisition; | ||
use std::fmt; | ||
use std::{error::Error, path::Path}; | ||
use tracing::{error, info}; | ||
use tracing_error::{ErrorSubscriber, SpanTrace}; | ||
use tracing_subscriber::prelude::*; | ||
|
||
#[derive(Debug)] | ||
struct FileError { | ||
context: SpanTrace, | ||
} | ||
|
||
impl FileError { | ||
fn new() -> Self { | ||
Self { | ||
context: SpanTrace::capture(), | ||
} | ||
} | ||
} | ||
|
||
impl Error for FileError { | ||
fn provide<'a>(&'a self, mut req: Requisition<'a, '_>) { | ||
req.provide_ref(&self.context) | ||
.provide_ref::<tracing::Span>(self.context.as_ref()); | ||
} | ||
} | ||
|
||
impl fmt::Display for FileError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.pad("file does not exist") | ||
} | ||
} | ||
|
||
#[tracing::instrument] | ||
fn read_file(path: &Path) -> Result<String, FileError> { | ||
Err(FileError::new()) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct ConfigError { | ||
source: FileError, | ||
} | ||
|
||
impl From<FileError> for ConfigError { | ||
fn from(source: FileError) -> Self { | ||
Self { source } | ||
} | ||
} | ||
|
||
impl Error for ConfigError { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
Some(&self.source) | ||
} | ||
} | ||
|
||
impl fmt::Display for ConfigError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.pad("config file cannot be loaded") | ||
} | ||
} | ||
|
||
#[tracing::instrument] | ||
fn load_config() -> Result<String, ConfigError> { | ||
let path = Path::new("my_config"); | ||
let config = read_file(&path)?; | ||
Ok(config) | ||
} | ||
|
||
struct App { | ||
// Imagine this is actually something we deserialize with serde | ||
config: String, | ||
} | ||
|
||
impl App { | ||
fn run() -> Result<(), AppError> { | ||
let this = Self::init()?; | ||
this.start() | ||
} | ||
|
||
fn init() -> Result<Self, ConfigError> { | ||
let config = load_config()?; | ||
Ok(Self { config }) | ||
} | ||
|
||
fn start(&self) -> Result<(), AppError> { | ||
// Pretend our actual application logic all exists here | ||
info!("Loaded config: {}", self.config); | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
struct AppError { | ||
source: ConfigError, | ||
} | ||
|
||
impl From<ConfigError> for AppError { | ||
fn from(source: ConfigError) -> Self { | ||
Self { source } | ||
} | ||
} | ||
|
||
impl Error for AppError { | ||
fn source(&self) -> Option<&(dyn Error + 'static)> { | ||
Some(&self.source) | ||
} | ||
} | ||
|
||
impl fmt::Display for AppError { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.pad("config invalid") | ||
} | ||
} | ||
|
||
#[tracing::instrument] | ||
fn main() { | ||
tracing_subscriber::registry() | ||
.with(tracing_subscriber::fmt::subscriber()) | ||
// The `ErrorSubscriber` subscriber layer enables the use of `SpanTrace`. | ||
.with(ErrorSubscriber::default()) | ||
.init(); | ||
|
||
if let Err(e) = App::run() { | ||
error!( | ||
error = &e as &(dyn Error + 'static), | ||
"App exited unsuccessfully" | ||
); | ||
} | ||
} |
This file contains 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 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 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 |
---|---|---|
|
@@ -1122,21 +1122,43 @@ impl<'a> field::Visit for DefaultVisitor<'a> { | |
} | ||
|
||
fn record_error(&mut self, field: &Field, value: &(dyn std::error::Error + 'static)) { | ||
if let Some(source) = value.source() { | ||
let italic = self.writer.italic(); | ||
self.record_debug( | ||
field, | ||
&format_args!( | ||
"{} {}{}{}{}", | ||
value, | ||
italic.paint(field.name()), | ||
italic.paint(".sources"), | ||
self.writer.dimmed().paint("="), | ||
ErrorSourceList(source) | ||
), | ||
) | ||
} else { | ||
self.record_debug(field, &format_args!("{}", value)) | ||
let source = value.source(); | ||
let spantrace = value.chain().find_map(|value| value.request_ref::<tracing::Span>()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this would be |
||
match (source, spantrace) { | ||
(Some(source), None) => { | ||
let italic = self.writer.italic(); | ||
self.record_debug( | ||
field, | ||
&format_args!( | ||
"{} {}{}{}{}", | ||
value, | ||
italic.paint(field.name()), | ||
italic.paint(".sources"), | ||
self.writer.dimmed().paint("="), | ||
ErrorSourceList(source) | ||
), | ||
) | ||
}, | ||
(None, None) => self.record_debug(field, &format_args!("{}", value)), | ||
(Some(source), Some(spantrace)) => { | ||
let italic = self.writer.italic(); | ||
self.record_debug( | ||
field, | ||
&format_args!( | ||
"{} {}{}{}{} {}{}{}{:?}", | ||
value, | ||
italic.paint(field.name()), | ||
italic.paint(".sources"), | ||
self.writer.dimmed().paint("="), | ||
ErrorSourceList(source), | ||
italic.paint(field.name()), | ||
italic.paint(".span_context"), | ||
self.writer.dimmed().paint("="), | ||
spantrace | ||
), | ||
) | ||
}, | ||
(None, Some(spantrace)) => unimplemented!(), | ||
} | ||
} | ||
|
||
|
This file contains 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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this appears to be dead code but doesn't produce a lint????!?