Skip to content

Commit

Permalink
Merge branch 'master' into constant_field_name
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Sep 5, 2023
2 parents a680b4c + 672dfdf commit cb5b499
Show file tree
Hide file tree
Showing 33 changed files with 2,411 additions and 463 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ are not maintained by the `tokio` project. These include:
- [`tracing-logfmt`] provides a layer that formats events and spans into the logfmt format.
- [`tracing-chrome`] provides a layer that exports trace data that can be viewed in `chrome://tracing`.
- [`reqwest-tracing`] provides a middleware to trace [`reqwest`] HTTP requests.
- [`tracing-cloudwatch`] provides a layer that sends events to AWS CloudWatch Logs.
- [`clippy-tracing`] provides a tool to add, remove and check for `tracing::instrument`.

(if you're the maintainer of a `tracing` ecosystem crate not in this list,
please let us know!)
Expand Down Expand Up @@ -445,6 +447,8 @@ please let us know!)
[`tracing-chrome`]: https://crates.io/crates/tracing-chrome
[`reqwest-tracing`]: https://crates.io/crates/reqwest-tracing
[`reqwest`]: https://crates.io/crates/reqwest
[`tracing-cloudwatch`]: https://crates.io/crates/tracing-cloudwatch
[`clippy-tracing`]: https://crates.io/crates/clippy-tracing

**Note:** that some of the ecosystem crates are currently unreleased and
undergoing active development. They may be less stable than `tracing` and
Expand All @@ -460,14 +464,17 @@ Tracing.
* [Diagnostics with Tracing][tokio-blog-2019-08] on the Tokio blog, August 2019
* [Production-Grade Logging in Rust Applications][production-logging-2020], November 2020
* [Custom Logging in Rust using `tracing` and `tracing-subscriber`, part 1][custom-logging-part-1] and [part 2][custom-logging-part-2], October 2021
* [Instrumenting Axum projects][detsys-blog-2023-08], August 2023

[tokio-blog-2019-08]: https://tokio.rs/blog/2019-08-tracing/
[detsys-blog-2023-08]: https://determinate.systems/posts/instrumenting-axum

#### Talks

* [Bay Area Rust Meetup talk and Q&A][bay-rust-2019-03], March 2019
* [RustConf 2019 talk][rust-conf-2019-08-video] and [slides][rust-conf-2019-08-slides], August 2019
* [Are we observable yet? @ RustyDays talk][rusty-days-2020-08-video] and [slides][rusty-days-2020-08-slides], August 2020
* [Crabs with instruments!][tremorcon-2021-09], September 2021

[bay-rust-2019-03]: https://www.youtube.com/watch?v=j_kXRg3zlec
[rust-conf-2019-08-video]: https://www.youtube.com/watch?v=JjItsfqFIdo
Expand All @@ -477,6 +484,7 @@ Tracing.
[production-logging-2020]: https://medium.com/better-programming/production-grade-logging-in-rust-applications-2c7fffd108a6
[custom-logging-part-1]: https://burgers.io/custom-logging-in-rust-using-tracing
[custom-logging-part-2]: https://burgers.io/custom-logging-in-rust-using-tracing-part-2
[tremorcon-2021-09]: https://www.youtube.com/watch?v=ZC7fyqshun8

