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/02 15:34:53 UTC

[skywalking-rust] branch context-manager updated: Finish m2.

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 c3d4d0d  Finish m2.
c3d4d0d is described below

commit c3d4d0d98a1cb8134412357dc2c790d1a4b0c2ef
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Mon Mar 2 23:34:40 2020 +0800

    Finish m2.
---
 core/src/skywalking/agent/context_manager.rs | 66 ++++++++++++++++++++--------
 core/src/skywalking/agent/reporter.rs        |  8 ++--
 core/src/skywalking/core/context.rs          | 10 ++---
 core/src/skywalking/core/context_listener.rs |  2 +-
 4 files changed, 57 insertions(+), 29 deletions(-)

diff --git a/core/src/skywalking/agent/context_manager.rs b/core/src/skywalking/agent/context_manager.rs
index b361474..3db7980 100644
--- a/core/src/skywalking/agent/context_manager.rs
+++ b/core/src/skywalking/agent/context_manager.rs
@@ -19,6 +19,7 @@ use lazy_static::lazy_static;
 
 use crate::skywalking::agent::reporter::Reporter;
 use crate::skywalking::core::{Context, ContextListener, Extractable, Span, TracingContext};
+use crate::skywalking::core::span::TracingSpan;
 
 thread_local!( static CTX: RefCell<CurrentTracingContext> = RefCell::new(CurrentTracingContext::new()));
 lazy_static! {
@@ -30,51 +31,80 @@ lazy_static! {
 pub struct ContextManager {}
 
 impl ContextManager {
-    pub fn createEntrySpan(operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) {
+    pub fn tracing_entry<F>(operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>, f: F)
+        where F: FnOnce(Box<dyn Span>) -> Box<dyn Span> {
         CTX.with(|context| {
-            context.borrow_mut().createEntrySpan(operation_name, parent, extractor);
+            let span = context.borrow_mut().create_entry_span(operation_name, parent, 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() }
+                }
+            }
         });
     }
 }
 
 struct CurrentTracingContext {
-    context: Option<Box<TracingContext>>,
-    active_spans: Box<Vec<Rc<Box<dyn Span>>>>,
+    option: Option<Box<WorkingContext>>,
+}
+
+struct WorkingContext {
+    context: Box<TracingContext>,
 }
 
 impl CurrentTracingContext {
     fn new() -> Self {
         CurrentTracingContext {
-            context: match TracingContext::new(SKYWALKING_REPORTER.service_instance_id()) {
-                Some(tx) => { Some(Box::new(tx)) }
+            option: match TracingContext::new(SKYWALKING_REPORTER.service_instance_id()) {
+                Some(tx) => {
+                    Some(Box::new(WorkingContext {
+                        context: Box::new(tx)
+                    }))
+                }
                 None => { None }
             },
-            active_spans: Box::new(Vec::new()),
         }
     }
 
-    pub fn createEntrySpan(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) -> Option<Rc<Box<dyn Span>>> {
-        match self.context.borrow_mut() {
+    fn create_entry_span(&mut self, operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) -> Option<Box<dyn Span>> {
+        match self.option.borrow_mut() {
             None => { None }
-            Some(txt) => {
-                let mut span = Rc::new(txt.create_entry_span(operation_name, parent, extractor));
-                let response = Rc::clone(&span);
-                self.active_spans.push(span);
-                Some(response)
+            Some(wx) => {
+                Some(wx.context.create_entry_span(operation_name, parent, extractor))
             }
         }
     }
+
+    fn finish_span(&mut self, span: Box<dyn Span>) {
+        match self.option.borrow_mut() {
+            None => {}
+            Some(wx) => {
+                wx.context.finish_span(span);
+            }
+        };
+    }
+
+    fn finish(&mut self) {
+    }
 }
 
 
 #[cfg(test)]
 mod context_tests {
     use crate::skywalking::agent::context_manager::*;
-    use crate::skywalking::core::{ContextListener, TracingContext};
+    use crate::skywalking::core::{ContextListener, Tag, TracingContext};
 
     #[test]
-    fn test_static_reporter() {
-//REPORTER.set_reporter(Box::new(MockReporter {}));
+    fn test_context_manager() {
+        ContextManager::tracing_entry("op1", None, None, |mut span| {
+            span.tag(Tag::new(String::from("tag1"), String::from("value1")));
+            span
+        });
     }
 
     struct MockReporter {}
@@ -84,6 +114,6 @@ mod context_tests {
             Some(1)
         }
 
-        fn report_trace(&self, finished_context: TracingContext) {}
+        fn report_trace(&self, finished_context: Box<TracingContext>) {}
     }
 }
diff --git a/core/src/skywalking/agent/reporter.rs b/core/src/skywalking/agent/reporter.rs
index a07b521..cf26f3f 100644
--- a/core/src/skywalking/agent/reporter.rs
+++ b/core/src/skywalking/agent/reporter.rs
@@ -15,16 +15,14 @@
 
 use crate::skywalking::core::{ContextListener, TracingContext};
 
-pub struct Reporter {
-
-}
+pub struct Reporter {}
 
 impl ContextListener for Reporter {
     fn service_instance_id(&self) -> Option<i32> {
-        unimplemented!()
+        Some(1)
     }
 
-    fn report_trace(&self, finished_context: TracingContext) {
+    fn report_trace(&self, finished_context: Box<TracingContext>) {
         unimplemented!()
     }
 }
diff --git a/core/src/skywalking/core/context.rs b/core/src/skywalking/core/context.rs
index 641d9a5..9c66abf 100644
--- a/core/src/skywalking/core/context.rs
+++ b/core/src/skywalking/core/context.rs
@@ -180,7 +180,7 @@ mod context_tests {
         }
         context.finish_span(span1);
 
-        reporter.report_trace(context);
+        reporter.report_trace(Box::new(context));
         // context has moved into reporter. Can't be used again.
 
         let received_context = reporter.recv.recv().unwrap();
@@ -190,13 +190,13 @@ mod context_tests {
 
     #[test]
     fn test_no_context() {
-        let context = TracingContext::new(Some(1));
+        let context = TracingContext::new(None);
         assert_eq!(context.is_none(), true);
     }
 
     struct MockReporter {
-        sender: Box<Sender<TracingContext>>,
-        recv: Box<Receiver<TracingContext>>,
+        sender: Box<Sender<Box<TracingContext>>>,
+        recv: Box<Receiver<Box<TracingContext>>>,
     }
 
     impl MockReporter {
@@ -214,7 +214,7 @@ mod context_tests {
             Some(1)
         }
 
-        fn report_trace(&self, finished_context: TracingContext) {
+        fn report_trace(&self, finished_context: Box<TracingContext>) {
             self.sender.send(finished_context);
         }
     }
diff --git a/core/src/skywalking/core/context_listener.rs b/core/src/skywalking/core/context_listener.rs
index 4e85787..9cb5842 100644
--- a/core/src/skywalking/core/context_listener.rs
+++ b/core/src/skywalking/core/context_listener.rs
@@ -26,5 +26,5 @@ pub trait ContextListener {
 
     /// Move the finished and inactive context to the reporter.
     /// The reporter should use async way to transport the data to the backend through HTTP, gRPC or SkyWalking forwarder.
-    fn report_trace(&self, finished_context: TracingContext);
+    fn report_trace(&self, finished_context: Box<TracingContext>);
 }