You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2020/03/03 02:33:19 UTC

[skywalking-rust] branch context-manager updated: Could keep parent in the shadow, and build the span relationship based on stack automatically.

This is an automated email from the ASF dual-hosted git repository.

wusheng pushed a commit to branch context-manager
in repository https://gitbox.apache.org/repos/asf/skywalking-rust.git


The following commit(s) were added to refs/heads/context-manager by this push:
     new af889fc  Could keep parent in the shadow, and build the span relationship based on stack automatically.
af889fc is described below

commit af889fc4159e14daca978a39dac29f9e3910d2df
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Tue Mar 3 10:33:03 2020 +0800

    Could keep parent in the shadow, and build the span relationship based on stack automatically.
---
 core/src/skywalking/agent/context_manager.rs | 46 +++++++++++++++++++++++-----
 core/src/skywalking/core/context.rs          | 28 ++++++++---------
 2 files changed, 52 insertions(+), 22 deletions(-)

diff --git a/core/src/skywalking/agent/context_manager.rs b/core/src/skywalking/agent/context_manager.rs
index 3db7980..a492e42 100644
--- a/core/src/skywalking/agent/context_manager.rs
+++ b/core/src/skywalking/agent/context_manager.rs
@@ -11,7 +11,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-use std::borrow::BorrowMut;
+use std::borrow::{Borrow, BorrowMut};
 use std::cell::RefCell;
 use std::rc::Rc;
 
