-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for initializing tracing
There is a second kind of infrastructure in addition to logging that requires initialization: tracing. In a nutshell, in order to collect traces we need to register a subscriber. In our case, we want this subscriber to log events and spans as it would do for regular log based messages. This change adds the infrastructure for doing so. In binaries we would typically just set a global subscriber and be done. However, we want to preserve per-test configurability and so we just set the subscriber for the context of the test. Because conceptually logging and tracing are two separate concerns and applications are free to mix and match, we introduce two features, log and trace, that enable one or the other or both.
- Loading branch information
Showing
5 changed files
with
143 additions
and
20 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,6 +1,14 @@ | ||
// Copyright (C) 2019-2020 Daniel Mueller <[email protected]> | ||
// SPDX-License-Identifier: (Apache-2.0 OR MIT) | ||
|
||
use tokio::runtime::Builder; | ||
|
||
use tracing::debug; | ||
use tracing::error; | ||
use tracing::info; | ||
use tracing::instrument; | ||
|
||
|
||
mod something { | ||
pub type Error = String; | ||
} | ||
|
@@ -29,6 +37,36 @@ async fn with_inner_test_attribute_and_async() { | |
assert_eq!(async { 42 }.await, 42) | ||
} | ||
|
||
#[instrument] | ||
async fn instrumented(input: usize) -> usize { | ||
info!("input = {}", input); | ||
if input == 0 || input == 4 { | ||
error!("here we go"); | ||
} | ||
let result = input + 1; | ||
info!("result = {}", result); | ||
result | ||
} | ||
|
||
#[test_env_log::test] | ||
fn trace_with_custom_runtime() { | ||
let mut rt = Builder::new().basic_scheduler().build().unwrap(); | ||
|
||
rt.block_on(async { | ||
instrumented(0).await; | ||
instrumented(1).await; | ||
debug!("done"); | ||
}) | ||
} | ||
|
||
#[test_env_log::test(tokio::test)] | ||
async fn trace_with_tokio_attribute() { | ||
instrumented(6).await; | ||
instrumented(4).await; | ||
debug!("done"); | ||
} | ||
|
||
|
||
mod local { | ||
use super::Error; | ||
|
||
|