Skip to content

Commit c1f4662

Browse files
committed
fix: allow span links to be added to a SpanRef
1 parent 1d9bd25 commit c1f4662

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

opentelemetry-sdk/src/trace/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,16 @@ mod tests {
234234
span.update_name("span_name_updated");
235235
span.set_attribute(KeyValue::new("attribute1", "value1"));
236236
span.add_event("test-event".to_string(), vec![]);
237+
span.add_link(
238+
SpanContext::new(
239+
TraceId::from(47),
240+
SpanId::from(11),
241+
TraceFlags::default(),
242+
false,
243+
Default::default(),
244+
),
245+
vec![],
246+
);
237247
});
238248

239249
// Assert
@@ -247,6 +257,9 @@ mod tests {
247257
assert_eq!(span.attributes.len(), 1);
248258
assert_eq!(span.events.len(), 1);
249259
assert_eq!(span.events[0].name, "test-event");
260+
assert_eq!(span.links.len(), 1);
261+
assert_eq!(span.links[0].span_context.trace_id(), TraceId::from(47));
262+
assert_eq!(span.links[0].span_context.span_id(), SpanId::from(11));
250263
assert_eq!(span.span_context.trace_flags(), TraceFlags::SAMPLED);
251264
assert!(!span.span_context.is_remote());
252265
assert_eq!(span.status, Status::Unset);

opentelemetry/src/trace/context.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,30 @@ impl SpanRef<'_> {
185185
self.with_inner_mut(move |inner| inner.update_name(new_name))
186186
}
187187

188+
/// Adds a [`Link`] to another [`SpanContext`].
189+
///
190+
/// This method allows linking the current span to another span, identified by
191+
/// its `SpanContext`. Links can be used to connect spans from different traces
192+
/// or within the same trace. Attributes can be attached to the link to provide
193+
/// additional context or metadata.
194+
///
195+
/// # Arguments
196+
///
197+
/// * `span_context` - The `SpanContext` of the span to link to. This represents
198+
/// the target span's unique identifiers and trace information.
199+
/// * `attributes` - A vector of `KeyValue` pairs that describe additional
200+
/// attributes of the link. These attributes can include any contextual
201+
/// information relevant to the link between the spans.
202+
///
203+
/// Note - Any [`Link`] added via this mechanism is not accessible to a `Sampler`.
204+
/// It is recommended to add Links at [`Span`] creation time, rather than adding
205+
/// them afterwards.
206+
///
207+
/// [`Link`]: crate::trace::Link
208+
pub fn add_link(&self, span_context: SpanContext, attributes: Vec<KeyValue>) {
209+
self.with_inner_mut(move |inner| inner.add_link(span_context, attributes));
210+
}
211+
188212
/// Signals that the operation described by this span has now ended.
189213
pub fn end(&self) {
190214
self.end_with_timestamp(crate::time::now());

0 commit comments

Comments
 (0)