tracing-async2
This crate makes it easy to create your own custom tracing layers using a simple callback mechanism. One abvious use is to store tracing events into a database but you could just as well send them to a downstream service using gRPC or http. Or, for testing purposes, to accumulate them into a vector.
This crate provides a set of tracing layers that can be used to process
[tracing::Event
]s using simple callbacks or even in an asynchronous
context. This is done using variations the [CallbackLayer
].
Using the tracing_layer_async_within
macro
This macro simplifies some async scenarios where the provided callback was
not Sync
or Send
. Here is an example of how you could use this macro to
create a layer that saves tracing events into a database using tokio_postgres
:
The above code gets expanded into the code below:
Of note are the following:
- The
PGClient
was declared as a reference but the generated returned function requires it to be owned. buffer_size
is an additional parameter to the generated function.
Using callback_layer
If your needs are really simple, like accumulating traces in a vector.
Then you can use the [callback_layer
]:
use ;
use ;
use ;
let log = new;
let cb_log = log.clone;
registry
.with
.with
.init;
Using channel_layer
If you ever had the need to record traces in a database or do something
asynchronously with [tracing::Event
], then you can use the
[channel_layer
]:
use channel_layer;
use ;
use ;
let rt = new_current_thread
.build
.expect;
rt.block_on;
Using async_layer
If you don't want to handle the channel yourself, then you might consider
the use of [async_layer
] instead:
use async_layer;
use ;
use ;
let rt = new_current_thread
.build
.expect;
rt.block_on;
License: MIT