pub struct AsyncTraceDispatcher { /* private fields */ }Expand description
Asynchronous trace dispatcher for RocketMQ message tracing.
Collects trace contexts from producers and consumers, batches them according to configured thresholds, and asynchronously sends them to a trace topic. The dispatcher uses a bounded channel for non-blocking appends and spawns background tasks for batch processing.
Trace contexts are grouped by topic before being encoded and sent. Messages exceeding the configured size limit are split into multiple trace messages.
§Implementation
Uses a tokio::mpsc::channel with a capacity of 2048 for queuing trace contexts and flush
commands. A single worker task periodically flushes batches based on count, time thresholds, or
explicit flush requests. The worker is submitted through the client runtime helper, which uses
the explicitly injected client runtime.
§Discard Policy
When the internal queue is full, new trace contexts are discarded and the discard counter is incremented. This prevents blocking the caller.
Implementations§
Source§impl AsyncTraceDispatcher
impl AsyncTraceDispatcher
Sourcepub fn new(
client_runtime: Arc<ClientRuntime>,
group: &str,
type_: Type,
batch_num: usize,
trace_topic_name: &str,
rpc_hook: Option<Arc<dyn RPCHook>>,
) -> Self
pub fn new( client_runtime: Arc<ClientRuntime>, group: &str, type_: Type, batch_num: usize, trace_topic_name: &str, rpc_hook: Option<Arc<dyn RPCHook>>, ) -> Self
Creates a new async trace dispatcher.
The dispatcher is created in a stopped state and must be started via
start before it can process trace contexts.
§Arguments
group- Producer or consumer group name used for trace producer identificationtype_- Trace type, eitherType::ProduceorType::Consumebatch_num- Maximum number of contexts per batch (capped at 20)trace_topic_name- Destination topic for trace messages (uses system default if empty)rpc_hook- Optional RPC hook applied to the internal trace producer
Sourcepub fn get_discard_count(&self) -> u64
pub fn get_discard_count(&self) -> u64
Returns the number of trace contexts discarded due to queue overflow.
Sourcepub fn queue_size(&self) -> usize
pub fn queue_size(&self) -> usize
Returns the approximate number of trace contexts queued for the worker.
Sourcepub fn is_started(&self) -> bool
pub fn is_started(&self) -> bool
Returns whether the dispatcher has been started.
Sourcepub fn is_stopped(&self) -> bool
pub fn is_stopped(&self) -> bool
Returns whether the dispatcher has been stopped.
Sourcepub fn batch_num(&self) -> usize
pub fn batch_num(&self) -> usize
Returns the configured maximum number of trace contexts per batch.
Sourcepub fn set_client_host(&self, client_host: Option<CheetahString>)
pub fn set_client_host(&self, client_host: Option<CheetahString>)
Sets the resolved client identifier used to enrich generated trace records.
Sourcepub fn set_namespace_v2(&self, namespace_v2: Option<CheetahString>)
pub fn set_namespace_v2(&self, namespace_v2: Option<CheetahString>)
Sets the namespace v2 configuration for the dispatcher.
Sourcepub fn set_use_tls(&self, use_tls: bool)
pub fn set_use_tls(&self, use_tls: bool)
Sets whether the internal trace producer should use TLS.
pub fn is_use_tls(&self) -> bool
Sourcepub fn get_trace_producer(&self) -> Option<Arc<Mutex<DefaultMQProducer>>>
pub fn get_trace_producer(&self) -> Option<Arc<Mutex<DefaultMQProducer>>>
Returns a reference to the internal trace producer.
The producer is available only after the dispatcher has been started.
pub fn trace_topic_name(&self) -> &str
Sourcepub async fn flush_async(&self) -> ClientResult<()>
pub async fn flush_async(&self) -> ClientResult<()>
Asynchronously flushes pending trace contexts without blocking a Tokio worker thread.
Sourcepub async fn shutdown_async(&self)
pub async fn shutdown_async(&self)
Asynchronously shuts down the dispatcher without blocking the current Tokio worker.
Concurrent callers await the same shutdown sequence, so pending traces are flushed and the worker is joined at most once.
pub fn register_shutdown_hook(&self)
pub fn register_shut_down_hook(&self)
pub fn remove_shutdown_hook(&self)
Trait Implementations§
Source§impl Drop for AsyncTraceDispatcher
impl Drop for AsyncTraceDispatcher
Source§impl TraceDispatcher for AsyncTraceDispatcher
impl TraceDispatcher for AsyncTraceDispatcher
Source§fn start(
&self,
name_srv_addr: &str,
access_channel: AccessChannel,
) -> ClientResult<()>
fn start( &self, name_srv_addr: &str, access_channel: AccessChannel, ) -> ClientResult<()>
Starts the trace dispatcher and its background worker task.
Initializes the internal trace producer, spawns the worker task for batch processing, and marks the dispatcher as started. This method can be called only once; subsequent calls are ignored.
§Errors
Returns an error if the dispatcher has already been started or if internal state initialization fails.
Source§fn append(&self, ctx: &dyn Any) -> bool
fn append(&self, ctx: &dyn Any) -> bool
Appends a trace context to the dispatcher queue.
This method does not block the calling thread. If the internal queue is full, the trace context is discarded and the discard counter is incremented.
§Arguments
ctx- Trace context, must be aTraceContextinstance
§Returns
Returns true if the context was successfully queued, false if it was
discarded due to queue overflow or type mismatch.
Source§fn flush(&self) -> ClientResult<()>
fn flush(&self) -> ClientResult<()>
Flushes pending trace contexts by requesting an acknowledgement from the worker task.
Blocks the calling thread until the worker processes the flush command or the flush timeout elapses.
§Errors
Returns an error if the dispatcher has not been started.
Source§fn shutdown(&self)
fn shutdown(&self)
Shuts down the dispatcher and stops the background worker task.
Attempts to flush remaining trace contexts before stopping. This method blocks briefly to allow pending data to be processed. The dispatcher cannot be restarted after shutdown.
Source§fn as_any(&self) -> &dyn Any
fn as_any(&self) -> &dyn Any
Any trait object. Read moreSource§fn as_mut_any(&mut self) -> &mut dyn Any
fn as_mut_any(&mut self) -> &mut dyn Any
Any trait object. Read morefn trace_topic_name(&self) -> Option<&str>
fn trace_client_host(&self) -> Option<CheetahString>
Auto Trait Implementations§
impl !Freeze for AsyncTraceDispatcher
impl !RefUnwindSafe for AsyncTraceDispatcher
impl !UnwindSafe for AsyncTraceDispatcher
impl Send for AsyncTraceDispatcher
impl Sync for AsyncTraceDispatcher
impl Unpin for AsyncTraceDispatcher
impl UnsafeUnpin for AsyncTraceDispatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.