Help us expand this list! If you've written or spoken about Tracing, or
know of resources that aren't listed, please open a pull request adding them.
Expand Down
1 change: 1 addition & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[build.environment]
RUSTDOCFLAGS="""
-D warnings \
--force-warn rustdoc::redundant-explicit-links \
--force-warn renamed-and-removed-lints \
--cfg docsrs \
--html-before-content /opt/build/repo/assets/warning.html \
Expand Down
26 changes: 20 additions & 6 deletions tracing-appender/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,34 @@
//! - Using a combination of [`NonBlocking`] and [`RollingFileAppender`] to allow writes to a log file
//! without blocking.
//!
//! ## Rolling File Appender
//! ## File Appender
//!
//! The [`rolling` module][rolling] provides functions to create rolling and non-rolling file
//! appenders.
//!
//! Rolling file appender rotation options are [`Rotation::MINUTELY`](rolling::Rotation::MINUTELY),
//! [`Rotation::HOURLY`](rolling::Rotation::HOURLY), and
//! [`Rotation::DAILY`](rolling::Rotation::DAILY).
//!
//! To create a non-rolling file appender, use
//! [`tracing_appender::rolling::never(/*...*/)`](rolling::never) or
//! [`Rotation::NEVER`](rolling::Rotation::NEVER).
//!
//! The following example creates an hourly rotating file appender that writes to
//! `/some/directory/prefix.log.YYYY-MM-DD-HH`:
//!
//! ```rust
//! # fn docs() {
//! let file_appender = tracing_appender::rolling::hourly("/some/directory", "prefix.log");
//! # }
//! ```
//! This creates an hourly rotating file appender that writes to `/some/directory/prefix.log.YYYY-MM-DD-HH`.
//! [`Rotation::DAILY`](rolling::Rotation::DAILY) and [`Rotation::NEVER`](rolling::Rotation::NEVER) are the other available options.
//!
//! The file appender implements [`std::io::Write`][write]. To be used with [`tracing_subscriber::FmtSubscriber`][fmt_subscriber],
//! it must be combined with a [`MakeWriter`][make_writer] implementation to be able to record tracing spans/event.
//! The file appender implements [`std::io::Write`][write]. To be used with
//! [`tracing_subscriber::FmtSubscriber`][fmt_subscriber], it must be combined with a
//! [`MakeWriter`][make_writer] implementation to be able to record tracing spans/event.
//!
//! The [`rolling` module][rolling]'s documentation provides more detail on how to use this file appender.
//! See the [`rolling` module][rolling]'s documentation for more detail on how to use this file
//! appender.
//!
//! ## Non-Blocking Writer
//!
Expand Down
10 changes: 5 additions & 5 deletions tracing-appender/src/rolling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl fmt::Debug for RollingFileAppender {
}
}

/// Creates a minutely, rolling file appender. This will rotate the log file once per minute.
/// Creates a minutely-rotating file appender. This will rotate the log file once per minute.
///
/// The appender returned by `rolling::minutely` can be used with `non_blocking` to create
/// a non-blocking, minutely file appender.
Expand Down Expand Up @@ -299,7 +299,7 @@ pub fn minutely(
RollingFileAppender::new(Rotation::MINUTELY, directory, file_name_prefix)
}

/// Creates an hourly, rolling file appender.
/// Creates an hourly-rotating file appender.
///
/// The appender returned by `rolling::hourly` can be used with `non_blocking` to create
/// a non-blocking, hourly file appender.
Expand Down Expand Up @@ -334,7 +334,7 @@ pub fn hourly(
RollingFileAppender::new(Rotation::HOURLY, directory, file_name_prefix)
}

/// Creates a file appender that rotates daily.
/// Creates a daily-rotating file appender.
///
/// The appender returned by `rolling::daily` can be used with `non_blocking` to create
/// a non-blocking, daily file appender.
Expand Down Expand Up @@ -370,13 +370,13 @@ pub fn daily(
RollingFileAppender::new(Rotation::DAILY, directory, file_name_prefix)
}

/// Creates a non-rolling, file appender
/// Creates a non-rolling file appender.
///
/// The appender returned by `rolling::never` can be used with `non_blocking` to create
/// a non-blocking, non-rotating appender.
///
/// The location of the log file will be specified the `directory` passed in.
/// `file_name` specifies the prefix of the log file. No date or time is appended.
/// `file_name` specifies the complete name of the log file (no date or time is appended).
///
/// # Examples
///
Expand Down
11 changes: 7 additions & 4 deletions tracing-attributes/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ pub(crate) fn gen_function<'a, B: ToTokens + 'a>(
// unreachable, but does affect inference, so it needs to be written
// exactly that way for it to do its magic.
let fake_return_edge = quote_spanned! {return_span=>
#[allow(unreachable_code, clippy::diverging_sub_expression, clippy::let_unit_value, clippy::unreachable)]
#[allow(
unknown_lints, unreachable_code, clippy::diverging_sub_expression,
clippy::let_unit_value, clippy::unreachable, clippy::let_with_type_underscore,
clippy::empty_loop
)]
if false {
let __tracing_attr_fake_return: #return_type =
unreachable!("this is just for type inference, and is unreachable code");
let __tracing_attr_fake_return: #return_type = loop {};
return __tracing_attr_fake_return;
}
};
Expand Down Expand Up @@ -340,7 +343,7 @@ fn gen_block<B: ToTokens>(
// regression in case the level is enabled.
let __tracing_attr_span;
let __tracing_attr_guard;
if tracing::level_enabled!(#level) {
if tracing::level_enabled!(#level) || tracing::if_log_enabled!(#level, {true} else {false}) {
__tracing_attr_span = #span;
#follows_from
__tracing_attr_guard = __tracing_attr_span.enter();
Expand Down
65 changes: 33 additions & 32 deletions tracing-core/src/field.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
//! Span and `Event` key-value data.
//!
//! Spans and events may be annotated with key-value data, referred to as known
//! as _fields_. These fields consist of a mapping from a key (corresponding to
//! a `&str` but represented internally as an array index) to a [`Value`].
//! Spans and events may be annotated with key-value data, known as _fields_.
//! These fields consist of a mapping from a key (corresponding to a `&str` but
//! represented internally as an array index) to a [`Value`].
//!
//! # `Value`s and `Collect`s
//!
//! Collectors consume `Value`s as fields attached to [span]s or [`Event`]s.
//! The set of field keys on a given span or is defined on its [`Metadata`].
//! The set of field keys on a given span or event is defined on its [`Metadata`].
//! When a span is created, it provides [`Attributes`] to the collector's
//! [`new_span`] method, containing any fields whose values were provided when
//! the span was created; and may call the collector's [`record`] method
Expand Down Expand Up @@ -373,6 +373,10 @@ macro_rules! impl_one_value {
impl $crate::sealed::Sealed for $value_ty {}
impl $crate::field::Value for $value_ty {
fn record(&self, key: &$crate::field::Field, visitor: &mut dyn $crate::field::Visit) {
// `op` is always a function; the closure is used because
// sometimes there isn't a real function corresponding to that
// operation. the clippy warning is not that useful here.
#[allow(clippy::redundant_closure_call)]
visitor.$record(key, $op(*self))
}
}
Expand All @@ -388,6 +392,10 @@ macro_rules! impl_one_value {
impl $crate::sealed::Sealed for ty_to_nonzero!($value_ty) {}
impl $crate::field::Value for ty_to_nonzero!($value_ty) {
fn record(&self, key: &$crate::field::Field, visitor: &mut dyn $crate::field::Visit) {
// `op` is always a function; the closure is used because
// sometimes there isn't a real function corresponding to that
// operation. the clippy warning is not that useful here.
#[allow(clippy::redundant_closure_call)]
visitor.$record(key, $op(self.get()))
}
}
Expand Down Expand Up @@ -748,9 +756,6 @@ impl FieldSet {
}

/// Returns a new `ValueSet` with entries for this `FieldSet`'s values.
///
/// Note that a `ValueSet` may not be constructed with arrays of over 32
/// elements.
#[doc(hidden)]
pub fn value_set<'v, V>(&'v self, values: &'v V) -> ValueSet<'v>
where
Expand Down Expand Up @@ -880,6 +885,19 @@ impl<'a> ValueSet<'a> {
}
}

/// Returns the number of fields in this `ValueSet` that would be visited
/// by a given [visitor] to the [`ValueSet::record()`] method.
///
/// [visitor]: Visit
/// [`ValueSet::record()`]: ValueSet::record()
pub fn len(&self) -> usize {
let my_callsite = self.callsite();
self.values
.iter()
.filter(|(field, _)| field.callsite() == my_callsite)
.count()
}

/// Returns `true` if this `ValueSet` contains a value for the given `Field`.
pub(crate) fn contains(&self, field: &Field) -> bool {
field.callsite() == self.callsite()
Expand All @@ -890,7 +908,7 @@ impl<'a> ValueSet<'a> {
}

/// Returns true if this `ValueSet` contains _no_ values.
pub(crate) fn is_empty(&self) -> bool {
pub fn is_empty(&self) -> bool {
let my_callsite = self.callsite();
self.values
.iter()
Expand Down Expand Up @@ -936,37 +954,20 @@ impl<'a> fmt::Display for ValueSet<'a> {
mod private {
use super::*;

/// Marker trait implemented by arrays which are of valid length to
/// construct a `ValueSet`.
///
/// `ValueSet`s may only be constructed from arrays containing 32 or fewer
/// elements, to ensure the array is small enough to always be allocated on the
/// stack. This trait is only implemented by arrays of an appropriate length,
/// ensuring that the correct size arrays are used at compile-time.
/// Restrictions on `ValueSet` lengths were removed in #2508 but this type remains for backwards compatibility.
pub trait ValidLen<'a>: Borrow<[(&'a Field, Option<&'a (dyn Value + 'a)>)]> {}
}

macro_rules! impl_valid_len {
( $( $len:tt ),+ ) => {
$(
impl<'a> private::ValidLen<'a> for
[(&'a Field, Option<&'a (dyn Value + 'a)>); $len] {}
)+
}
}

impl_valid_len! {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32
impl<'a, const N: usize> ValidLen<'a> for [(&'a Field, Option<&'a (dyn Value + 'a)>); N] {}
}

#[cfg(test)]
mod test {
use super::*;
use crate::metadata::{Kind, Level, Metadata};

struct TestCallsite1;
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1;
// Make sure TEST_CALLSITE_* have non-zero size, so they can't be located at the same address.
struct TestCallsite1(u8);
static TEST_CALLSITE_1: TestCallsite1 = TestCallsite1(0);
static TEST_META_1: Metadata<'static> = metadata! {
name: "field_test1",
target: module_path!(),
Expand All @@ -986,8 +987,8 @@ mod test {
}
}

struct TestCallsite2;
static TEST_CALLSITE_2: TestCallsite2 = TestCallsite2;
struct TestCallsite2(u8);
static TEST_CALLSITE_2: TestCallsite2 = TestCallsite2(0);
static TEST_META_2: Metadata<'static> = metadata! {
name: "field_test2",
target: module_path!(),
Expand Down
6 changes: 3 additions & 3 deletions tracing-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ macro_rules! metadata {
$name,
$target,
$level,
Some(file!()),
Some(line!()),
Some(module_path!()),
::core::option::Option::Some(file!()),
::core::option::Option::Some(line!()),
::core::option::Option::Some(module_path!()),
$crate::field::FieldSet::new($fields, $crate::identify_callsite!($callsite)),
$kind,
)
Expand Down
8 changes: 8 additions & 0 deletions tracing-core/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ impl<'a> Record<'a> {
self.values.record(visitor)
}

/// Returns the number of fields that would be visited from this `Record`
/// when [`Record::record()`] is called
///
/// [`Record::record()`]: Record::record()
pub fn len(&self) -> usize {
self.values.len()
}

/// Returns `true` if this `Record` contains a value for the given `Field`.
pub fn contains(&self, field: &field::Field) -> bool {
self.values.contains(field)
Expand Down
6 changes: 3 additions & 3 deletions tracing-flame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl Default for Config {
empty_samples: true,
threads_collapsed: false,
module_path: true,
file_and_line: true,
file_and_line: false,
}
}
}
Expand Down Expand Up @@ -404,7 +404,7 @@ where

if let Some(second) = first.parent() {
for parent in second.scope().from_root() {
stack += "; ";
stack += ";";
write(&mut stack, parent, &self.config)
.expect("expected: write to String never fails");
}
Expand Down Expand Up @@ -446,7 +446,7 @@ where
}

for parent in first.scope().from_root() {
stack += "; ";
stack += ";";
expect!(
write(&mut stack, parent, &self.config),
"expected: write to String never fails"
Expand Down
Loading

0 comments on commit cb5b499

Please sign in to comment.