Skip to content

Commit

Permalink
Fix trace id is not transmitted. (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmjoy authored Aug 26, 2022
1 parent 49f6d76 commit 10aa902
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/trace/trace_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl TracingContext {
propagation: &PropagationContext,
) -> Span {
let mut span = self.create_entry_span(operation_name);
self.trace_id = propagation.parent_trace_id.clone();
span.with_span_object_mut(|span| {
span.refs.push(SegmentReference {
ref_type: RefType::CrossProcess as i32,
Expand Down
56 changes: 55 additions & 1 deletion tests/trace_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use std::{
sync::{Arc, Mutex},
thread,
};
use tokio::{runtime::Handle, task};

/// Serialize from A should equal Serialize from B
#[allow(dead_code)]
Expand Down Expand Up @@ -217,7 +218,7 @@ async fn create_span_from_context() {
}

#[test]
fn crossprocess_test() {
fn cross_process_test() {
let tracer = Tracer::new("service", "instance", PrintReporter::new());
let mut context1 = tracer.create_trace_context();
assert_eq!(context1.service(), "service");
Expand All @@ -239,6 +240,7 @@ fn crossprocess_test() {

let span3 = context2.last_span().unwrap();

assert_eq!(context1.trace_id(), context2.trace_id());
assert_eq!(span3.span_id, 0);
assert_eq!(span3.parent_span_id, -1);
assert_eq!(span3.refs.len(), 1);
Expand All @@ -259,6 +261,58 @@ fn crossprocess_test() {
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn cross_process_test_1() {
let propagation = Mutex::new(String::new());

MockReporter::with(
|reporter| {
let tracer = Tracer::new("service1", "instance1", reporter);
let mut context = tracer.create_trace_context();
let _span1 = context.create_entry_span("entry_1");
let _span2 = context.create_exit_span("exit_1", "peer_1");
*propagation.try_lock().unwrap() = encode_propagation(&context, "exit_1", "peer_1");
tracer
},
|segment1| {
let propagation = propagation.lock().unwrap().clone();
task::block_in_place(move || {
Handle::current().block_on(async move {
MockReporter::with(
|reporter| {
let tracer = Tracer::new("service2", "instance2", reporter);
let mut context = tracer.create_trace_context();
let _span1 = context.create_entry_span_with_propagation(
"entry_1",
&decode_propagation(&propagation).unwrap(),
);
tracer
},
|segment2| {
assert_eq!(segment1.trace_id, segment2.trace_id);
assert_eq!(
segment2.spans[0].refs[0],
SegmentReference {
ref_type: RefType::CrossProcess as i32,
trace_id: segment1.trace_id.clone(),
parent_trace_segment_id: segment1.trace_segment_id.clone(),
parent_span_id: 1,
parent_service: segment1.service.clone(),
parent_service_instance: segment1.service_instance.clone(),
parent_endpoint: segment1.spans[0].operation_name.clone(),
network_address_used_at_peer: segment1.spans[0].peer.clone(),
}
);
},
)
.await;
});
});
},
)
.await;
}

#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn cross_threads_test() {
MockReporter::with_many(
Expand Down

0 comments on commit 10aa902

Please sign in to comment.