|
10 | 10 | //! These cannot be changed after the `Span`'s end time has been set.
|
11 | 11 | use crate::trace::SpanLimits;
|
12 | 12 | use crate::Resource;
|
13 |
| -use opentelemetry::trace::{Event, SpanContext, SpanId, SpanKind, Status}; |
| 13 | +use opentelemetry::trace::{Event, Link, SpanContext, SpanId, SpanKind, Status}; |
14 | 14 | use opentelemetry::KeyValue;
|
15 | 15 | use std::borrow::Cow;
|
16 | 16 | use std::time::SystemTime;
|
@@ -170,6 +170,28 @@ impl opentelemetry::trace::Span for Span {
|
170 | 170 | });
|
171 | 171 | }
|
172 | 172 |
|
| 173 | + /// Add `Link` to this `Span` |
| 174 | + /// |
| 175 | + fn add_link(&mut self, span_context: SpanContext, attributes: Vec<KeyValue>) { |
| 176 | + let span_links_limit = self.span_limits.max_links_per_span as usize; |
| 177 | + let link_attributes_limit = self.span_limits.max_attributes_per_link as usize; |
| 178 | + self.with_data(|data| { |
| 179 | + if data.links.links.len() < span_links_limit { |
| 180 | + let dropped_attributes_count = |
| 181 | + attributes.len().saturating_sub(link_attributes_limit); |
| 182 | + let mut attributes = attributes; |
| 183 | + attributes.truncate(link_attributes_limit); |
| 184 | + data.links.add_link(Link::new( |
| 185 | + span_context, |
| 186 | + attributes, |
| 187 | + dropped_attributes_count as u32, |
| 188 | + )); |
| 189 | + } else { |
| 190 | + data.links.dropped_count += 1; |
| 191 | + } |
| 192 | + }); |
| 193 | + } |
| 194 | + |
173 | 195 | /// Finishes the span with given timestamp.
|
174 | 196 | fn end_with_timestamp(&mut self, timestamp: SystemTime) {
|
175 | 197 | self.ensure_ended_and_exported(Some(timestamp));
|
@@ -595,16 +617,13 @@ mod tests {
|
595 | 617 | let provider = provider_builder.build();
|
596 | 618 | let tracer = provider.tracer("opentelemetry-test");
|
597 | 619 |
|
598 |
| - let mut link = Link::new( |
599 |
| - SpanContext::new( |
600 |
| - TraceId::from_u128(12), |
601 |
| - SpanId::from_u64(12), |
602 |
| - TraceFlags::default(), |
603 |
| - false, |
604 |
| - Default::default(), |
605 |
| - ), |
606 |
| - Vec::new(), |
607 |
| - ); |
| 620 | + let mut link = Link::with_context(SpanContext::new( |
| 621 | + TraceId::from_u128(12), |
| 622 | + SpanId::from_u64(12), |
| 623 | + TraceFlags::default(), |
| 624 | + false, |
| 625 | + Default::default(), |
| 626 | + )); |
608 | 627 | for i in 0..(DEFAULT_MAX_ATTRIBUTES_PER_LINK * 2) {
|
609 | 628 | link.attributes
|
610 | 629 | .push(KeyValue::new(format!("key {}", i), i.to_string()));
|
@@ -632,20 +651,29 @@ mod tests {
|
632 | 651 |
|
633 | 652 | let mut links = Vec::new();
|
634 | 653 | for _i in 0..(DEFAULT_MAX_LINKS_PER_SPAN * 2) {
|
635 |
| - links.push(Link::new( |
636 |
| - SpanContext::new( |
637 |
| - TraceId::from_u128(12), |
638 |
| - SpanId::from_u64(12), |
639 |
| - TraceFlags::default(), |
640 |
| - false, |
641 |
| - Default::default(), |
642 |
| - ), |
643 |
| - Vec::new(), |
644 |
| - )) |
| 654 | + links.push(Link::with_context(SpanContext::new( |
| 655 | + TraceId::from_u128(12), |
| 656 | + SpanId::from_u64(12), |
| 657 | + TraceFlags::default(), |
| 658 | + false, |
| 659 | + Default::default(), |
| 660 | + ))) |
645 | 661 | }
|
646 | 662 |
|
647 | 663 | let span_builder = tracer.span_builder("test").with_links(links);
|
648 |
| - let span = tracer.build(span_builder); |
| 664 | + let mut span = tracer.build(span_builder); |
| 665 | + |
| 666 | + // add links using span api after building the span |
| 667 | + span.add_link( |
| 668 | + SpanContext::new( |
| 669 | + TraceId::from_u128(12), |
| 670 | + SpanId::from_u64(12), |
| 671 | + TraceFlags::default(), |
| 672 | + false, |
| 673 | + Default::default(), |
| 674 | + ), |
| 675 | + vec![], |
| 676 | + ); |
649 | 677 | let link_queue = span
|
650 | 678 | .data
|
651 | 679 | .clone()
|
|
0 commit comments