Skip to content

Commit

Permalink
attributes: remove default level for instrument attribute macro
Browse files Browse the repository at this point in the history
This commit removes the default level for the #[instrument] attribute
macro, forcing user to explicitly specify the logging level.

Fixes tokio-rs#1070

Signed-off-by: psinghal20 <psinghal20@gmail.com>
  • Loading branch information
psinghal20 committed Nov 25, 2020
1 parent 710228e commit a0706b9
Show file tree
Hide file tree
Showing 26 changed files with 82 additions and 86 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ use tracing::{debug, error, info, span, warn, Level};
// the `#[tracing::instrument]` attribute creates and enters a span
// every time the instrumented function is called. The span is named after the
// the function or method. Parameters passed to the function are recorded as fields.
#[tracing::instrument]
#[tracing::instrument(level = "info")]
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> {
// this creates an event at the DEBUG level with two fields:
// - `excitement`, with the key "excitement" and the value "yay!"
Expand Down Expand Up @@ -199,7 +199,7 @@ use tracing::{info, instrument};
use tokio::{io::AsyncWriteExt, net::TcpStream};
use std::io;

#[instrument]
#[instrument(level = "info")]
async fn write(stream: &mut TcpStream) -> io::Result<usize> {
let result = stream.write(b"hello world\n").await;
info!("wrote to stream; success={:?}", result.is_ok());
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/async-fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ use tracing_attributes::instrument;

use std::{error::Error, io, net::SocketAddr};

#[instrument]
#[instrument(level = "info")]
async fn connect(addr: &SocketAddr) -> io::Result<TcpStream> {
let stream = TcpStream::connect(&addr).await;
tracing::info!("created stream");
stream
}

#[instrument]
#[instrument(level = "info")]
async fn write(stream: &mut TcpStream) -> io::Result<usize> {
let result = stream.write(b"hello world\n").await;
info!("wrote to stream; success={:?}", result.is_ok());
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/attrs-args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use tracing::{debug, info};
use tracing_attributes::instrument;

#[instrument]
#[instrument(level = "info")]
fn nth_fibonacci(n: u64) -> u64 {
if n == 0 || n == 1 {
debug!("Base case");
Expand All @@ -14,7 +14,7 @@ fn nth_fibonacci(n: u64) -> u64 {
}
}

#[instrument]
#[instrument(level = "info")]
fn fibonacci_seq(to: u64) -> Vec<u64> {
let mut sequence = vec![];

Expand Down
2 changes: 1 addition & 1 deletion examples/examples/attrs-basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use tracing::{debug, info, span, Level};
use tracing_attributes::instrument;

#[instrument]
#[instrument(level = "info")]
#[inline]
fn suggest_band() -> String {
debug!("Suggesting a band.");
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/attrs-literal-field-names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use tracing::{debug, span, Level};
use tracing_attributes::instrument;

#[instrument]
#[instrument(level = "info")]
#[inline]
fn suggest_band() -> String {
debug!("Suggesting a band.");
Expand Down
6 changes: 3 additions & 3 deletions examples/examples/custom-error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,20 @@ impl fmt::Display for FooError {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn do_something(foo: &str) -> Result<&'static str, impl Error + Send + Sync + 'static> {
do_another_thing(42, false)
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn do_another_thing(
answer: usize,
will_succeed: bool,
) -> Result<&'static str, impl Error + Send + Sync + 'static> {
Err(FooError::new("something broke, lol"))
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::subscriber())
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/fmt/yak_shave.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use tracing::{debug, error, info, span, trace, warn, Level};
// the `#[tracing::instrument]` attribute creates and enters a span
// every time the instrumented function is called. The span is named after the
// the function or method. Paramaters passed to the function are recorded as fields.
#[tracing::instrument]
#[tracing::instrument(level = "info")]
pub fn shave(yak: usize) -> Result<(), Box<dyn Error + 'static>> {
// this creates an event at the TRACE log level with two fields:
// - `excitement`, with the key "excitement" and the value "yay!"
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/futures-proxy-server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use tracing::{debug, debug_span, info, instrument, warn, Instrument as _};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[instrument]
#[instrument(level = "info")]
async fn transfer(mut inbound: TcpStream, proxy_addr: SocketAddr) -> Result<(), Error> {
let mut outbound = TcpStream::connect(&proxy_addr).await?;

Expand Down
6 changes: 3 additions & 3 deletions examples/examples/instrumented-error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ impl fmt::Display for FooError {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn do_something(foo: &str) -> Result<&'static str, impl Error + Send + Sync + 'static> {
// Results can be instrumented with a `SpanTrace` via the `InstrumentResult` trait
do_another_thing(42, false).in_current_span()
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn do_another_thing(
answer: usize,
will_succeed: bool,
Expand All @@ -41,7 +41,7 @@ fn do_another_thing(
Err(FooError::new("something broke, lol").in_current_span())
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn main() {
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::subscriber())
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/map-traced-error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ fn print_extracted_spantraces(error: &(dyn Error + 'static)) {
}
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn do_something() -> Result<(), TracedError<OuterError>> {
do_the_real_stuff().map_err(TracedError::err_into)
}

#[tracing::instrument]
#[tracing::instrument(level = "info")]
fn do_the_real_stuff() -> Result<(), TracedError<InnerError>> {
Err(InnerError).map_err(TracedError::from)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/opentelemetry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use tracing::{span, trace, warn};
use tracing_attributes::instrument;
use tracing_subscriber::prelude::*;

#[instrument]
#[instrument(level = "info")]
#[inline]
fn expensive_work() -> &'static str {
span!(tracing::Level::INFO, "expensive_step_1")
Expand Down
4 changes: 2 additions & 2 deletions examples/examples/spawny-thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::error::Error;
use tracing::{debug, info};
use tracing_attributes::instrument;

#[instrument]
#[instrument(level = "info")]
async fn parent_task(subtasks: usize) {
info!("spawning subtasks...");
let subtasks = (1..=subtasks)
Expand All @@ -29,7 +29,7 @@ async fn parent_task(subtasks: usize) {
info!(sum);
}

#[instrument]
#[instrument(level = "info")]
async fn subtask(number: usize) -> usize {
info!("polling subtask...");
number
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/tokio-spawny-thing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use tracing::{debug, info, instrument, span, Instrument as _, Level};

type Error = Box<dyn std::error::Error + Send + Sync + 'static>;

#[instrument]
#[instrument(level = "info")]
async fn parent_task(subtasks: usize) -> Result<(), Error> {
info!("spawning subtasks...");
let subtasks = (1..=subtasks)
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/tower-load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ fn gen_uri(authority: &str) -> (usize, String) {
(len, format!("http://{}/{}", authority, letter))
}

#[tracing::instrument(target = "gen", "load_gen")]
#[tracing::instrument(target = "gen", "load_gen", level = "info")]
async fn load_gen(addr: SocketAddr) -> Result<(), Err> {
let svc = ServiceBuilder::new()
.buffer(5)
Expand Down
2 changes: 1 addition & 1 deletion tracing-attributes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ with a `tracing` [span]. For example:
```rust
use tracing_attributes::instrument;

#[instrument]
#[instrument(level = "info")]
pub fn my_function(my_arg: usize) {
// ...
}
Expand Down
36 changes: 16 additions & 20 deletions tracing-attributes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
//! ```
//! use tracing_attributes::instrument;
//!
//! #[instrument]
//! #[instrument(level = "info")]
//! pub fn my_function(my_arg: usize) {
//! // ...
//! }
Expand Down Expand Up @@ -101,7 +101,6 @@ use syn::{
/// Instruments a function to create and enter a `tracing` [span] every time
/// the function is called.
///
/// Unless overriden, a span with `info` level will be generated.
/// The generated span's name will be the name of the function. Any arguments
/// to that function will be recorded as fields using [`fmt::Debug`]. To skip
/// recording a function's or method's argument, pass the argument's name
Expand All @@ -125,34 +124,26 @@ use syn::{
/// Instrumenting a function:
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument]
/// #[instrument(level = "info")]
/// pub fn my_function(my_arg: usize) {
/// // This event will be recorded inside a span named `my_function` with the
/// // field `my_arg`.
/// tracing::info!("inside my_function!");
/// // ...
/// }
/// ```
/// Setting the level for the generated span:
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(level = "debug")]
/// pub fn my_function() {
/// // ...
/// }
/// ```
/// Overriding the generated span's name:
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(name = "my_name")]
/// #[instrument(name = "my_name", level = "info")]
/// pub fn my_function() {
/// // ...
/// }
/// ```
/// Overriding the generated span's target:
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(target = "my_target")]
/// #[instrument(target = "my_target", "new_load", level = "info")]
/// pub fn my_function() {
/// // ...
/// }
Expand All @@ -164,7 +155,7 @@ use syn::{
/// # use tracing_attributes::instrument;
/// struct NonDebug;
///
/// #[instrument(skip(non_debug))]
/// #[instrument(skip(non_debug), level = "info")]
/// fn my_function(arg: usize, non_debug: NonDebug) {
/// // ...
/// }
Expand All @@ -174,7 +165,7 @@ use syn::{
///
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(fields(foo="bar", id=1, show=true))]
/// #[instrument(fields(foo="bar", id=1, show=true), level = "info")]
/// fn my_function(arg: usize) {
/// // ...
/// }
Expand All @@ -185,7 +176,7 @@ use syn::{
///
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument(err)]
/// #[instrument(err, level = "info")]
/// fn my_function(arg: usize) -> Result<(), std::io::Error> {
/// Ok(())
/// }
Expand All @@ -195,7 +186,7 @@ use syn::{
///
/// ```
/// # use tracing_attributes::instrument;
/// #[instrument]
/// #[instrument(level = "info")]
/// pub async fn my_function() -> Result<(), ()> {
/// // ...
/// # Ok(())
Expand All @@ -221,7 +212,7 @@ use syn::{
///
/// #[async_trait]
/// impl Foo for FooImpl {
/// #[instrument(fields(value = self.0, tmp = std::any::type_name::<Self>()))]
/// #[instrument(fields(value = self.0, tmp = std::any::type_name::<Self>()), level = "debug")]
/// async fn foo(&self, arg: usize) {}
/// }
/// ```
Expand All @@ -244,7 +235,7 @@ use syn::{
///
/// #[async_trait]
/// impl Bar for BarImpl {
/// #[instrument(fields(tmp = std::any::type_name::<Self>()))]
/// #[instrument(fields(tmp = std::any::type_name::<Self>()), level = "debug")]
/// async fn bar() {}
/// }
/// ```
Expand Down Expand Up @@ -546,7 +537,12 @@ impl InstrumentArgs {
\"debug\", \"info\", \"warn\", or \"error\", or a number 1-5"
)
},
None => quote!(tracing::Level::INFO),
None => quote! {
compile_error!(
"no verbosity level specified, expected one of \"trace\", \
\"debug\", \"info\", \"warn\", or \"error\", or a number 1-5"
)
},
}
}

Expand Down
Loading

0 comments on commit a0706b9

Please sign in to comment.