Skip to content

Commit bba223d

Browse files
authored
feat: add_link and with_link (#161)
1 parent 456550d commit bba223d

17 files changed

Lines changed: 600 additions & 768 deletions

File tree

fastrace-opentelemetry/src/lib.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use fastrace::prelude::*;
3333
use opentelemetry::InstrumentationScope;
3434
use opentelemetry::KeyValue;
3535
use opentelemetry::trace::Event;
36+
use opentelemetry::trace::Link;
3637
use opentelemetry::trace::SpanContext as OtelSpanContext;
3738
use opentelemetry::trace::SpanKind;
3839
use opentelemetry::trace::Status;
@@ -139,7 +140,6 @@ static OTEL_PROPERTIES: LazyLock<HashSet<&str>> = LazyLock::new(|| {
139140
])
140141
});
141142

142-
/// Convert a list of properties to a list of key-value pairs.
143143
fn map_props_to_kvs(props: Vec<(Cow<'static, str>, Cow<'static, str>)>) -> Vec<KeyValue> {
144144
props
145145
.into_iter()
@@ -148,7 +148,6 @@ fn map_props_to_kvs(props: Vec<(Cow<'static, str>, Cow<'static, str>)>) -> Vec<K
148148
.collect()
149149
}
150150

151-
/// Convert a list of [`EventRecord`] to OpenTelemetry [`SpanEvents`].
152151
fn map_events(events: Vec<EventRecord>) -> SpanEvents {
153152
let mut queue = SpanEvents::default();
154153
queue.events.reserve(events.len());
@@ -167,6 +166,31 @@ fn map_events(events: Vec<EventRecord>) -> SpanEvents {
167166
queue
168167
}
169168

169+
fn map_links(links: Vec<SpanContext>) -> SpanLinks {
170+
let links = links
171+
.into_iter()
172+
.map(|link| {
173+
let trace_flags = if link.sampled {
174+
TraceFlags::SAMPLED
175+
} else {
176+
TraceFlags::default()
177+
};
178+
let span_context = OtelSpanContext::new(
179+
link.trace_id.0.into(),
180+
link.span_id.0.into(),
181+
trace_flags,
182+
false,
183+
TraceState::default(),
184+
);
185+
Link::with_context(span_context)
186+
})
187+
.collect();
188+
189+
let mut span_links = SpanLinks::default();
190+
span_links.links = links;
191+
span_links
192+
}
193+
170194
trait DynSpanExporter: Send + Sync + Debug {
171195
fn export(
172196
&self,
@@ -209,6 +233,7 @@ impl OpenTelemetryReporter {
209233
name,
210234
properties,
211235
events,
236+
links,
212237
}| {
213238
let parent_span_id = parent_id.0.into();
214239
let span_kind = span_kind(&properties);
@@ -221,6 +246,7 @@ impl OpenTelemetryReporter {
221246
+ Duration::from_nanos(begin_time_unix_ns + duration_ns);
222247
let attributes = map_props_to_kvs(properties);
223248
let events = map_events(events);
249+
let links = map_links(links);
224250

225251
SpanData {
226252
span_context: OtelSpanContext::new(
@@ -239,7 +265,7 @@ impl OpenTelemetryReporter {
239265
attributes,
240266
dropped_attributes_count: 0,
241267
events,
242-
links: SpanLinks::default(),
268+
links,
243269
status,
244270
instrumentation_scope,
245271
}

fastrace/src/collector/command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Copyright 2020 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
use crate::collector::CollectToken;
34
use crate::collector::SpanSet;
4-
use crate::util::CollectToken;
55

66
#[derive(Debug)]
77
pub enum CollectCommand {

0 commit comments

Comments
 (0)