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 2023/05/19 04:51:42 UTC
[skywalking-rust] branch master updated: Obtain Span object without intermediary. (#57)
This is an automated email from the ASF dual-hosted git repository.
wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking-rust.git
The following commit(s) were added to refs/heads/master by this push:
new 9ce27ed Obtain Span object without intermediary. (#57)
9ce27ed is described below
commit 9ce27ed69b0950cab6be304a4d6219c615b05ec1
Author: jmjoy <jm...@apache.org>
AuthorDate: Fri May 19 12:51:36 2023 +0800
Obtain Span object without intermediary. (#57)
---
.github/workflows/ci.yaml | 16 ++++-----
.github/workflows/codecov.yaml | 2 +-
Cargo.toml | 2 +-
rust-toolchain.toml | 4 ++-
src/trace/span.rs | 76 ++++++++++-----------------------------
src/trace/trace_context.rs | 81 +++++++++++++++++++++++++++---------------
tests/trace_context.rs | 6 ++--
7 files changed, 88 insertions(+), 99 deletions(-)
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
index 7d891aa..10d189e 100644
--- a/.github/workflows/ci.yaml
+++ b/.github/workflows/ci.yaml
@@ -50,9 +50,9 @@ jobs:
with:
submodules: 'recursive'
- name: Install Cargo nightly
- run: rustup toolchain install nightly
+ run: rustup toolchain install nightly-2023-05-19
- name: Cargo minimal versions
- run: cargo +nightly -Z minimal-versions generate-lockfile
+ run: cargo +nightly-2023-05-19 -Z minimal-versions generate-lockfile
- name: Check License Header
uses: apache/skywalking-eyes/header/@d299844e334855087f18ae1fe3c81ae8d22bc282
with:
@@ -79,9 +79,9 @@ jobs:
with:
submodules: 'recursive'
- name: Install Rust toolchain
- run: rustup toolchain install nightly-2022-07-30 --component rustfmt
+ run: rustup toolchain install nightly-2023-05-19 --component rustfmt
- name: Run format
- run: cargo +nightly-2022-07-30 fmt --all -- --check
+ run: cargo +nightly-2023-05-19 fmt --all -- --check
check-and-test:
strategy:
@@ -98,7 +98,7 @@ jobs:
with:
submodules: 'recursive'
- name: Install protoc
- run: sudo apt-get install -y protobuf-compiler=3.6.1.3-2ubuntu5
+ run: sudo apt-get install -y protobuf-compiler
if: ${{ matrix.features != '--features vendored' }}
- name: Install Rust toolchain
run: rustup toolchain install 1.63.0 --component clippy
@@ -116,8 +116,8 @@ jobs:
with:
submodules: 'recursive'
- name: Install protoc
- run: sudo apt-get install -y protobuf-compiler=3.6.1.3-2ubuntu5
+ run: sudo apt-get install -y protobuf-compiler
- name: Install Rust toolchain
- run: rustup toolchain install nightly-2022-07-30
+ run: rustup toolchain install nightly-2023-05-19
- name: Run docs
- run: cargo +nightly-2022-07-30 rustdoc --release --all-features -- --cfg docsrs
+ run: cargo +nightly-2023-05-19 rustdoc --release --all-features -- --cfg docsrs
diff --git a/.github/workflows/codecov.yaml b/.github/workflows/codecov.yaml
index 791d418..df52e4f 100644
--- a/.github/workflows/codecov.yaml
+++ b/.github/workflows/codecov.yaml
@@ -31,7 +31,7 @@ jobs:
with:
submodules: true
- name: Install protoc
- run: sudo apt-get install -y protobuf-compiler=3.6.1.3-2ubuntu5
+ run: sudo apt-get install -y protobuf-compiler
- uses: actions-rs/toolchain@v1
with:
profile: minimal
diff --git a/Cargo.toml b/Cargo.toml
index ac3dd99..12d9a3c 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -31,7 +31,7 @@ description = "Apache SkyWalking Rust Agent"
license = "Apache-2.0"
homepage = "https://skywalking.apache.org/"
repository = "https://github.com/apache/skywalking-rust"
-rust-version = "1.59"
+rust-version = "1.65"
[features]
management = ["hostname", "systemstat"]
diff --git a/rust-toolchain.toml b/rust-toolchain.toml
index 374f8d1..1af08b0 100644
--- a/rust-toolchain.toml
+++ b/rust-toolchain.toml
@@ -15,6 +15,8 @@
# specific language governing permissions and limitations
# under the License.
#
+
[toolchain]
-channel = "1.63.0"
+channel = "1.65"
components = ["rustfmt", "clippy"]
+
diff --git a/src/trace/span.rs b/src/trace/span.rs
index 60371f0..100a528 100644
--- a/src/trace/span.rs
+++ b/src/trace/span.rs
@@ -22,42 +22,7 @@ use crate::{
skywalking_proto::v3::{SpanLayer, SpanObject, SpanType},
trace::trace_context::SpanStack,
};
-use parking_lot::{
- MappedRwLockReadGuard, MappedRwLockWriteGuard, RwLockReadGuard, RwLockWriteGuard,
-};
-use std::{
- fmt::Formatter,
- ops::{Deref, DerefMut},
- sync::Arc,
-};
-
-/// Wrapper of [SpanObject] immutable reference.
-pub struct SpanObjectRef<'a>(pub(crate) MappedRwLockReadGuard<'a, SpanObject>);
-
-impl Deref for SpanObjectRef<'_> {
- type Target = SpanObject;
-
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
-
-/// Wrapper of [SpanObject] mutable reference.
-pub struct SpanObjectMut<'a>(MappedRwLockWriteGuard<'a, SpanObject>);
-
-impl Deref for SpanObjectMut<'_> {
- type Target = SpanObject;
-
- fn deref(&self) -> &Self::Target {
- &self.0
- }
-}
-
-impl DerefMut for SpanObjectMut<'_> {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.0
- }
-}
+use std::{fmt::Formatter, mem::take, sync::Arc};
/// Span is a concept that represents trace information for a single RPC.
/// The Rust SDK supports Entry Span to represent inbound to a service
@@ -97,24 +62,18 @@ impl DerefMut for SpanObjectMut<'_> {
#[must_use = "assign a variable name to guard the span not be dropped immediately."]
pub struct Span {
index: usize,
+ obj: Option<SpanObject>,
stack: Arc<SpanStack>,
}
impl std::fmt::Debug for Span {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
- let span_object: SpanObject;
f.debug_struct("Span")
.field(
"data",
- match self.stack.active.try_read() {
- Some(spans) => match spans.get(self.index) {
- Some(span) => {
- span_object = span.clone();
- &span_object
- }
- None => &"<hanged>",
- },
- None => &"<locked>",
+ match self.obj {
+ Some(ref obj) => obj,
+ None => &"<none>",
},
)
.finish()
@@ -124,8 +83,12 @@ impl std::fmt::Debug for Span {
const SKYWALKING_RUST_COMPONENT_ID: i32 = 11000;
impl Span {
- pub(crate) fn new(index: usize, stack: Arc<SpanStack>) -> Self {
- Self { index, stack }
+ pub(crate) fn new(index: usize, obj: SpanObject, stack: Arc<SpanStack>) -> Self {
+ Self {
+ index,
+ obj: Some(obj),
+ stack,
+ }
}
#[allow(clippy::too_many_arguments)]
@@ -153,17 +116,15 @@ impl Span {
}
/// Get immutable span object reference.
- pub fn span_object(&self) -> SpanObjectRef<'_> {
- SpanObjectRef(RwLockReadGuard::map(self.stack.active(), |stack| {
- &stack[self.index]
- }))
+ #[inline]
+ pub fn span_object(&self) -> &SpanObject {
+ self.obj.as_ref().unwrap()
}
/// Mutable with inner span object.
- pub fn span_object_mut(&mut self) -> SpanObjectMut<'_> {
- SpanObjectMut(RwLockWriteGuard::map(self.stack.active_mut(), |stack| {
- &mut stack[self.index]
- }))
+ #[inline]
+ pub fn span_object_mut(&mut self) -> &mut SpanObject {
+ self.obj.as_mut().unwrap()
}
/// Get span id.
@@ -191,7 +152,8 @@ impl Drop for Span {
/// Set the end time as current time, pop from context active span stack,
/// and push to context spans.
fn drop(&mut self) {
- self.stack.finalize_span(self.index);
+ self.stack
+ .finalize_span(self.index, take(&mut self.obj).unwrap());
}
}
diff --git a/src/trace/trace_context.rs b/src/trace/trace_context.rs
index 95641ea..10b8dbe 100644
--- a/src/trace/trace_context.rs
+++ b/src/trace/trace_context.rs
@@ -29,7 +29,7 @@ use crate::{
},
trace::{
propagation::context::PropagationContext,
- span::{Span, SpanObjectRef},
+ span::Span,
tracer::{Tracer, WeakTracer},
},
};
@@ -38,14 +38,30 @@ use parking_lot::{
};
use std::{fmt::Formatter, mem::take, sync::Arc};
+pub(crate) struct ActiveSpan {
+ span_id: i32,
+
+ /// For [TracingContext::continued] used.
+ r#ref: Option<SegmentReference>,
+}
+
+impl ActiveSpan {
+ fn new(span_id: i32) -> Self {
+ Self {
+ span_id,
+ r#ref: None,
+ }
+ }
+}
+
#[derive(Default)]
pub(crate) struct SpanStack {
- // TODO Switch to use `try_rwlock` instead of `RwLock` for better performance.
pub(crate) finalized: RwLock<Vec<SpanObject>>,
- pub(crate) active: RwLock<Vec<SpanObject>>,
+ pub(crate) active: RwLock<Vec<ActiveSpan>>,
}
impl SpanStack {
+ #[allow(dead_code)]
pub(crate) fn finalized(&self) -> RwLockReadGuard<'_, Vec<SpanObject>> {
self.finalized.try_read().expect(LOCK_MSG)
}
@@ -54,15 +70,15 @@ impl SpanStack {
self.finalized.try_write().expect(LOCK_MSG)
}
- pub(crate) fn active(&self) -> RwLockReadGuard<'_, Vec<SpanObject>> {
+ pub(crate) fn active(&self) -> RwLockReadGuard<'_, Vec<ActiveSpan>> {
self.active.try_read().expect(LOCK_MSG)
}
- pub(crate) fn active_mut(&self) -> RwLockWriteGuard<'_, Vec<SpanObject>> {
+ pub(crate) fn active_mut(&self) -> RwLockWriteGuard<'_, Vec<ActiveSpan>> {
self.active.try_write().expect(LOCK_MSG)
}
- fn pop_active(&self, index: usize) -> Option<SpanObject> {
+ fn pop_active(&self, index: usize) -> Option<ActiveSpan> {
let mut stack = self.active_mut();
if stack.len() > index + 1 {
None
@@ -72,14 +88,21 @@ impl SpanStack {
}
/// Close span. We can't use closed span after finalize called.
- pub(crate) fn finalize_span(&self, index: usize) {
+ pub(crate) fn finalize_span(&self, index: usize, mut obj: SpanObject) {
let span = self.pop_active(index);
- if let Some(mut span) = span {
- span.end_time = fetch_time(TimePeriod::End);
- self.finalized_mut().push(span);
- } else {
- panic!("Finalize span isn't the active span");
+ if let Some(span) = span {
+ if span.span_id == obj.span_id {
+ obj.end_time = fetch_time(TimePeriod::End);
+
+ if let Some(r#ref) = span.r#ref {
+ obj.refs.push(r#ref);
+ }
+
+ self.finalized_mut().push(obj);
+ return;
+ }
}
+ panic!("Finalize span isn't the active span");
}
}
@@ -175,30 +198,32 @@ impl TracingContext {
span_id
}
- /// Get the last finalized span.
- pub fn last_span(&self) -> Option<SpanObjectRef<'_>> {
+ /// Clone the last finalized span.
+ #[doc(hidden)]
+ pub fn last_span(&self) -> Option<SpanObject> {
RwLockReadGuard::try_map(self.span_stack.finalized(), |spans| spans.last())
.ok()
- .map(SpanObjectRef)
+ .as_deref()
+ .cloned()
}
fn spans_mut(&mut self) -> RwLockWriteGuard<'_, Vec<SpanObject>> {
self.span_stack.finalized.try_write().expect(LOCK_MSG)
}
- pub(crate) fn active_span_stack(&self) -> RwLockReadGuard<'_, Vec<SpanObject>> {
+ pub(crate) fn active_span_stack(&self) -> RwLockReadGuard<'_, Vec<ActiveSpan>> {
self.span_stack.active()
}
- pub(crate) fn active_span_stack_mut(&mut self) -> RwLockWriteGuard<'_, Vec<SpanObject>> {
+ pub(crate) fn active_span_stack_mut(&mut self) -> RwLockWriteGuard<'_, Vec<ActiveSpan>> {
self.span_stack.active_mut()
}
- pub(crate) fn active_span(&self) -> Option<MappedRwLockReadGuard<'_, SpanObject>> {
+ pub(crate) fn active_span(&self) -> Option<MappedRwLockReadGuard<'_, ActiveSpan>> {
RwLockReadGuard::try_map(self.active_span_stack(), |stack| stack.last()).ok()
}
- pub(crate) fn active_span_mut(&mut self) -> Option<MappedRwLockWriteGuard<'_, SpanObject>> {
+ pub(crate) fn active_span_mut(&mut self) -> Option<MappedRwLockWriteGuard<'_, ActiveSpan>> {
RwLockWriteGuard::try_map(self.active_span_stack_mut(), |stack| stack.last_mut()).ok()
}
@@ -219,8 +244,8 @@ impl TracingContext {
false,
);
- let index = self.push_active_span(span);
- Span::new(index, self.span_stack.clone())
+ let index = self.push_active_span(&span);
+ Span::new(index, span, self.span_stack.clone())
}
/// Create a new entry span, which is an initiator of collection of spans.
@@ -272,8 +297,8 @@ impl TracingContext {
false,
);
- let index = self.push_active_span(span);
- Span::new(index, self.span_stack.clone())
+ let index = self.push_active_span(&span);
+ Span::new(index, span, self.span_stack.clone())
}
/// Create a new local span.
@@ -296,8 +321,8 @@ impl TracingContext {
false,
);
- let index = self.push_active_span(span);
- Span::new(index, self.span_stack.clone())
+ let index = self.push_active_span(&span);
+ Span::new(index, span, self.span_stack.clone())
}
/// Capture a snapshot for cross-thread propagation.
@@ -329,7 +354,7 @@ impl TracingContext {
};
if let Some(mut span) = self.active_span_mut() {
- span.refs.push(segment_ref);
+ span.r#ref = Some(segment_ref);
}
}
}
@@ -360,10 +385,10 @@ impl TracingContext {
self.active_span().map(|span| span.span_id)
}
- fn push_active_span(&mut self, span: SpanObject) -> usize {
+ fn push_active_span(&mut self, span: &SpanObject) -> usize {
self.primary_endpoint_name = span.operation_name.clone();
let mut stack = self.active_span_stack_mut();
- stack.push(span);
+ stack.push(ActiveSpan::new(span.span_id));
stack.len() - 1
}
diff --git a/tests/trace_context.rs b/tests/trace_context.rs
index c67bdf6..4603ad9 100644
--- a/tests/trace_context.rs
+++ b/tests/trace_context.rs
@@ -114,7 +114,7 @@ async fn create_span() {
logs: Vec::<Log>::new(),
skip_analysis: false,
};
- assert_eq!(context.last_span().as_deref(), Some(&span3_expected));
+ assert_eq!(context.last_span(), Some(span3_expected));
}
{
@@ -140,7 +140,7 @@ async fn create_span() {
logs: Vec::<Log>::new(),
skip_analysis: false,
};
- assert_eq!(context.last_span().as_deref(), Some(&span5_expected));
+ assert_eq!(context.last_span(), Some(span5_expected));
}
}
@@ -162,7 +162,7 @@ async fn create_span() {
logs: expected_log,
skip_analysis: false,
};
- assert_eq!(context.last_span().as_deref(), Some(&span1_expected));
+ assert_eq!(context.last_span(), Some(span1_expected));
}
tracer