Skip to content
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

backport several non-breaking changes to v0.1.x #1139

Merged
merged 6 commits into from
Dec 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions-rs/cargo@v1
with:
command: check
args: --all --bins --examples --tests --benches
args: --all --bins --tests --benches

cargo-hack:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -114,7 +114,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, beta, nightly, 1.42.0]
rust: [stable, beta, nightly]
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
Expand All @@ -128,6 +128,32 @@ jobs:
command: test
args: --all

test-msrv:
# Test against the minimum supported Rust version
needs: check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions-rs/toolchain@v1
with:
toolchain: 1.42.0
profile: minimal
override: true
# We can't use `cargo test --all`, as that will also compile the examples.
# We don't guarantee the examples will work on the MSRV, because they have
# external dependencies which may not comply with our MSRV policy, but don't
# actually impact users on our MSRV.
- name: Run tests
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --lib --tests
- name: Run doctests
uses: actions-rs/cargo@v1
with:
command: test
args: --workspace --doc

test-build-wasm:
needs: check
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ attachment that `Future::instrument` does.

[std-future]: https://doc.rust-lang.org/stable/std/future/trait.Future.html
[`tracing-futures`]: https://docs.rs/tracing-futures
[closing]: https://docs.rs/tracing/latest/span/index.html#closing-spans
[closing]: https://docs.rs/tracing/latest/tracing/span/index.html#closing-spans
[`Future::instrument`]: https://docs.rs/tracing/latest/tracing/trait.Instrument.html#method.instrument
[`#[instrument]`]: https://docs.rs/tracing/0.1.11/tracing/attr.instrument.html

Expand Down
9 changes: 3 additions & 6 deletions netlify.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
[build]
command = """
curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly --profile minimal \
&& source $HOME/.cargo/env \
&& RUSTDOCFLAGS=\"--cfg docsrs\" cargo +nightly doc --no-deps
"""
publish = "target/doc"
command = "rustup install nightly --profile minimal && cargo doc --no-deps && cp -r target/doc _netlify_out"
environment = { RUSTDOCFLAGS= "--cfg docsrs" }
publish = "_netlify_out"

[[redirects]]
from = "/"
Expand Down
20 changes: 16 additions & 4 deletions tracing-appender/src/non_blocking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub const DEFAULT_BUFFERED_LINES_LIMIT: usize = 128_000;
pub struct WorkerGuard {
guard: Option<JoinHandle<()>>,
sender: Sender<Msg>,
shutdown: Sender<()>,
}

