Commit Graph

888 Commits

Author SHA1 Message Date
Eliza Weisman
f12d88a9ae [wip] draft of field skipping utils 2020-11-23 11:28:39 -08:00
Eliza Weisman
b1baa6c2ef subscriber: add lifetime parameter to MakeWriter (#781)
## Motivation

Currently, the `tracing-subscriber` crate has the `MakeWriter` trait for
customizing the io writer used by `fmt`. This trait is necessary (rather
than simply using a `Write` instance) because the default implementation
performs the IO on the thread where an event was recorded, meaning that
a separate writer needs to be acquired by each thread (either by calling
a function like `io::stdout`, by locking a shared `Write` instance,
etc).

Right now there is a blanket impl for `Fn() -> T where T: Write`. This
works fine with functions like `io::stdout`. However, the _other_ common
case for this trait is locking a shared writer.

Therefore, it makes sense to see an implementation like this:

``` rust
impl<'a, W: io::Write> MakeWriter for Mutex<W>
where
    W: io::Write,
{
    type Writer = MutexWriter<'a, W>;
    fn make_writer(&self) -> Self::Writer {
        MutexWriter(self.lock().unwrap())
    }
}

pub struct MutexWriter<'a, W>(MutexGuard<'a, W>);

impl<W: io::Write> io::Write for MutexWriter<'_, W> {
    // write to the shared writer in the `MutexGuard`...
}
```

Unfortunately, it's impossible to write this. Since `MakeWriter` always
takes an `&self` parameter and returns `Self::Writer`, the generic
parameter is unbounded:
```
    Checking tracing-subscriber v0.2.4 (/home/eliza/code/tracing/tracing-subscriber)
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
  --> tracing-subscriber/src/fmt/writer.rs:61:6
   |
61 | impl<'a, W: io::Write> MakeWriter for Mutex<W>
   |      ^^ unconstrained lifetime parameter

error: aborting due to previous error
```

This essentially precludes any `MakeWriter` impl where the writer is
borrowed from the type implementing `MakeWriter`. This is a significant
blow to the usefulness of the trait. For example, it prevented the use
of `MakeWriter` in `tracing-flame` as suggested in
https://github.com/tokio-rs/tracing/pull/631#discussion_r391138233.

## Proposal

This PR changes `MakeWriter` to be generic over a lifetime `'a`:

```rust
pub trait MakeWriter<'a> {
    type Writer: io::Write;

    fn make_writer(&'a self) -> Self::Writer;
}
```
The `self` parameter is now borrowed for the `&'a` lifetime, so it is
okay to return a writer borrowed from `self`, such as in the `Mutex`
case.

I've also added an impl of `MakeWriter` for `Mutex<T> where T: Writer`.

Unfortunately, this is a breaking change and will need to wait until we
release `tracing-subscriber` 0.3.

Fixes #675.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-11-21 10:42:25 -08:00
Ecky Putrady
e0fb974a86 docs: add a blogpost link in External Resources section (#1113)
The blogpost is about the usage of tracing and some of its complementary modules.

Related [Reddit comment]( https://www.reddit.com/r/rust/comments/jv6a5j/productiongrade_logging_in_rust_applications/gcrjoa6?utm_source=share&utm_medium=web2x&context=3).
2020-11-21 09:55:06 -08:00
David Barsky
48c415fcef examples: add fmt-multiple-writers.rs (#1106)
This PR:

* Adds an example demonstrating how `fmt::Subscriber` can be used to write to stdout and a file simultaneously.
* Removes a few mentions of "Layers" in the examples directory; opting for "Subscribers" instead.

Resolves #1091
2020-11-21 09:52:40 -08:00
Alan D. Salewski
b3c1e0eb8d subscriber: rename trait CollectorExt to CollectExt (#1115)
The trait being augmented is the 'Collect' trait, so name the trait
that extends should be called 'CollectExt'.

This is in keeping with the extension trait naming conventions
documented in RFC 445 and the spirit of the discussion in
tokio-rs/tracing PR #1015.
2020-11-20 21:11:08 -05:00
Joshua Nelson
1f3e095a5d docs: Remove deny(broken_intra_doc_links) (#1114)
As of Rust 1.48, these are stable and the warnings are enabled by default.
2020-11-19 15:02:13 -08:00
Joshua Nelson
9e07ff8143 docs: Switch to even more intra-doc links (#1084) 2020-11-19 10:53:20 -05:00
dependabot-preview[bot]
e7f57e49fe chore(deps): update parking_lot requirement from >= 0.7, <= 0.11 to >= 0.7, <= 0.12 (#1111)
Updates the requirements on [parking_lot](https://github.com/Amanieu/parking_lot) to permit the latest version.
- [Release notes](https://github.com/Amanieu/parking_lot/releases)
- [Changelog](https://github.com/Amanieu/parking_lot/blob/master/CHANGELOG.md)
- [Commits](https://github.com/Amanieu/parking_lot/compare/0.7.0...0.11.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-11-18 09:34:49 -08:00
Naja Melan
8e5d9b9c7c subscriber: Remove trailing space from ChronoLocal time formatter. (#1103)
I reckon this is a bug, and @samschlegel fixed this for ChronoUtc,
but not for ChronoLocal.
2020-11-17 14:44:24 -08:00
Alan D. Salewski
ebab36924e docs: fix minor typo for DEFAULT_BUFFERED_LINES_LIMIT (#1109) 2020-11-17 11:43:35 -05:00
Alan D. Salewski
783bdd16eb docs: add missing example for Rotation::MINUTELY (#1110) 2020-11-17 11:42:53 -05:00
David Barsky
0d661b6160 examples: fix echo example by reintroducing concurrency (#1107) 2020-11-17 11:29:51 -05:00
dependabot-preview[bot]
103689a22c chore(deps): update pin-project-lite requirement from 0.1 to 0.2 (#1108)
Updates the requirements on [pin-project-lite](https://github.com/taiki-e/pin-project-lite) to permit the latest version.
- [Release notes](https://github.com/taiki-e/pin-project-lite/releases)
- [Changelog](https://github.com/taiki-e/pin-project-lite/blob/master/CHANGELOG.md)
- [Commits](https://github.com/taiki-e/pin-project-lite/compare/v0.1.0...v0.2.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-11-16 12:56:14 -08:00
Julian Tescher
6b272c6c4e opentelemetry: prepare for 0.9.0 release (#1104) 2020-11-15 09:29:26 -08:00
Maxime Bedard
c142c406f2 Add busy/idle timings to the opentelemetry subscriber (#1096) 2020-11-13 19:06:40 -08:00
Taiki Endo
fa105b6eca ci: install cargo-hack from GitHub release instead of using cache (#1100)
## Motivation

Closes #648

## Solution

Install cargo-hack from GitHub release instead of using cache.
See also https://github.com/taiki-e/cargo-hack/pull/89 and 
https://github.com/taiki-e/cargo-hack/pull/91.
2020-11-13 14:31:18 -08:00
xd009642
3ea53c6d79 subscriber: make with_ansi docs consistent (#1098) 2020-11-13 14:30:33 -08:00
Julian Tescher
4f7356100f opentelemetry: update to latest otel release version (#1049)
## Motivation

Support the latest OpenTelemetry specification

## Solution

In order to support the latest spec, this patch makes the following
breaking API changes:

* Update `opentelemetry` to 0.10.x
* Update `CompatSpan` to reflect changes to `Span` trait
* Record `u64` values as strings as they are no longer supported in 
  OpenTelemetry.

Additionally the following non-public api, doc, and example changes:

* Update examples and docs to use new simplified pipeline builder.
* As `opentelemetry::api` no longer exports trace types, internally use
`opentelemetry::trace as otel` to disambiguate from tracing types.
* Remove `rand` dependency as it is no longer needed

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
2020-11-12 14:20:35 -08:00
David
8e5101fc3b subscriber: use default values for all Format type params (#1087)
`format::Format` already has default values for the log format (`Full`)
as well as time measuring gizmo (`SystemTime`) so no need to plug those
in.
2020-11-12 12:47:54 -08:00
nasa
710228e78f chore: Use cfg_feature! for Module Declarations in Registry (#1074) 2020-11-09 16:39:12 -05:00
Jane Lusby
ef1d940bfb Add support for converting errors wrapped by TracedError (#1055)
## Motivation

Right now TracedError is pretty much incompatible with enum heavy error
handling implementations. The way its currently designed you'd have to make
sure that each leaf error independently is wrapped in a `TracedError` so that
all sources end up capturing a SpanTrace.

## Solution

To resolve this I've added a `map` method for transforming the inner type of a
TracedError while preserving the existing SpanTrace. This is still a bit
cumbersome, where before you'd be able to write `result.map_err(|source|
MyError { source, some_context })` now you'll have to write
`result.map_err(|source| source.map(|source| MyError { source, some_context
}))`. To fix this we should probably eventually add an extension trait on
`Result` but I want to experiment with that half of the change in `zebra`
before committing to an API, since it can easily be done out of tree.

Co-authored-by: Eliza Weisman <eliza@buoyant.io>
2020-11-05 16:42:20 -08:00
Eliza Weisman
8070a851ac subscriber: fix accessing the field formatter from FmtContext (#1081)
## Motivation

Currently, the `FmtContext` type implements `FormatFields` using the
subscriber's field formatter. However, this is difficult to use. The
`FmtContext` may _not_ be passed to functions expecting a `for<'writer>
FormatFields<'writer>`, because it only implements `FormatFields` for
its _own_ lifetime. This means the writer must have the same lifetime as
the context, which is not correct.

## Solution

This branch fixes this by changing the impl of `FormatFields` for
`FmtContext` to be generic over both the context's lifetime _and_ the
field formatter's lifetime. Additionally, I've added a method for
borrowing the actual field formatter as its concrete type, in case the
`FormatEvent` impl puts additional constraints on its type or is only
implemented for a specific named type, and wants to actually _use_ that
type.
2020-11-02 13:17:31 -08:00
Joshua Nelson
a0e1daa7be docs: Convert even more intra-doc links (#1077)
## Motivation

#940

## Solution

This changes the docs.rs links, many of which were broken (or would have
been broken on the next release). It also converts some links I missed
the first time (maybe they were added after I opened the PR?).
2020-11-02 13:01:07 -08:00
Joshua Nelson
879b350708 docs: Convert _all_ the intra-doc links (#1022)
## Motivation

Closes https://github.com/tokio-rs/tracing/issues/940.

## Solution

Uses ~~a heavily patched version~~ the latest master version of
https://github.com/poliorcetics/cargo-intraconv/ to automatically
convert the links. Fixes remaining issues by hand.
2020-11-02 09:47:49 -08:00
David
eadf2a23da chore: Fix several renames missed in #1015 (#1066)
The changes were in tests, tracing-error, and example.
2020-10-27 13:46:32 -04:00
Eliza Weisman
70d55afba0 subscriber: make Compact formatter not be useless (#1069)
Depends on #1067 

## Motivation

Currently, the `Compact` event formatter in `tracing-subscriber`
is...kind of bad, and nobody seems to use it. It outputs the names of
all the spans in the current context, and (inexplicably) the _names_ of
all the fields on the current span, but not their values. This isn't
very useful.

## Solution

This branch rewrites the `Compact` formatter to be a little less bad.
Now, we output all the fields from the current span context, but skip
span names, and we shorten the level to a single character when it's
enabled. Additionally, using the `compact` formatter disables targets by
default. Now, the lines are nicely short, but maybe still useful.

Example output before this change:
![Screenshot_20201024_122857](https://user-images.githubusercontent.com/2796466/97093422-a2ad2780-1600-11eb-94bf-ee13011a3e73.png)
...and after:
![Screenshot_20201024_133352](https://user-images.githubusercontent.com/2796466/97093410-8e692a80-1600-11eb-9b42-1aefa1725957.png)

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-10-26 14:05:29 -07:00
Eliza Weisman
bd8c6da0ce subscriber: add Pretty formatter (#1067)
## Motivation

Currently, the `tracing_subscriber::fmt` module contains only
single-line event formatters. Some users have requested a
human-readable, pretty-printing event formatter optimized for
aesthetics.

## Solution

This branch adds a new `Pretty` formatter which uses an _excessively_
pretty output format. It's neither compact, single-line oriented, nor
easily machine-readable, but it looks _quite_ nice, in my opinion. This
is well suited for local development or potentially for user-facing logs
in a CLI application.

Additionally, I tried to improve the docs for the different formatters
currently provided, including example output. Check out [the Netlify
preview][1]!

[1]: https://deploy-preview-1067--tracing-rs.netlify.app/tracing_subscriber/fmt/index.html#formatters

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-10-26 10:11:59 -07:00
Eliza Weisman
1bf9da2bc7 subscriber: update sharded-slab to 0.1, pool hashmap allocations (#1062)
## Motivation

hawkw/sharded-slab#45 changes `sharded-slab` so that the per-shard
metadata is allocated only when a new shard is created, rather than all
up front when the slab is created. This fixes the very large amount of
memory allocated by simply creating a new `Registry` without actually
collecting any traces.

## Solution

This branch updates `tracing-subscriber` to depend on `sharded-slab`
0.1.0, which includes the upstream fix.

In addition, this branch the registry from using `sharded_slab::Slab` to
`sharded_slab::Pool`. This allows us to clear hashmap allocations for
extensions in-place, retaining the already allocated maps. This should
improve `new_span` performance a bit.

Fixes #1005
2020-10-22 15:10:00 -07:00
David Barsky
eeb6974cab subscriber: remove deprecated type, structs, and methods #1030
This PR removes the deprecated items in `tracing-subscriber`. It _does
not_ remove `Collect::drop_span` as that will happen in a separate PR.
Note that branch builds atop of #1015, which means:

- #1015 needs to be merged first
- This PR will be rebased against the master branch containing #1015.
- _Then_ this can safely be merged.
2020-10-22 14:04:19 -07:00
David Barsky
98ba44c22e core: rename Subscriber to Collect (#1015)
This PR renames the following:

- `tracing_core::Subscriber` to `tracing_core::Collect`
- `tracing_subscriber::layer::Layer` to `tracing_subscriber::layer::Subscribe`

Authored-by: David Barsksy <dbarsky@amazon.com>
Co-authored-by: Eliza Weisman <eliza@buoyant.io>
2020-10-22 15:11:23 -04:00
Veetaha
b7b14df219 chore: fix rustdoc warning in tracing-subscriber (#1061) 2020-10-22 13:45:26 -04:00
David
0dc8ef2da9 subscriber: remove TraceLogger (#1052)
* Remove TraceLogger

The `TraceLogger` implementation is deprecated and this PR removes it
completely.
2020-10-21 16:55:42 -07:00
Eliza Weisman
67340e911f subscriber: make Registry::enter/exit much faster (#1058)
## Motivation

We've tried very hard to make sure that entering and exiting spans is
lightweight...in the `tracing-core` core machinery. Unfortunately, we
haven't done any benchmarking of how subscriber implementations actually
handle enter/exit events. It turns out that in `tracing-subscriber`'s
`Registry`, there's actually significant overhead for entering a span:
calling `span.enter()` may take as long as 69 ns (on my machine).

## Solution

I've written some microbenchmarks for entering and exiting enabled spans
using `tracing-subscriber::fmt`, comparing them with the overhead of
calling `enter` on an enabled span. Based on this, I've made some
performance improvements. These optimizations include:

- Removing the `HashSet` that's used for tracking previously entered
  span IDs, in favor of linear search. Span stacks probably never deep
  enough for a hashmap to be faster than iterating over a couple of
  vec indices.
- Preallocating the vec used for the span stack to hold at least 64
  elements. This means we'll never have a lag spike from reallocating,
  as I think it'll almost never be deeper than 64 IDs.
- Only cloning/dropping an ID's ref count for the _first_ ID in the stack.

This makes entering and exiting enabled spans significantly faster:
![image](https://user-images.githubusercontent.com/2796466/96798681-3fc85000-13b6-11eb-9e85-7602d918ee09.png)

It would be nice to continue optimizing this, but this might be about
the best it gets given the other requirements that we're now making
assertions about.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-10-21 16:52:25 -07:00
dependabot-preview[bot]
499d3da19c chore(deps): update env_logger requirement from 0.7 to 0.8 (#1050)
Updates the requirements on [env_logger](https://github.com/env-logger-rs/env_logger) to permit the latest version.
- [Release notes](https://github.com/env-logger-rs/env_logger/releases)
- [Changelog](https://github.com/env-logger-rs/env_logger/blob/master/CHANGELOG.md)
- [Commits](https://github.com/env-logger-rs/env_logger/compare/v0.7.0...v0.8.1)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-10-19 15:46:18 -07:00
David
19af2924f1 chore: fix tracing-macros::dbg (#1054)
…and the `factorial` example.
2020-10-19 14:39:57 -07:00
dependabot-preview[bot]
cd911e069e chore(deps): update pin-project requirement from 0.4 to 1.0 (#1038)
Updates the requirements on [pin-project](https://github.com/taiki-e/pin-project) to permit the latest version.
- [Release notes](https://github.com/taiki-e/pin-project/releases)
- [Changelog](https://github.com/taiki-e/pin-project/blob/master/CHANGELOG.md)
- [Commits](https://github.com/taiki-e/pin-project/compare/v0.4.0...v1.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-10-18 13:57:44 -07:00
Guilherme Souza
b5b26beb33 chore: remove duplicated section from tracing/README.md (#1046)
In `tracing/README.md`, the section  "Supported Rust Versions" was 
presented twice with the same content.
2020-10-17 15:52:34 -04:00
Julian Tescher
549c919d0f opentelemetry: prepare for 0.8.0 release (#1036) 2020-10-13 16:33:21 -07:00
xd009642
f841b579cb docs: add favicon for extra pretty docs (#1033)
A PR to make the docs extra swish via a favicon for the browser tab!

Before 

![image](https://user-images.githubusercontent.com/3472518/95902628-f7dd7380-0d8c-11eb-855a-f6530103917d.png)

After

![image](https://user-images.githubusercontent.com/3472518/95902709-0f1c6100-0d8d-11eb-85f7-49cdf36473ac.png)
2020-10-13 16:02:09 -07:00
Eliza Weisman
83760225fa subscriber: fix reload ergonomics (#1035)
## Motivation

Currently, the `reload` layer is generic over both the type of the layer
being reloaded, *and* the type of the subscriber that the layer is
layered onto. This means that the `reload::Handle` type that's used to
reload the value of the layer *also* is parameterized over the
subscriber's type.

The subscriber type parameter makes the `reload` API significantly
harder to use. Any time a `reload::Handle` is returned by a function,
taken as an argument, or stored in a struct, the full type of the
subscriber under the layer must be written out --- and often, it is
quite long. What makes this worse is that sometimes the type of the
subscriber may vary at runtime based on some configuration, while the
type of the layer that's reloaded remains the same. For example, in
Linkerd, we've had to do [this][1], which is really not ideal.

## Solution

This branch removes the `Subscriber` type parameter from `reload::Layer`
and `reload::Handle`. Now, the `Handle` type is only generic over the
type of the inner layer that's being reloaded. It turns out that the
`Subscriber` type parameter was only necessary to add a `L: Layer<S>`
bound to `reload::Layer`'s constructor, which isn't really necessary ---
if the layer does not implement `Layer<S>`, the type error will occur
when `Subscriber::with` is actually used to layer it, which is fine.

I also changed the `with_filter_reloading` option on the `FmtSubscriber`
builder to also work with `LevelFilter`s, since there's no reason for it
not to, and added an example.

Since this breaks existing code, this change has to be made as part of
0.3.

[1]: 6c484f6dcd/linkerd/app/core/src/trace.rs (L19-L36)
2020-10-13 14:47:49 -07:00
dependabot-preview[bot]
aef5bd83f8 chore(deps): update crossbeam-channel requirement from 0.4.2 to 0.5.0 (#1031)
Updates the requirements on [crossbeam-channel](https://github.com/crossbeam-rs/crossbeam) to permit the latest version.
- [Release notes](https://github.com/crossbeam-rs/crossbeam/releases)
- [Changelog](https://github.com/crossbeam-rs/crossbeam/blob/master/CHANGELOG.md)
- [Commits](https://github.com/crossbeam-rs/crossbeam/compare/crossbeam-channel-0.4.3...crossbeam-channel-0.5.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-10-12 09:35:17 -07:00
Julian Tescher
082ce998c4 opentelemetry: Assign default ids if missing (#1027)
## Motivation

It is currently possible to create a span graph which includes a span
that has both an invalid parent otel context _and_ a missing trace id by
assigning an invalid extracted parent context to a non-root span.
Constructing this particular graph will currently cause a panic.

## Solution

Explicitly assign invalid trace / span ids when sampling using the otel
SDK if the span builder does not contain these values.
2020-10-09 17:09:21 -07:00
Darin Morrison
288f71da24 chore: remove deprecated add-path from CI (#1026)
## Motivation

Use of `echo "::add-path::<PATH>"` has been deprecated on GitHub CI.

## Solution

Update CI to use new path setter `echo "<PATH>" >> $GITHUB_PATH`.

Co-authored-by: Darin Morrison <darinmorrison@users.noreply.github.com>
2020-10-08 17:36:29 -07:00
Felix Wiedemann
4e87df5484 attributes: fix #[instrument(err)] in case of early returns (#1006)
## Motivation

For the non-async case, early returns could escape the block so that the
error handling was skipped.

I was wondering for quite some time now what was going on here 😆

## Solution

Just put the block into the all-mighty "try-catch-closure" construct.

Annotating the return type of the closure improves the error message in
case of a mismatch between the function's return type and body.

Example code:
```
fn with_err() -> Result<(), String> {
    Ok(5)
}
```

Without return type annotation:
```
error[E0308]: mismatched types
 --> src/main.rs:2:37
  |
2 |   fn with_err() -> Result<(), String> {
  |  _____________________________________^
3 | |     Ok(5)
4 | | }
  | |_^ expected `()`, found integer
```

With return type annotation:
```
error[E0308]: mismatched types
 --> src/main.rs:3:8
  |
3 |     Ok(5)
  |        ^ expected `()`, found integer
```
2020-10-07 14:41:43 -07:00
Eliza Weisman
940808c68a core: remove mandatory liballoc dependency with no-std (#1017)
## Motivation

Presently, `tracing` and `tracing-core` allow disabling the Rust
standard library. However, even when used with the standard library
disabled, they still require the use of `alloc`, which means some form
of global allocator must exist. This can be a significant limitation on
some very constrained embedded devices. Additionally, the use of any
heap allocations on these devices ought to be avoided.

Now that the callsite registry is no longer a `Vec`, we don't need heap
allocations for storing registered callsites. However, `Dispatch`s are
currently _always_ stored in `Arc`s, and the global registry always
contains a `Vec` of `Weak` refs to them. This is required in order to
support thread-local scoped dispatcher API, where multiple `Dispatch`es
can coexist. However, on `no-std` platforms without threads, we already
don't support the thread-local scoped dispatch API. In this case, we can
reduce the complexity and the number of allocations required by the
dispatcher.

Additionally, even when the standard library is in use, if the
dispatcher is a global default rather than a scoped dispatcher, it
remains set forever. This means it can never be dropped. When this is
the case, it's possible to clone the dispatcher by cloning a `'static`
reference, rather than a (comparatively) costly `Arc` bump.

## Solution

This branch changes the global default dispatcher to be represented as a
`&'static dyn Subscriber` reference on all platforms. In addition, the
dependency on `liballoc` is now feature flagged, allowing it to be
disabled along with the dependency on `libstd`. This means that
`tracing` can now be used with _no_ heap allocations required.

In order to use `tracing` without `liballoc`, `Dispatch`es can only be
created from `&'static dyn Subscriber` references, since this is the
only way to erase their types that exists without boxed trait objects.
Therefore, I've added a new `Dispatch::from_static` constructor that is
always available. This is more limiting than `Dispatch::new` --- some
ways of modeling runtime composition of the subscriber are much harder
(although it could still contain state). However, users without `alloc`
are probably fairly used to this --- the `log` crate [has the same
limitation](https://docs.rs/log/0.4.11/log/#use-with-std) when used
without `std`.

Also, I've changed the global subscriber registry when `std` _is_
enabled to use a `RwLock` rather than a mutex. Now, multiple callsites
can be registered simultaneously, and a write lock is only necessary to
add a new subscriber or deregister old ones. It's now fine to use
`RwLock` here, because we no longer use any locking at all when `std` is
not enabled --- we always use the single global default subscriber, so
we don't require a list of currently live subscribers.

I've modified the `tracing` and `tracing-core` crates to add new feature
flags for enabling `std` and `alloc` separately.

Closes #861

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-10-07 12:38:08 -07:00
dependabot-preview[bot]
692c879c1f chore(deps): update cfg-if requirement from 0.1.10 to 1.0.0 (#1023)
Updates the requirements on [cfg-if](https://github.com/alexcrichton/cfg-if) to permit the latest version.
- [Release notes](https://github.com/alexcrichton/cfg-if/releases)
- [Commits](https://github.com/alexcrichton/cfg-if/compare/0.1.10...1.0.0)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
2020-10-07 09:38:36 -07:00
Joshua Nelson
1d901b33a5 subscriber: warn if trying to enable a statically disabled level (#990)
## Motivation


Fixes https://github.com/tokio-rs/tracing/issues/975.

## Solution

This implements the warning in `tracing-subscriber` only, as mentioned
in
https://github.com/tokio-rs/tracing/issues/975#issuecomment-692903469.
It warns whenever directives are parsed, including programmatically and
through environment variables. It does not include the suggested new API
which returns the filters that wouldn't be parsed.

- List filters that would be ignored
- Mention 'static max level'
- Describe how to enable the logging

Example output:

```
$ RUST_LOG=off,debug,silenced[x]=trace cargo run -q
warning: some trace filter directives would enable traces that are disabled statically
 | `debug` would enable the DEBUG level for all targets
 | `silenced[x]=trace` would enable the TRACE level for the `silenced` target
 = note: the static max level is info
 = note: to enable DEBUG logging, remove the `max_level_info` feature
```
![image](https://user-images.githubusercontent.com/23638587/95243324-77dc6a00-07de-11eb-8ed3-6ee2109940d4.png)
2020-10-06 16:36:00 -07:00
nasa
cb1dd95b8a subscriber: use macros for module declarations (#1009)
## Motivation

This is code refactoring. Currently, there is a lot of code that declares
modules as follows:

```rust
#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub use registry::Registry;

#[cfg(feature = "registry")]
#[cfg_attr(docsrs, doc(cfg(feature = "registry")))]
pub fn registry() -> Registry {
    Registry::default()
}
```

It is verbose to write a module declaration multiple times using the feature
attribute. Also, if the definition for the same feature spans multiple places,
it will be a little harder to read the code.

## Solution

You can combine features attributes in one place by writing as follows. It also
eliminates the need to write the same code over and over again.

```rust
cfg_feature!("registry", {
    pub use registry::Registry;

    pub fn registry() -> Registry {
        Registry::default()
    }
});

```

If this code is accepted, we will extend the declaration of the module using
macros to other files as well as `tracing-subscriber/lib.rs`.
2020-10-06 11:12:59 -07:00
Joshua Nelson
dcad8d0f10 chore: remove stdlib.rs (#1008)
## Motivation

Since `core` and `alloc` are unconditionally available, there is no
need to use a facade module for most of std.

Follow-up to https://github.com/tokio-rs/tracing/pull/960#discussion_r497655633:
these modules aren't necessary.

## Solution

Most of the imports could be switch to `use core` or `use alloc` without
trouble. The only exceptions were for `Mutex` and `Once`, which are only
available with the full standard library. These had a `sync` module
added to `tracing-core` which acts as a facade, re-exporting `std` if
available and `spin` if not.
2020-10-05 17:40:32 -07:00
Eliza Weisman
4707e6fe40 core: fix linked list tests reusing Registrations (#1016)
## Motivation

Currently, the tests for the linked list implementation in
`tracing_core::callsite` declare two static `Registration`s in the test
module, which are used in multiple tests. Since these tests are all run
in one binary, in separate threads, these statics are shared across all
tests. This means that --- depending on the order in which tests are run
--- these `Registration`s may already have values for their `next`
pointers. In particular, there's a potential issue where the `for_each`
in the test `linked_list_push` can loop repeatedly, if one of the
`Registration`s already had a next pointer from a previous test. See:
https://github.com/tokio-rs/tracing/pull/1008#issuecomment-703041964

## Solution

This branch declares separate `Registration` statics for each test.
These are not shared between multiple test threads.

We still reuse the same callsite statics, since their state does not
change during the tests --- only the `Registration`s change. I also
refactored the test code a little bit to use only a single type
implementing `Callsite`, rather than two separate types, since the
callsite impl is not actually used and this makes the code somewhat more
concise.

Finally, I added a new test for pushing more than two callsites.

Signed-off-by: Eliza Weisman <eliza@buoyant.io>
2020-10-05 16:17:30 -07:00