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
I have the need to have an attribute, say environment that is attached to every event without the need to create a global span and pass that throughout the application.
Motivation
There are some attributes which are common to an application, usually known at app-initialization time. For example, environment which is taken from some environment variable say ENVIRONMENT which will have the value dev | stage | uat | prod.
It would be nice to just be able to define this at initialization time, for attributes that should be included with each event as "global" base attributes. I can see this for distributed systems with something like runner_id as well which is a run-time static variable.
Proposal
In these cases the values are likely known at startup, so they could be assigned as part of the tracing initialization. Expanding on the builder pattern with some kind of .with_base_attribute(k, v) that can be called numerous times to register each one.
This attribute could be attached to each event and logged without the need for juggling spans, especially in a highly asynchronous code-base.
Alternatives
I have tried using an info_span! that I create in the main of the application and .enter it, retaining the guard for the lifetime of the application. It does not seem to work if tokio::spawn is called to create tasks, even if they are marked as #[instrument].
I have tried manually attaching the span via .instrument(span.clone()) on the async fn spawned, which does seem to work. The problem is those tasks also spawn other tasks and it becomes quite messy attempting to "pass it all the way down".
Example:
// GOAL: I want `environment = dev` to be added to every `event!` logged// This is run as a tokio::task#[instrument(skip_all)]asyncfndispatcher(mutrx: tokio::mpsc::Receiver<Message>, ...) -> Result<()>{whileletSome(msg) = rx.recv.await{match msg {Message::SpawnTask => {// We could call `.instrument` on run_bg_task() but what is the span?
tokio::spawn(run_bg_task())}}}Ok(())}// This span does not capture `dispatcher` as the parent automatically#[instrument]asyncfnrun_bg_task() -> Result<()>{// do some work...Ok(())}
The text was updated successfully, but these errors were encountered:
Feature Request
I have the need to have an attribute, say
environment
that is attached to every event without the need to create a global span and pass that throughout the application.Motivation
There are some attributes which are common to an application, usually known at app-initialization time. For example,
environment
which is taken from some environment variable sayENVIRONMENT
which will have the valuedev | stage | uat | prod
.It would be nice to just be able to define this at initialization time, for attributes that should be included with each event as "global" base attributes. I can see this for distributed systems with something like
runner_id
as well which is a run-time static variable.Proposal
In these cases the values are likely known at startup, so they could be assigned as part of the
tracing
initialization. Expanding on the builder pattern with some kind of.with_base_attribute(k, v)
that can be called numerous times to register each one.This attribute could be attached to each event and logged without the need for juggling spans, especially in a highly asynchronous code-base.
Alternatives
I have tried using an
info_span!
that I create in themain
of the application and.enter
it, retaining the guard for the lifetime of the application. It does not seem to work iftokio::spawn
is called to create tasks, even if they are marked as#[instrument]
.I have tried manually attaching the span via
.instrument(span.clone())
on theasync fn
spawned, which does seem to work. The problem is those tasks also spawn other tasks and it becomes quite messy attempting to "pass it all the way down".Example:
The text was updated successfully, but these errors were encountered: