Skip to content
Merged
16 changes: 8 additions & 8 deletions e2e/data/expected_context.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ segmentItems:
endTime: gt 0
isError: false
operationName: /pong
parentSpanId: 1
parentSpanId: 0
peer: consumer:8082
skipAnalysis: false
spanId: 2
spanId: 1
spanLayer: Http
spanType: Exit
startTime: gt 0
- componentId: 11000
endTime: gt 0
isError: false
operationName: /ping
parentSpanId: 0
parentSpanId: -1
peer: ''
skipAnalysis: false
spanId: 1
spanId: 0
spanLayer: Http
spanType: Entry
startTime: gt 0
Expand All @@ -51,20 +51,20 @@ segmentItems:
endTime: gt 0
isError: false
operationName: /pong
parentSpanId: 0
parentSpanId: -1
peer: ''
refs:
- networkAddress: consumer:8082
parentEndpoint: /pong
parentService: producer
parentServiceInstance: node_0
parentSpanId: 2
parentSpanId: 1
parentTraceSegmentId: not null
refType: CrossProcess
traceId: not null
skipAnalysis: false
spanId: 1
spanId: 0
spanLayer: Http
spanType: Entry
startTime: gt 0
serviceName: consumer
serviceName: consumer
1 change: 1 addition & 0 deletions src/context/propagation/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

pub static SKYWALKING_HTTP_CONTEXT_HEADER_KEY: &str = "sw8";

#[derive(Debug)]
pub struct PropagationContext {
/// It defines whether next span should be trace or not.
/// In SkyWalking, If `do_sample == true`, the span should be reported to
Expand Down
2 changes: 1 addition & 1 deletion src/context/propagation/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn encode_propagation(context: &TracingContext, endpoint: &str, address: &st
res += "1-";
res += format!("{}-", encode(context.trace_id.to_string())).as_str();
res += format!("{}-", encode(context.trace_segment_id.to_string())).as_str();
res += format!("{}-", context.next_span_id).as_str();
res += format!("{}-", context.peek_active_span_id().unwrap_or(0)).as_str();
res += format!("{}-", encode(context.service.as_str())).as_str();
res += format!("{}-", encode(context.service_instance.as_str())).as_str();
res += format!("{}-", encode(endpoint)).as_str();
Expand Down
2 changes: 1 addition & 1 deletion src/context/system_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ impl TimeFetcher for UnixTimeStampFetcher {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_secs() as i64
.as_millis() as i64
}
}
33 changes: 27 additions & 6 deletions src/context/trace_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use crate::skywalking_proto::v3::{
KeyStringValuePair, Log, RefType, SegmentObject, SegmentReference, SpanLayer, SpanObject,
SpanType,
};
use std::collections::LinkedList;
use std::fmt::Formatter;
use std::sync::Arc;

Expand Down Expand Up @@ -70,10 +71,12 @@ impl std::fmt::Debug for Span {
}
}

static SKYWALKING_RUST_COMPONENT_ID: i32 = 11000;
const SKYWALKING_RUST_COMPONENT_ID: i32 = 11000;

impl Span {
#[allow(clippy::too_many_arguments)]
pub fn new(
span_id: i32,
parent_span_id: i32,
operation_name: String,
remote_peer: String,
Expand All @@ -83,7 +86,7 @@ impl Span {
time_fetcher: Arc<dyn TimeFetcher + Sync + Send>,
) -> Self {
let span_internal = SpanObject {
span_id: parent_span_id + 1,
span_id,
parent_span_id,
start_time: time_fetcher.get(),
end_time: 0, // not set
Expand Down Expand Up @@ -114,6 +117,10 @@ impl Span {
&self.span_internal
}

pub fn span_object_mut(&mut self) -> &mut SpanObject {
&mut self.span_internal
}

/// Add logs to the span.
pub fn add_log(&mut self, message: Vec<(&str, &str)>) {
let log = Log {
Expand Down Expand Up @@ -155,6 +162,7 @@ pub struct TracingContext {
pub spans: Vec<Box<Span>>,
time_fetcher: Arc<dyn TimeFetcher + Sync + Send>,
segment_link: Option<PropagationContext>,
active_span_id_stack: LinkedList<i32>,
}

impl std::fmt::Debug for TracingContext {
Expand Down Expand Up @@ -192,6 +200,7 @@ impl TracingContext {
time_fetcher,
spans: Vec::new(),
segment_link: None,
active_span_id_stack: LinkedList::new(),
}
}

Expand Down Expand Up @@ -227,6 +236,7 @@ impl TracingContext {
time_fetcher,
spans: Vec::new(),
segment_link: Some(context),
active_span_id_stack: LinkedList::new(),
}
}

Expand Down Expand Up @@ -257,8 +267,11 @@ impl TracingContext {
return Err("entry span have already exist.");
}

let parent_span_id = self.peek_active_span_id().unwrap_or(-1);

let mut span = Box::new(Span::new(
self.next_span_id,
parent_span_id,
operation_name.to_string(),
String::default(),
SpanType::Entry,
Expand Down Expand Up @@ -300,6 +313,8 @@ impl TracingContext {
});
}
self.next_span_id += 1;
self.active_span_id_stack
.push_back(span.span_internal.span_id);
Ok(span)
}

Expand Down Expand Up @@ -335,8 +350,11 @@ impl TracingContext {
return Err("entry span must be existed.");
}

let parent_span_id = self.peek_active_span_id().unwrap_or(-1);

let span = Box::new(Span::new(
self.next_span_id,
parent_span_id,
operation_name.to_string(),
remote_peer.to_string(),
SpanType::Exit,
Expand All @@ -345,17 +363,16 @@ impl TracingContext {
self.time_fetcher.clone(),
));
self.next_span_id += 1;
self.active_span_id_stack
.push_back(span.span_internal.span_id);
Ok(span)
}

/// Close span. We can't use closed span after finalize called.
pub fn finalize_span(&mut self, mut span: Box<Span>) {
span.close();
self.spans.push(span);
}

pub fn finalize_span_for_test(&self, span: &mut Box<Span>) {
span.close();
self.active_span_id_stack.pop_back();
}

/// It converts tracing context into segment object.
Expand All @@ -376,4 +393,8 @@ impl TracingContext {
is_size_limited: false,
}
}

pub(crate) fn peek_active_span_id(&self) -> Option<i32> {
self.active_span_id_stack.back().copied()
}
}
Loading