You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use tracing::{instrument,Span};
...
let span = info_span!("my_span");let guard = span.enter();/* -- alternative: let span = info_span!("my_span").entered(); defer! { drop(span) } // See https://github.com/rodrigocfd/defer-lite/ */// let _ = info_span!(...) // GOTCHA: DON'T DO THIS, immediately drops your span, so the hierarchy breaks
span.record("foo","bar");// must be defined in the span macro// auto associated with spaninfo!("something happened");// or warn!(...) or error!(...) or debug!(...)
span.record("error",anyhow!("something went wrong").into());// If required, reference the current span (last entered span on this thread):// tracing::Span::current()let current_span = Span::current();// manually set parent span//TODO: verifylet _ = info_span!(parent: span.id(),"foo_bar");// -- Either drop or use https://github.com/rodrigocfd/defer-lite/// drop(guard); // if you don't drop, rustc might drop your span early
use tracing_attributes::instrument;
...#[instrument]fndo_something(foo:&str) -> Result<String, anyhow::Error>{// these are automatically associated with the spandebug!(aa = 7,"not that important");info!("interesting");warn!("hm...");error!("ooh no!");//GOTCHA: attributes/fields must be declared in the attributeOk("output".to_owned())}#[instrument(err)]fndo_something_dangerous() -> Result<String, anyhow::Error>{todo!("boo")}
TODO: silent failure for attributes that aren't predefined
Get current span
info_span!("...", foo = tracing::field::Empty).entered();// not .enter()// allocate field `foo`, so we can set later
...Span::current().record("foo",88);// record field previously allocated
Set current span
let _ = span.entered();// ... do some work// _ is dropped at the end
Associating logs with spans, adding attributes
use tracing::{debug, error, info, info_span, warn};
...
let span = info_span!("do_something").entered();
...// attributes do NOT need to be declared on span firstdebug!(message = "example debug msg", aa = 7);info!(message = "example info msg", b = true);warn!(message = "example warn log event", f = 3.2);error!("ooh no!");// This marks span as error (in Jaeger too)// -- or put the message lastinfo!(cc = "cheese","something happened");info!(dd = %foo1,"whatever")// use fmt::Displayinfo!(ee = ?foo2,"something")// use fmt::Debug
Add a brief delay at the end of your program to report any outstanding spans
root_span.exit();// -- Give the last span time to report to Jaeger//sleep(Duration::from_millis(300));
opentelemetry::global::shutdown_tracer_provider();