-
Notifications
You must be signed in to change notification settings - Fork 43
tracing-tracy: replace baked-in config fields with a Config
trait
#91
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
162bc2a
tracing-tracy: replace baked-in config fields with a `Config` trait
nagisa 53818e0
config: enable customization of behaviour upon an error condition
nagisa 93fd449
doc: use `TracyLayer::default` where no customization of config occurs
nagisa fa4e1d0
config: perhaps drop DynamicConfig too?
nagisa a9e4c24
pass in metadata for stack depth config
nagisa 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use client::Client; | ||
use tracing_subscriber::fmt::format::DefaultFields; | ||
use tracing_subscriber::fmt::FormatFields; | ||
|
||
/// Configuration of the [`TracyLayer`](super::TracyLayer) behaviour. | ||
/// | ||
/// For most users [`DefaultConfig`] is going to be a good default choice, however advanced users | ||
/// can implement this trait manually to override the formatter used or to otherwise modify the | ||
/// behaviour of the `TracyLayer`. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ``` | ||
/// use tracing_subscriber::fmt::format::DefaultFields; | ||
/// | ||
/// struct TracyLayerConfig { | ||
/// fmt: DefaultFields, | ||
/// } | ||
/// impl tracing_tracy::Config for TracyLayerConfig { | ||
/// type Formatter = DefaultFields; | ||
/// fn formatter(&self) -> &Self::Formatter { | ||
/// &self.fmt | ||
/// } | ||
/// // The boilerplate ends here | ||
/// | ||
/// /// Collect 32 frames in stack traces. | ||
/// fn stack_depth(&self, _: &tracing::Metadata) -> u16 { | ||
/// 32 | ||
/// } | ||
/// | ||
/// /// Do not format fields into zone names. | ||
/// fn format_fields_in_zone_name(&self) -> bool { | ||
/// false | ||
/// } | ||
/// | ||
/// // etc. | ||
/// } | ||
/// ``` | ||
/// | ||
/// With this configuration `TracyLayer` will collect some call stacks and the formatting of the | ||
/// zone names is different from the `DefaultConfig`. | ||
pub trait Config { | ||
type Formatter: for<'writer> FormatFields<'writer> + 'static; | ||
|
||
/// Use a custom field formatting implementation. | ||
fn formatter(&self) -> &Self::Formatter; | ||
|
||
/// Specify the maximum number of stack frames that will be collected. | ||
/// | ||
/// Note that enabling callstack collection can and will introduce a non-trivial overhead at | ||
/// every instrumentation point. Specifying 0 frames will disable stack trace collection. | ||
/// | ||
/// Default implementation returns `0`. | ||
fn stack_depth(&self, metadata: &tracing_core::Metadata<'_>) -> u16 { | ||
let _ = metadata; | ||
0 | ||
} | ||
|
||
/// Specify whether or not to include tracing span fields in the tracy zone name, or to emit | ||
/// them as zone text. | ||
/// | ||
/// The former enables zone analysis along unique span field invocations, while the latter | ||
/// aggregates every invocation of a given span into a single zone, irrespective of field | ||
/// values. | ||
/// | ||
/// Default implementation returns `true`. | ||
fn format_fields_in_zone_name(&self) -> bool { | ||
true | ||
} | ||
|
||
/// Apply handling for errors detected by the [`TracyLayer`](super::TracyLayer). | ||
/// | ||
/// Fundamentally the way the tracing crate and the Tracy profiler work are somewhat | ||
/// incompatible in certain ways. For instance, a [`tracing::Span`] can be created on one | ||
/// thread and moved to another, where it is cleaned up. Tracy on the other hand expects that | ||
/// its eqvivalent concept of zone remains entirely within a thread. | ||
/// | ||
/// Another example a limitation in `Tracy` where the message length or zone name cannot exceed | ||
/// a certain (low) limit of bytes. | ||
/// | ||
/// Although `tracing_tracy` does it best to paper over these sorts of differences, it can’t | ||
/// always make them invisible. In certain cases detecting these sorts of issues is | ||
/// straightforward, and it is when `tracing_tracy` will invoke this method to enable users to | ||
/// report the issues in whatever way they wish to. | ||
/// | ||
/// By default a message coloured in red is emitted to the tracy client. | ||
fn on_error(&self, client: &Client, error: &'static str) { | ||
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. Ooooooh, this is quite a nice flexibility improvement. |
||
client.color_message(error, 0xFF000000, 0); | ||
} | ||
} | ||
|
||
/// A default configuration of the [`TracyLayer`](super::TracyLayer). | ||
/// | ||
/// This type does not allow for any adjustment of the configuration. In order to customize | ||
/// the behaviour of the layer implement the [`Config`] trait for your own type. | ||
#[derive(Default)] | ||
pub struct DefaultConfig(DefaultFields); | ||
|
||
impl Config for DefaultConfig { | ||
type Formatter = DefaultFields; | ||
fn formatter(&self) -> &Self::Formatter { | ||
&self.0 | ||
} | ||
} |
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
Oops, something went wrong.
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.
I wish there was a way to specify a default here. Sadly not much progress: https://rust-lang.github.io/rfcs/2532-associated-type-defaults.html
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.
I wish it was possible too, however even with ATDs it wouldn’t be possible as the method returns a reference to
&self
and traits do not have access to fields. Achieving this would need something like rust-lang/rfcs#1546.