Expand description
Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
This file is part of DMSC. The DMSC project belongs to the Dunimd Team.
Licensed under the Apache License, Version 2.0 (the “License”); You may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
§Distributed Tracing
This file implements a comprehensive distributed tracing system for the DMSC framework. It provides tools for creating, managing, and propagating trace information across asynchronous operations and distributed systems. The tracing system follows the W3C Trace Context standard and integrates with tokio’s context propagation mechanism.
§Key Components
- DMSCSpanId: Unique identifier for a span
- DMSCTraceId: Unique identifier for a trace
- DMSCSpanKind: Enumeration of span types (Server, Client, Producer, Consumer, Internal)
- DMSCSpanStatus: Status of a span (Ok, Error, Unset)
- DMSCSpan: A single distributed tracing span with attributes, events, and status
- DMSCSpanEvent: Timed events within a span
- DMSCTracingContext: Thread-local tracing context for propagation
- DMSCTracer: Distributed tracer for creating and managing spans
- DMSCTracerManager: Manager for multiple tracer instances
- DefaultTracerManager: Global tracer manager instance
§Design Principles
- W3C Trace Context Compliance: Follows the W3C Trace Context standard for interoperability
- Async Context Propagation: Integrates with tokio’s context propagation mechanism
- Thread Safety: Uses Arc and RwLock for safe concurrent access
- Sampling Support: Configurable sampling rate to control overhead
- Hierarchical Spans: Supports parent-child span relationships
- Baggage Support: Allows carrying contextual information across spans
- Extensible: Easy to add new span kinds and attributes
- Low Overhead: Efficient implementation with minimal performance impact
- Global Access: Provides a global tracer manager for easy access
- Serialization Support: All tracing components are serializable for export
§Usage
use dmsc::observability::{init_tracer, tracer, DMSCSpanKind, DMSCSpanStatus};
use dmsc::core::DMSCResult;
async fn example() -> DMSCResult<()> {
// Initialize the global tracer with 100% sampling rate
init_tracer(1.0);
// Get the global tracer
let tracer = tracer();
// Start a new trace
let trace_id = tracer.start_trace("example_trace").unwrap();
// Start a child span
let span_id = tracer.start_span_from_context("child_span", DMSCSpanKind::Internal).unwrap();
// Add an attribute to the span
tracer.span_mut(&span_id, |span| {
span.set_attribute("key".to_string(), "value".to_string());
})?;
// Add an event to the span
tracer.span_mut(&span_id, |span| {
let mut attributes = std::collections::HashMap::new();
attributes.insert("event_key".to_string(), "event_value".to_string());
span.add_event("example_event".to_string(), attributes);
})?;
// End the child span with OK status
tracer.end_span(&span_id, DMSCSpanStatus::Ok)?;
Ok(())
}Structs§
- DMSC
Span - A distributed tracing span
- DMSC
Span Event - Span event for recording timed occurrences
- DMSC
Span Id - Distributed tracing span ID
- DMSC
Trace Id - Distributed tracing trace ID
- DMSC
Tracer - Distributed tracer
- DMSC
Tracer Manager - Tracer manager for managing multiple tracer instances
- DMSC
Tracing Context - Thread-local tracing context
- Default
Tracer Manager - Default tracer manager instance
Enums§
- DMSC
Sampling Strategy - Sampling strategy enumeration
- DMSC
Span Kind - Span kind enumeration
- DMSC
Span Status - Span status
Statics§
- DEFAULT_
TRACER_ MANAGER - Global tracer manager instance
Functions§
- init_
tracer - Initialize global tracer with fixed rate (backward compatibility)
- init_
tracer_ with_ strategy - Initialize global tracer with custom sampling strategy
- tracer
- Get global tracer (backward compatibility)