@@ -31,18 +31,21 @@ lazy_static! {
 pub struct ContextManager {}
 
 impl ContextManager {
-    pub fn tracing_entry<F>(operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>, f: F)
+    pub fn tracing_entry<F>(operation_name: &str, extractor: Option<&dyn Extractable>, f: F)
         where F: FnOnce(Box<dyn Span>) -> Box<dyn Span> {
         CTX.with(|context| {
-            let span = context.borrow_mut().create_entry_span(operation_name, parent, extractor);
+            let span = context.borrow_mut().create_entry_span(operation_name, context.borrow().parent_span_id(), extractor);
             match span {
                 None => {}
                 Some(s) => {
                     let s = f(s);
+
                     let is_first_span = s.span_id() == 0;
                     context.borrow_mut().finish_span(s);
 
-                    if is_first_span { context.borrow_mut().finish() }
+                    if is_first_span {
+                        context.borrow_mut().finish();
+                    }
                 }
             }
         });
@@ -55,6 +58,7 @@ struct CurrentTracingContext {
 
 struct WorkingContext {
     context: Box<TracingContext>,
+    span_stack: Vec<i32>,
 }
 
 impl CurrentTracingContext {
@@ -63,7 +67,8 @@ impl CurrentTracingContext {
             option: match TracingContext::new(SKYWALKING_REPORTER.service_instance_id()) {
                 Some(tx) => {
                     Some(Box::new(WorkingContext {
-                        context: Box::new(tx)
+                        context: Box::new(tx),
+                        span_stack: Vec::new(),
                     }))
                 }
                 None => { None }
@@ -71,11 +76,13 @@ impl CurrentTracingContext {
         }
     }
 
-    fn create_entry_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) -> Option<Box<dyn Span>> {
+    fn create_entry_span(&mut self, operation_name: &str, parent_span_id: Option<i32>, extractor: Option<&dyn Extractable>) -> Option<Box<dyn Span>> {
         match self.option.borrow_mut() {
             None => { None }
             Some(wx) => {
-                Some(wx.context.create_entry_span(operation_name, parent, extractor))
+                let span = wx.context.create_entry_span(operation_name, parent_span_id, extractor);
+                wx.span_stack.push(span.span_id());
+                Some(span)
             }
         }
     }
@@ -85,11 +92,34 @@ impl CurrentTracingContext {
             None => {}
             Some(wx) => {
                 wx.context.finish_span(span);
+                wx.span_stack.pop();
             }
         };
     }
 
+    fn parent_span_id(&self) -> Option<i32> {
+        match self.option.borrow() {
+            None => { None }
+            Some(wx) => {
+                match wx.span_stack.last() {
+                    None => { None }
+                    Some(span_id) => { Some(span_id.clone()) }
+                }
+            }
+        }
+    }
+
     fn finish(&mut self) {
+        match self.option.borrow_mut() {
+            None => {}
+            Some(wx) => {
+                let tracingContext = &wx.context;
+                wx.span_stack.clear();
+
+                // TODO: Transfer tracingContext to protobuf
+            }
+        }
+        self.option = None;
     }
 }
 
@@ -101,7 +131,7 @@ mod context_tests {
 
     #[test]
     fn test_context_manager() {
-        ContextManager::tracing_entry("op1", None, None, |mut span| {
+        ContextManager::tracing_entry("op1", None, |mut span| {
             span.tag(Tag::new(String::from("tag1"), String::from("value1")));
             span
         });
diff --git a/core/src/skywalking/core/context.rs b/core/src/skywalking/core/context.rs
index 9c66abf..a9465b3 100644
--- a/core/src/skywalking/core/context.rs
+++ b/core/src/skywalking/core/context.rs
@@ -26,11 +26,11 @@ use crate::skywalking::core::span::TracingSpan;
 /// All new span belonging to this tracing context should be created through this context.
 pub trait Context {
     /// Create an entry span belonging this context
-    fn create_entry_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) -> Box<dyn Span>;
+    fn create_entry_span(&mut self, operation_name: &str, parent_span_id: Option<i32>, extractor: Option<&dyn Extractable>) -> Box<dyn Span>;
     /// Create an exit span belonging this context
-    fn create_exit_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, peer: &str, injector: Option<&dyn Injectable>) -> Box<dyn Span>;
+    fn create_exit_span(&mut self, operation_name: &str, parent_span_id: Option<i32>, peer: &str, injector: Option<&dyn Injectable>) -> Box<dyn Span>;
     /// Create an local span belonging this context
-    fn create_local_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>) -> Box<dyn Span>;
+    fn create_local_span(&mut self, operation_name: &str, parent_span_id: Option<i32>) -> Box<dyn Span>;
     /// Finish the given span. The span is only being accept if it belongs to this context.
     /// Return err if the span was created by another context.
     fn finish_span(&mut self, span: Box<dyn Span>);
@@ -100,10 +100,10 @@ impl TracingContext {
 
 /// Default implementation of Context
 impl Context for TracingContext {
-    fn create_entry_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) -> Box<dyn Span> {
-        let mut entry_span = TracingSpan::new_entry_span(operation_name, self.next_span_id(), match parent {
+    fn create_entry_span(&mut self, operation_name: &str, parent_span_id: Option<i32>, extractor: Option<&dyn Extractable>) -> Box<dyn Span> {
+        let mut entry_span = TracingSpan::new_entry_span(operation_name, self.next_span_id(), match parent_span_id {
             None => { -1 }
-            Some(s) => { s.span_id() }
+            Some(s) => { s }
         });
 
         if extractor.is_some() {
@@ -125,10 +125,10 @@ impl Context for TracingContext {
         Box::new(entry_span)
     }
 
-    fn create_exit_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, peer: &str, injector: Option<&dyn Injectable>) -> Box<dyn Span> {
-        let exit_span = TracingSpan::new_exit_span(operation_name, self.next_span_id(), match parent {
+    fn create_exit_span(&mut self, operation_name: &str, parent_span_id: Option<i32>, peer: &str, injector: Option<&dyn Injectable>) -> Box<dyn Span> {
+        let exit_span = TracingSpan::new_exit_span(operation_name, self.next_span_id(), match parent_span_id {
             None => { -1 }
-            Some(s) => { s.span_id() }
+            Some(s) => { s }
         }, peer);
 
         if injector.is_some() {
@@ -138,10 +138,10 @@ impl Context for TracingContext {
         Box::new(exit_span)
     }
 
-    fn create_local_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>) -> Box<dyn Span> {
-        Box::new(TracingSpan::new_local_span(operation_name, self.next_span_id(), match parent {
+    fn create_local_span(&mut self, operation_name: &str, parent_span_id: Option<i32>) -> Box<dyn Span> {
+        Box::new(TracingSpan::new_local_span(operation_name, self.next_span_id(), match parent_span_id {
             None => { -1 }
-            Some(s) => { s.span_id() }
+            Some(s) => { s }
         }))
     }
 
@@ -167,11 +167,11 @@ mod context_tests {
         let span1 = context.create_entry_span("op1", None, Some(&MockerHeader {}));
         {
             assert_eq!(span1.span_id(), 0);
-            let mut span2 = context.create_local_span("op2", Some(&span1));
+            let mut span2 = context.create_local_span("op2", Some(span1.span_id()));
             span2.tag(Tag::new(String::from("tag1"), String::from("value1")));
             {
                 assert_eq!(span2.span_id(), 1);
-                let span3 = context.create_exit_span("op3", Some(&span2), "127.0.0.1:8080", Some(&HeaderCarrier {}));
+                let span3 = context.create_exit_span("op3", Some(span2.span_id()), "127.0.0.1:8080", Some(&HeaderCarrier {}));
                 assert_eq!(span3.span_id(), 2);
 
                 context.finish_span(span3);