trace: Allow trace instrumentation to emit log records (#992)

## Motivation

`tokio-trace` currently offers a strategy for compatibility with the
`log` crate: its macros can be dropped in as a replacement for `log`'s
macros, and a subscriber can be used that translates trace events to log
records. However, this requires the application to be aware of
`tokio-trace` and manually set up this subscriber.

Many libraries currently emit `log` records, and would like to be able
to emit `tokio-trace` instrumentation instead. The `tokio` runtimes are
one such example. However, with the current log compatibility strategy,
replacing existing logging with trace instrumentation would break
`tokio`'s logs for any downstream user which is using only `log` and not
`tokio-trace`. It is desirable for libraries to have the option to emit
both `log` _and_ `tokio-trace` diagnostics from the same instrumentation
points.

## Solution

This branch adds a `log` feature flag to the `tokio-trace` crate, which
when set, causes `tokio-trace` instrumentation to emit log records as well
as `tokio-trace` instrumentation. 

## Notes

In order to allow spans to log their names when they are entered and 
exited even when the span is disabled, this branch adds an 
`&'static Metadata` to the `Span` type. This was previously stored in
the `Inner` type and was thus only present when the span was enabled.
This makes disabled spans one word longer, but enabled spans remain
the same size.

Fixes: #949

Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
Eliza Weisman
2019-03-26 16:43:05 -07:00
committed by GitHub
parent ceca2a3cd6
commit d8177f81ac
12 changed files with 372 additions and 106 deletions
+5 -5
View File
@@ -4,11 +4,7 @@ mod support;
use self::support::*;
use tokio_trace::{
field::{debug, display},
subscriber::with_default,
Level,
};
use tokio_trace::{field::display, subscriber::with_default, Level};
#[test]
fn event_without_message() {
@@ -133,7 +129,11 @@ fn borrowed_field() {
}
#[test]
// If emitting log instrumentation, this gets moved anyway, breaking the test.
#[cfg(not(feature = "log"))]
fn move_field_out_of_struct() {
use tokio_trace::field::debug;
#[derive(Debug)]
struct Position {
x: f32,
+5 -5
View File
@@ -4,11 +4,7 @@ mod support;
use self::support::*;
use std::thread;
use tokio_trace::{
field::{debug, display},
subscriber::with_default,
Level, Span,
};
use tokio_trace::{field::display, subscriber::with_default, Level, Span};
#[test]
fn handles_to_the_same_span_are_equal() {
@@ -307,7 +303,11 @@ fn borrowed_field() {
}
#[test]
// If emitting log instrumentation, this gets moved anyway, breaking the test.
#[cfg(not(feature = "log"))]
fn move_field_out_of_struct() {
use tokio_trace::field::debug;
#[derive(Debug)]
struct Position {
x: f32,