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 09:56:57 UTC

[skywalking-rust] 01/01: m1 only.

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

commit 66a896e547c191dd0b201e333ecb915566a8d369
Author: Wu Sheng <wu...@foxmail.com>
AuthorDate: Mon Mar 2 17:56:41 2020 +0800

    m1 only.
---
 Cargo.lock                                         |  7 ++
 Cargo.toml                                         | 17 ++++-
 core/Cargo.toml                                    | 27 +++++++
 {tracing-core => core}/src/lib.rs                  | 20 +----
 core/src/skywalking/agent/context_manager.rs       | 89 ++++++++++++++++++++++
 .../src/lib.rs => core/src/skywalking/agent/mod.rs | 21 +----
 .../src/skywalking/agent/reporter.rs               | 37 ++++-----
 .../src => core/src/skywalking/core}/context.rs    | 36 +++------
 .../src/skywalking/core}/context_carrier.rs        |  0
 .../src/skywalking/core}/context_listener.rs       |  2 +-
 .../src => core/src/skywalking/core}/id.rs         |  4 +-
 .../src => core/src/skywalking/core}/log.rs        |  2 +-
 .../src/lib.rs => core/src/skywalking/core/mod.rs  |  3 +-
 .../src/skywalking/core}/segment_ref.rs            |  8 +-
 .../src => core/src/skywalking/core}/span.rs       | 12 +--
 .../src => core/src/skywalking/core}/tag.rs        |  2 +-
 .../src/lib.rs => core/src/skywalking/mod.rs       | 22 +-----
 tracing-core/Cargo.toml                            | 11 ---
 18 files changed, 192 insertions(+), 128 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock
index dee751b..e99ac24 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -33,6 +33,12 @@ dependencies = [
 ]
 
 [[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
 name = "libc"
 version = "0.2.67"
 source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -90,6 +96,7 @@ name = "skywalking-core"
 version = "0.1.0"
 dependencies = [
  "base64",
+ "lazy_static",
  "rand",
 ]
 
diff --git a/Cargo.toml b/Cargo.toml
index 007e2b9..629476f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,5 +1,20 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.0
+#
+# Unless 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.
+
 [workspace]
 
 members = [
-    "tracing-core",
+    "core",
 ]
\ No newline at end of file
diff --git a/core/Cargo.toml b/core/Cargo.toml
new file mode 100644
index 0000000..c187379
--- /dev/null
+++ b/core/Cargo.toml
@@ -0,0 +1,27 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You 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.0
+#
+# Unless 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.
+
+[package]
+name = "skywalking-core"
+version = "0.1.0"
+authors = ["Apache Software Foundation (ASF)"]
+edition = "2018"
+description = "SkyWalking tracing core APIs. Provide the way to build SkyWalking native format traces/spans and propagated context."
+license = "Apache 2.0"
+
+[dependencies]
+rand = "0.7.3"
+base64 = "0.11.0"
+lazy_static = "1.4.0"
\ No newline at end of file
diff --git a/tracing-core/src/lib.rs b/core/src/lib.rs
similarity index 65%
copy from tracing-core/src/lib.rs
copy to core/src/lib.rs
index 7d9a2d1..f94e272 100644
--- a/tracing-core/src/lib.rs
+++ b/core/src/lib.rs
@@ -13,23 +13,5 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-pub use context::Context;
-pub use context::TracingContext;
-pub use context_carrier::Extractable;
-pub use context_carrier::Injectable;
-pub use context_listener::ContextListener;
-pub use id::ID;
-pub use log::EventField;
-pub use log::LogEvent;
-pub use span::Span;
-pub use tag::Tag;
-
-pub mod span;
-pub mod context;
-pub mod tag;
-pub mod id;
-pub mod context_listener;
-pub mod log;
-pub mod context_carrier;
-pub mod segment_ref;
+pub mod skywalking;
 
diff --git a/core/src/skywalking/agent/context_manager.rs b/core/src/skywalking/agent/context_manager.rs
new file mode 100644
index 0000000..b361474
--- /dev/null
+++ b/core/src/skywalking/agent/context_manager.rs
@@ -0,0 +1,89 @@
+// this work for additional information regarding copyright ownership.
+// The ASF licenses this file to You 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.0
+//
+// Unless 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.
+
+use std::borrow::BorrowMut;
+use std::cell::RefCell;
+use std::rc::Rc;
+
+use lazy_static::lazy_static;
+
+use crate::skywalking::agent::reporter::Reporter;
+use crate::skywalking::core::{Context, ContextListener, Extractable, Span, TracingContext};
+
+thread_local!( static CTX: RefCell<CurrentTracingContext> = RefCell::new(CurrentTracingContext::new()));
+lazy_static! {
+    static ref SKYWALKING_REPORTER : Reporter = {
+        Reporter::new()
+    };
+}
+
+pub struct ContextManager {}
+
+impl ContextManager {
+    pub fn createEntrySpan(operation_name: &str, parent: Option<&Box<dyn Span>>, extractor: Option<&dyn Extractable>) {
+        CTX.with(|context| {
+            context.borrow_mut().createEntrySpan(operation_name, parent, extractor);
+        });
+    }
+}
+
+struct CurrentTracingContext {
+    context: Option<Box<TracingContext>>,
+    active_spans: Box<Vec<Rc<Box<dyn Span>>>>,
+}
+
+impl CurrentTracingContext {
+    fn new() -> Self {
+        CurrentTracingContext {
+            context: match TracingContext::new(SKYWALKING_REPORTER.service_instance_id()) {
+                Some(tx) => { Some(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() {
+            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)
+            }
+        }
+    }
+}
+
+
+#[cfg(test)]
+mod context_tests {
+    use crate::skywalking::agent::context_manager::*;
+    use crate::skywalking::core::{ContextListener, TracingContext};
+
+    #[test]
+    fn test_static_reporter() {
+//REPORTER.set_reporter(Box::new(MockReporter {}));
+    }
+
+    struct MockReporter {}
+
+    impl ContextListener for MockReporter {
+        fn service_instance_id(&self) -> Option<i32> {
+            Some(1)
+        }
+
+        fn report_trace(&self, finished_context: TracingContext) {}
+    }
+}
diff --git a/tracing-core/src/lib.rs b/core/src/skywalking/agent/mod.rs
similarity index 65%
copy from tracing-core/src/lib.rs
copy to core/src/skywalking/agent/mod.rs
index 7d9a2d1..7cf36a6 100644
--- a/tracing-core/src/lib.rs
+++ b/core/src/skywalking/agent/mod.rs
@@ -13,23 +13,8 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-pub use context::Context;
-pub use context::TracingContext;
-pub use context_carrier::Extractable;
-pub use context_carrier::Injectable;
-pub use context_listener::ContextListener;
-pub use id::ID;
-pub use log::EventField;
-pub use log::LogEvent;
-pub use span::Span;
-pub use tag::Tag;
+pub use context_manager::ContextManager;
 
-pub mod span;
-pub mod context;
-pub mod tag;
-pub mod id;
-pub mod context_listener;
-pub mod log;
-pub mod context_carrier;
-pub mod segment_ref;
+pub mod context_manager;
+pub mod reporter;
 
diff --git a/tracing-core/src/lib.rs b/core/src/skywalking/agent/reporter.rs
similarity index 65%
copy from tracing-core/src/lib.rs
copy to core/src/skywalking/agent/reporter.rs
index 7d9a2d1..a07b521 100644
--- a/tracing-core/src/lib.rs
+++ b/core/src/skywalking/agent/reporter.rs
@@ -13,23 +13,24 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-pub use context::Context;
-pub use context::TracingContext;
-pub use context_carrier::Extractable;
-pub use context_carrier::Injectable;
-pub use context_listener::ContextListener;
-pub use id::ID;
-pub use log::EventField;
-pub use log::LogEvent;
-pub use span::Span;
-pub use tag::Tag;
+use crate::skywalking::core::{ContextListener, TracingContext};
 
-pub mod span;
-pub mod context;
-pub mod tag;
-pub mod id;
-pub mod context_listener;
-pub mod log;
-pub mod context_carrier;
-pub mod segment_ref;
+pub struct Reporter {
 
+}
+
+impl ContextListener for Reporter {
+    fn service_instance_id(&self) -> Option<i32> {
+        unimplemented!()
+    }
+
+    fn report_trace(&self, finished_context: TracingContext) {
+        unimplemented!()
+    }
+}
+
+impl Reporter {
+    pub fn new() -> Self {
+        Reporter {}
+    }
+}
\ No newline at end of file
diff --git a/tracing-core/src/context.rs b/core/src/skywalking/core/context.rs
similarity index 88%
rename from tracing-core/src/context.rs
rename to core/src/skywalking/core/context.rs
index 47a2ed8..641d9a5 100644
--- a/tracing-core/src/context.rs
+++ b/core/src/skywalking/core/context.rs
@@ -15,11 +15,12 @@
 
 use base64::{decode, encode};
 
-use crate::{ContextListener, ID, Span};
-use crate::context_carrier::{Extractable, Injectable};
-use crate::id::IDGenerator;
-use crate::segment_ref::SegmentRef;
-use crate::span::TracingSpan;
+use crate::skywalking::agent::reporter::Reporter;
+use crate::skywalking::core::{ID, Span};
+use crate::skywalking::core::context_carrier::{Extractable, Injectable};
+use crate::skywalking::core::id::IDGenerator;
+use crate::skywalking::core::segment_ref::SegmentRef;
+use crate::skywalking::core::span::TracingSpan;
 
 /// Context represents the context of a tracing process.
 /// All new span belonging to this tracing context should be created through this context.
@@ -51,9 +52,8 @@ pub struct TracingContext {
 
 impl TracingContext {
     /// Create a new instance
-    pub fn new(reporter: &dyn ContextListener) -> Option<TracingContext> {
-        let instance_id = reporter.service_instance_id();
-        match instance_id {
+    pub fn new(service_instance_id: Option<i32>) -> Option<TracingContext> {
+        match service_instance_id {
             None => { None }
             Some(id) => {
                 Some(TracingContext {
@@ -158,12 +158,12 @@ mod context_tests {
     use std::sync::mpsc;
     use std::sync::mpsc::{Receiver, Sender};
 
-    use crate::{Context, ContextListener, Extractable, ID, Injectable, Tag, TracingContext};
+    use crate::skywalking::core::{Context, ContextListener, Extractable, ID, Injectable, Tag, TracingContext};
 
     #[test]
     fn test_context_stack() {
         let reporter = MockReporter::new();
-        let mut context = TracingContext::new(&reporter).unwrap();
+        let mut context = TracingContext::new(reporter.service_instance_id()).unwrap();
         let span1 = context.create_entry_span("op1", None, Some(&MockerHeader {}));
         {
             assert_eq!(span1.span_id(), 0);
@@ -171,7 +171,7 @@ mod context_tests {
             span2.tag(Tag::new(String::from("tag1"), String::from("value1")));
             {
                 assert_eq!(span2.span_id(), 1);
-                let mut span3 = context.create_exit_span("op3", Some(&span2), "127.0.0.1:8080", Some(&HeaderCarrier {}));
+                let span3 = context.create_exit_span("op3", Some(&span2), "127.0.0.1:8080", Some(&HeaderCarrier {}));
                 assert_eq!(span3.span_id(), 2);
 
                 context.finish_span(span3);
@@ -190,7 +190,7 @@ mod context_tests {
 
     #[test]
     fn test_no_context() {
-        let context = TracingContext::new(&MockRegisterPending {});
+        let context = TracingContext::new(Some(1));
         assert_eq!(context.is_none(), true);
     }
 
@@ -235,16 +235,4 @@ mod context_tests {
             assert_eq!(value.len() > 0, true);
         }
     }
-
-    struct MockRegisterPending {}
-
-    impl ContextListener for MockRegisterPending {
-        fn service_instance_id(&self) -> Option<i32> {
-            None
-        }
-
-        fn report_trace(&self, finished_context: TracingContext) {
-            unimplemented!()
-        }
-    }
 }
\ No newline at end of file
diff --git a/tracing-core/src/context_carrier.rs b/core/src/skywalking/core/context_carrier.rs
similarity index 100%
rename from tracing-core/src/context_carrier.rs
rename to core/src/skywalking/core/context_carrier.rs
diff --git a/tracing-core/src/context_listener.rs b/core/src/skywalking/core/context_listener.rs
similarity index 96%
rename from tracing-core/src/context_listener.rs
rename to core/src/skywalking/core/context_listener.rs
index 9eab7f3..4e85787 100644
--- a/tracing-core/src/context_listener.rs
+++ b/core/src/skywalking/core/context_listener.rs
@@ -13,7 +13,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-use crate::TracingContext;
+use crate::skywalking::core::TracingContext;
 
 ///Report bridge defines the traits for the skywalking-report
 
diff --git a/tracing-core/src/id.rs b/core/src/skywalking/core/id.rs
similarity index 97%
rename from tracing-core/src/id.rs
rename to core/src/skywalking/core/id.rs
index b276986..384963a 100644
--- a/tracing-core/src/id.rs
+++ b/core/src/skywalking/core/id.rs
@@ -85,8 +85,8 @@ impl ToString for ID {
 
 #[cfg(test)]
 mod id_tests {
-    use crate::ID;
-    use crate::id::IDGenerator;
+    use crate::skywalking::core::ID;
+    use crate::skywalking::core::id::IDGenerator;
 
     #[test]
     fn test_id_generator() {
diff --git a/tracing-core/src/log.rs b/core/src/skywalking/core/log.rs
similarity index 97%
rename from tracing-core/src/log.rs
rename to core/src/skywalking/core/log.rs
index a8d3c9a..e0b81f1 100644
--- a/tracing-core/src/log.rs
+++ b/core/src/skywalking/core/log.rs
@@ -47,7 +47,7 @@ impl EventField {
 
 #[cfg(test)]
 mod log_tests {
-    use crate::log::{EventField, LogEvent};
+    use crate::skywalking::core::log::{EventField, LogEvent};
 
     #[test]
     fn test_log_new() {
diff --git a/tracing-core/src/lib.rs b/core/src/skywalking/core/mod.rs
similarity index 98%
copy from tracing-core/src/lib.rs
copy to core/src/skywalking/core/mod.rs
index 7d9a2d1..7715556 100644
--- a/tracing-core/src/lib.rs
+++ b/core/src/skywalking/core/mod.rs
@@ -31,5 +31,4 @@ pub mod id;
 pub mod context_listener;
 pub mod log;
 pub mod context_carrier;
-pub mod segment_ref;
-
+pub mod segment_ref;
\ No newline at end of file
diff --git a/tracing-core/src/segment_ref.rs b/core/src/skywalking/core/segment_ref.rs
similarity index 97%
rename from tracing-core/src/segment_ref.rs
rename to core/src/skywalking/core/segment_ref.rs
index eca2be1..421da20 100644
--- a/tracing-core/src/segment_ref.rs
+++ b/core/src/skywalking/core/segment_ref.rs
@@ -15,8 +15,8 @@
 
 use std::os::raw::c_long;
 
-use crate::{ID, Span, TracingContext};
-use crate::segment_ref::SegmentRefType::CROSS_PROCESS;
+use crate::skywalking::core::{ID, Span, TracingContext};
+use crate::skywalking::core::segment_ref::SegmentRefType::CROSS_PROCESS;
 
 #[derive(Clone, Hash)]
 pub struct SegmentRef {
@@ -214,8 +214,8 @@ impl SegmentRef {
 
 #[cfg(test)]
 mod segment_ref_tests {
-    use crate::ID;
-    use crate::segment_ref::SegmentRef;
+    use crate::skywalking::core::ID;
+    use crate::skywalking::core::segment_ref::SegmentRef;
 
     #[test]
     fn test_deserialize_context_carrier() {
diff --git a/tracing-core/src/span.rs b/core/src/skywalking/core/span.rs
similarity index 97%
rename from tracing-core/src/span.rs
rename to core/src/skywalking/core/span.rs
index a51c8eb..9842dea 100644
--- a/tracing-core/src/span.rs
+++ b/core/src/skywalking/core/span.rs
@@ -15,9 +15,9 @@
 
 use std::time::SystemTime;
 
-use crate::log::LogEvent;
-use crate::segment_ref::SegmentRef;
-use crate::Tag;
+use crate::skywalking::core::log::LogEvent;
+use crate::skywalking::core::segment_ref::SegmentRef;
+use crate::skywalking::core::Tag;
 
 /// Span is one of the tracing concept, representing a time duration.
 ///Span is an important and common concept in distributed tracing system. Learn Span from Google Dapper Paper.
@@ -209,9 +209,9 @@ impl Span for TracingSpan {
 mod span_tests {
     use std::time::SystemTime;
 
-    use crate::log::{EventField, LogEvent};
-    use crate::span::*;
-    use crate::Tag;
+    use crate::skywalking::core::log::{EventField, LogEvent};
+    use crate::skywalking::core::span::*;
+    use crate::skywalking::core::Tag;
 
     #[test]
     fn test_span_new() {
diff --git a/tracing-core/src/tag.rs b/core/src/skywalking/core/tag.rs
similarity index 97%
rename from tracing-core/src/tag.rs
rename to core/src/skywalking/core/tag.rs
index aab0e0c..80bbad9 100644
--- a/tracing-core/src/tag.rs
+++ b/core/src/skywalking/core/tag.rs
@@ -41,7 +41,7 @@ impl Tag {
 
 #[cfg(test)]
 mod tag_tests {
-    use crate::Tag;
+    use crate::skywalking::core::Tag;
 
     #[test]
     fn test_tag_new() {
diff --git a/tracing-core/src/lib.rs b/core/src/skywalking/mod.rs
similarity index 65%
rename from tracing-core/src/lib.rs
rename to core/src/skywalking/mod.rs
index 7d9a2d1..08730c9 100644
--- a/tracing-core/src/lib.rs
+++ b/core/src/skywalking/mod.rs
@@ -13,23 +13,5 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-pub use context::Context;
-pub use context::TracingContext;
-pub use context_carrier::Extractable;
-pub use context_carrier::Injectable;
-pub use context_listener::ContextListener;
-pub use id::ID;
-pub use log::EventField;
-pub use log::LogEvent;
-pub use span::Span;
-pub use tag::Tag;
-
-pub mod span;
-pub mod context;
-pub mod tag;
-pub mod id;
-pub mod context_listener;
-pub mod log;
-pub mod context_carrier;
-pub mod segment_ref;
-
+pub mod core;
+pub mod agent;
diff --git a/tracing-core/Cargo.toml b/tracing-core/Cargo.toml
deleted file mode 100644
index 821e4ef..0000000
--- a/tracing-core/Cargo.toml
+++ /dev/null
@@ -1,11 +0,0 @@
-[package]
-name = "skywalking-core"
-version = "0.1.0"
-authors = ["Apache Software Foundation (ASF)"]
-edition = "2018"
-description = "SkyWalking tracing core APIs. Provide the way to build SkyWalking native format traces/spans and propagated context."
-license = "Apache 2.0"
-
-[dependencies]
-rand = "0.7.3"
-base64 = "0.11.0"
\ No newline at end of file