/// A non-blocking writer.
Expand Down Expand Up @@ -148,8 +149,11 @@ impl NonBlocking {
) -> (NonBlocking, WorkerGuard) {
let (sender, receiver) = bounded(buffered_lines_limit);

let worker = Worker::new(receiver, writer);
let worker_guard = WorkerGuard::new(worker.worker_thread(), sender.clone());
let (shutdown_sender, shutdown_receiver) = bounded(0);

let worker = Worker::new(receiver, writer, shutdown_receiver);
let worker_guard =
WorkerGuard::new(worker.worker_thread(), sender.clone(), shutdown_sender);

(
Self {
Expand Down Expand Up @@ -245,10 +249,11 @@ impl MakeWriter for NonBlocking {
}

impl WorkerGuard {
fn new(handle: JoinHandle<()>, sender: Sender<Msg>) -> Self {
fn new(handle: JoinHandle<()>, sender: Sender<Msg>, shutdown: Sender<()>) -> Self {
WorkerGuard {
guard: Some(handle),
sender,
shutdown,
}
}
}
Expand All @@ -259,7 +264,14 @@ impl Drop for WorkerGuard {
.sender
.send_timeout(Msg::Shutdown, Duration::from_millis(100))
{
Ok(_) | Err(SendTimeoutError::Disconnected(_)) => (),
Ok(_) => {
// Attempt to wait for `Worker` to flush all messages before dropping. This happens
// when the `Worker` calls `recv()` on a zero-capacity channel. Use `send_timeout`
// so that drop is not blocked indefinitely.
// TODO: Make timeout configurable.
let _ = self.shutdown.send_timeout((), Duration::from_millis(1000));
}
Err(SendTimeoutError::Disconnected(_)) => (),
Err(SendTimeoutError::Timeout(e)) => println!(
"Failed to send shutdown signal to logging worker. Error: {:?}",
e
Expand Down
14 changes: 11 additions & 3 deletions tracing-appender/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{io, thread};
pub(crate) struct Worker<T: Write + Send + Sync + 'static> {
writer: T,
receiver: Receiver<Msg>,
shutdown: Receiver<()>,
}

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
Expand All @@ -18,8 +19,12 @@ pub(crate) enum WorkerState {
}

impl<T: Write + Send + Sync + 'static> Worker<T> {
pub(crate) fn new(receiver: Receiver<Msg>, writer: T) -> Worker<T> {
Self { writer, receiver }
pub(crate) fn new(receiver: Receiver<Msg>, writer: T, shutdown: Receiver<()>) -> Worker<T> {
Self {
writer,
receiver,
shutdown,
}
}

fn handle_recv(&mut self, result: &Result<Msg, RecvError>) -> io::Result<WorkerState> {
Expand Down Expand Up @@ -67,7 +72,10 @@ impl<T: Write + Send + Sync + 'static> Worker<T> {
loop {
match self.work() {
Ok(WorkerState::Continue) | Ok(WorkerState::Empty) => {}
Ok(WorkerState::Shutdown) | Ok(WorkerState::Disconnected) => break,
Ok(WorkerState::Shutdown) | Ok(WorkerState::Disconnected) => {
let _ = self.shutdown.recv();
break;
}
Err(_) => {
// TODO: Expose a metric for IO Errors, or print to stderr
}
Expand Down
46 changes: 35 additions & 11 deletions tracing-flame/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,21 @@ struct Config {

/// Don't include thread_id
threads_collapsed: bool,

/// Don't display module_path
module_path: bool,

/// Don't display file and line
file_and_line: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
empty_samples: true,
threads_collapsed: false,
module_path: true,
file_and_line: true,
}
}
}
Expand Down Expand Up @@ -311,6 +319,18 @@ where
self.config.threads_collapsed = enabled;
self
}

/// Configures whether or not module paths should be included in the output.
pub fn with_module_path(mut self, enabled: bool) -> Self {
self.config.module_path = enabled;
self
}

/// Configures whether or not file and line should be included in the output.
pub fn with_file_and_line(mut self, enabled: bool) -> Self {
self.config.file_and_line = enabled;
self
}
}

impl<W> FlushGuard<W>
Expand Down Expand Up @@ -394,7 +414,7 @@ where

for parent in parents {
stack += "; ";
write(&mut stack, parent).expect("expected: write to String never fails");
write(&mut stack, parent, &self.config).expect("expected: write to String never fails");
}

write!(&mut stack, " {}", samples.as_nanos())
Expand Down Expand Up @@ -436,14 +456,14 @@ where

for parent in parents {
expect!(
write(&mut stack, parent),
write(&mut stack, parent, &self.config),
"expected: write to String never fails"
);
stack += "; ";
}

expect!(
write(&mut stack, first),
write(&mut stack, first, &self.config),
"expected: write to String never fails"
);
expect!(
Expand Down Expand Up @@ -473,22 +493,26 @@ where
}
}

fn write<S>(dest: &mut String, span: SpanRef<'_, S>) -> fmt::Result
fn write<S>(dest: &mut String, span: SpanRef<'_, S>, config: &Config) -> fmt::Result
where
S: Subscriber + for<'span> LookupSpan<'span>,
{
if let Some(module_path) = span.metadata().module_path() {
write!(dest, "{}::", module_path)?;
if config.module_path {
if let Some(module_path) = span.metadata().module_path() {
write!(dest, "{}::", module_path)?;
}
}

write!(dest, "{}", span.name())?;

if let Some(file) = span.metadata().file() {
write!(dest, ":{}", file)?;
}
if config.file_and_line {
if let Some(file) = span.metadata().file() {
write!(dest, ":{}", file)?;
}

if let Some(line) = span.metadata().line() {
write!(dest, ":{}", line)?;
if let Some(line) = span.metadata().line() {
write!(dest, ":{}", line)?;
}
}

Ok(())
Expand Down
67 changes: 65 additions & 2 deletions tracing-subscriber/src/filter/env/directive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,17 @@ impl FromStr for Directive {
lazy_static! {
static ref DIRECTIVE_RE: Regex = Regex::new(
r"(?x)
^(?P<global_level>trace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])$ |
^(?P<global_level>(?i:trace|debug|info|warn|error|off|[0-5]))$ |
# ^^^.
# `note: we match log level names case-insensitively
^
(?: # target name or span name
(?P<target>[\w:-]+)|(?P<span>\[[^\]]*\])
){1,2}
(?: # level or nothing
=(?P<level>trace|TRACE|debug|DEBUG|info|INFO|warn|WARN|error|ERROR|off|OFF|[0-5])?
=(?P<level>(?i:trace|debug|info|warn|error|off|[0-5]))?
# ^^^.
# `note: we match log level names case-insensitively
)?
$
"
Expand Down Expand Up @@ -831,6 +835,32 @@ mod test {
assert_eq!(dirs[1].in_span, None);
}

#[test]
fn parse_directives_ralith_uc() {
let dirs = parse_directives("common=INFO,server=DEBUG");
assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs);
assert_eq!(dirs[0].target, Some("common".to_string()));
assert_eq!(dirs[0].level, LevelFilter::INFO);
assert_eq!(dirs[0].in_span, None);

assert_eq!(dirs[1].target, Some("server".to_string()));
assert_eq!(dirs[1].level, LevelFilter::DEBUG);
assert_eq!(dirs[1].in_span, None);
}

#[test]
fn parse_directives_ralith_mixed() {
let dirs = parse_directives("common=iNfo,server=dEbUg");
assert_eq!(dirs.len(), 2, "\nparsed: {:#?}", dirs);
assert_eq!(dirs[0].target, Some("common".to_string()));
assert_eq!(dirs[0].level, LevelFilter::INFO);
assert_eq!(dirs[0].in_span, None);

assert_eq!(dirs[1].target, Some("server".to_string()));
assert_eq!(dirs[1].level, LevelFilter::DEBUG);
assert_eq!(dirs[1].in_span, None);
}

#[test]
fn parse_directives_valid() {
let dirs = parse_directives("crate1::mod1=error,crate1::mod2,crate2=debug,crate3=off");
Expand Down Expand Up @@ -1003,6 +1033,39 @@ mod test {
assert_eq!(dirs[1].in_span, None);
}

// helper function for tests below
fn test_parse_bare_level(directive_to_test: &str, level_expected: LevelFilter) {
let dirs = parse_directives(directive_to_test);
assert_eq!(
dirs.len(),
1,
"\ninput: \"{}\"; parsed: {:#?}",
directive_to_test,
dirs
);
assert_eq!(dirs[0].target, None);
assert_eq!(dirs[0].level, level_expected);
assert_eq!(dirs[0].in_span, None);
}

#[test]
fn parse_directives_global_bare_warn_lc() {
// test parse_directives with no crate, in isolation, all lowercase
test_parse_bare_level("warn", LevelFilter::WARN);
}

#[test]
fn parse_directives_global_bare_warn_uc() {
// test parse_directives with no crate, in isolation, all uppercase
test_parse_bare_level("WARN", LevelFilter::WARN);
}

#[test]
fn parse_directives_global_bare_warn_mixed() {
// test parse_directives with no crate, in isolation, mixed case
test_parse_bare_level("wArN", LevelFilter::WARN);
}

#[test]
fn parse_directives_valid_with_spans() {
let dirs = parse_directives("crate1::mod1[foo]=error,crate1::mod2[bar],crate2[baz]=debug");
Expand Down