Skip to content

Commit 45a0626

Browse files
authored
api: unify Event and Link access patterns (#757)
This makes the data in span `Event`s and `Link`s `pub`, but guards against future additions by making the structs `non_exhaustive`.
1 parent a451eb3 commit 45a0626

File tree

5 files changed

+33
-40
lines changed

5 files changed

+33
-40
lines changed

opentelemetry-api/src/trace/mod.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -209,17 +209,21 @@ impl From<&'static str> for TraceError {
209209
#[error("{0}")]
210210
struct Custom(String);
211211

212-
/// A `Span` has the ability to add events. Events have a time associated
213-
/// with the moment when they are added to the `Span`.
212+
/// Events record things that happened during a [`Span`]'s lifetime.
213+
#[non_exhaustive]
214214
#[derive(Clone, Debug, PartialEq)]
215215
pub struct Event {
216-
/// Event name
216+
/// The name of this event.
217217
pub name: Cow<'static, str>,
218-
/// Event timestamp
218+
219+
/// The time at which this event occurred.
219220
pub timestamp: time::SystemTime,
220-
/// Event attributes
221+
222+
/// Attributes that describe this event.
221223
pub attributes: Vec<KeyValue>,
222-
/// Number of dropped attributes
224+
225+
/// The number of attributes that were above the configured limit, and thus
226+
/// dropped.
223227
pub dropped_attributes_count: u32,
224228
}
225229

@@ -250,41 +254,30 @@ impl Event {
250254
}
251255
}
252256

253-
/// During the `Span` creation user MUST have the ability to record links to other `Span`s. Linked
254-
/// `Span`s can be from the same or a different trace.
257+
/// Link is the relationship between two Spans.
258+
///
259+
/// The relationship can be within the same trace or across different traces.
260+
#[non_exhaustive]
255261
#[derive(Clone, Debug, PartialEq)]
256262
pub struct Link {
257-
span_context: SpanContext,
263+
/// The span context of the linked span.
264+
pub span_context: SpanContext,
258265

259-
/// Attributes describing this link
266+
/// Attributes that describe this link.
260267
pub attributes: Vec<KeyValue>,
261268

262-
/// The number of attributes that were above the limit, and thus dropped.
269+
/// The number of attributes that were above the configured limit, and thus
270+
/// dropped.
263271
pub dropped_attributes_count: u32,
264272
}
265273

266274
impl Link {
267-
/// Create a new link
275+
/// Create a new link.
268276
pub fn new(span_context: SpanContext, attributes: Vec<KeyValue>) -> Self {
269277
Link {
270278
span_context,
271279
attributes,
272280
dropped_attributes_count: 0,
273281
}
274282
}
275-
276-
/// The span context of the linked span
277-
pub fn span_context(&self) -> &SpanContext {
278-
&self.span_context
279-
}
280-
281-
/// Attributes of the span link
282-
pub fn attributes(&self) -> &Vec<KeyValue> {
283-
&self.attributes
284-
}
285-
286-
/// Dropped attributes count
287-
pub fn dropped_attributes_count(&self) -> u32 {
288-
self.dropped_attributes_count
289-
}
290283
}

opentelemetry-api/src/trace/tracer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl SpanBuilder {
336336

337337
/// Assign links
338338
pub fn with_links(self, mut links: Vec<Link>) -> Self {
339-
links.retain(|l| l.span_context().is_valid());
339+
links.retain(|l| l.span_context.is_valid());
340340
SpanBuilder {
341341
links: Some(links),
342342
..self

opentelemetry-jaeger/src/exporter/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,7 +676,7 @@ fn links_to_references(links: sdk::trace::EvictedQueue<Link>) -> Option<Vec<jaeg
676676
let refs = links
677677
.iter()
678678
.map(|link| {
679-
let span_context = link.span_context();
679+
let span_context = &link.span_context;
680680
let trace_id_bytes = span_context.trace_id().to_bytes();
681681
let (high, low) = trace_id_bytes.split_at(8);
682682
let trace_id_high = i64::from_be_bytes(high.try_into().unwrap());

opentelemetry-proto/src/transform/traces.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ pub mod tonic {
3636
impl From<Link> for span::Link {
3737
fn from(link: Link) -> Self {
3838
span::Link {
39-
trace_id: link.span_context().trace_id().to_bytes().to_vec(),
40-
span_id: link.span_context().span_id().to_bytes().to_vec(),
41-
trace_state: link.span_context().trace_state().header(),
42-
attributes: Attributes::from(link.attributes().clone()).0,
43-
dropped_attributes_count: link.dropped_attributes_count(),
39+
trace_id: link.span_context.trace_id().to_bytes().to_vec(),
40+
span_id: link.span_context.span_id().to_bytes().to_vec(),
41+
trace_state: link.span_context.trace_state().header(),
42+
attributes: Attributes::from(link.attributes).0,
43+
dropped_attributes_count: link.dropped_attributes_count,
4444
}
4545
}
4646
}
@@ -154,11 +154,11 @@ pub mod grpcio {
154154
impl From<Link> for Span_Link {
155155
fn from(link: Link) -> Self {
156156
Span_Link {
157-
trace_id: link.span_context().trace_id().to_bytes().to_vec(),
158-
span_id: link.span_context().span_id().to_bytes().to_vec(),
159-
trace_state: link.span_context().trace_state().header(),
160-
attributes: Attributes::from(link.attributes().clone()).0,
161-
dropped_attributes_count: link.dropped_attributes_count(),
157+
trace_id: link.span_context.trace_id().to_bytes().to_vec(),
158+
span_id: link.span_context.span_id().to_bytes().to_vec(),
159+
trace_state: link.span_context.trace_state().header(),
160+
attributes: Attributes::from(link.attributes).0,
161+
dropped_attributes_count: link.dropped_attributes_count,
162162
..Default::default()
163163
}
164164
}

opentelemetry-sdk/src/trace/span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ mod tests {
611611
.links;
612612
let link_vec: Vec<_> = link_queue.iter().collect();
613613
let processed_link = link_vec.get(0).expect("should have at least one link");
614-
assert_eq!(processed_link.attributes().len(), 128);
614+
assert_eq!(processed_link.attributes.len(), 128);
615615
}
616616

617617
#[test]

0 commit comments

Comments
 (0)