Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions opentelemetry-sdk/src/trace/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
}

impl SpanEvents {
/// Create a new `SpanEvents` from a list of events and a dropped count
pub fn new(events: Vec<Event>, dropped_count: u32) -> Self {
Self {
events,
dropped_count,
}
}

Check warning on line 40 in opentelemetry-sdk/src/trace/events.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/events.rs#L35-L40

Added lines #L35 - L40 were not covered by tests

pub(crate) fn add_event(&mut self, event: Event) {
self.events.push(event);
}
Expand Down
8 changes: 8 additions & 0 deletions opentelemetry-sdk/src/trace/links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@
}

impl SpanLinks {
/// Create a new `SpanLinks` from a list of events and a dropped count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it only required for tests?

Copy link
Contributor Author

@paullegranddc paullegranddc Apr 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I need it for tests, but there are definitely wider use cases.
For instance you could imagine span processors or span exporters trying to create "synthetic spans" and put span links and span events on these synthetic spans.

This currently doesn't work, and I can't think of a good rational as to why creating SpanData without span events should not be possible, and not with them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently doesn't work, and I can't think of a good rational as to why creating SpanData without span events should not be possible, and not with them.

It feels like there is one too many negations here. I assume you're saying that if it's possible to create SpanData without SpanLinks and SpanEvents, then it should be possible to create SpanData with them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if they should not be builders. The reason being that I believe we had intentionally made them non_exhaustive to prevent api breakage but if we go with simply a new that would then potentially require creating a new with breaking changes in the future.
What do you think @cijothomas ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well actually I was thinking of closing this PR because I found a way to get around this.

The main reason I was proposing this is I have a function like fn transform_span(s: SpanData) -> InternalSpan with complex business logic which I'd like to write unit tests for.

What I ended up doing is creating a struct mirroring SpanData in my crate, which I can construct. so now I do

pub(crate) struct ExportSpan {
    pub span_context: opentelemetry::trace::SpanContext,
    pub parent_span_id: opentelemetry::trace::SpanId,
    pub span_kind: opentelemetry::trace::SpanKind,
    pub name: Cow<'static, str>,
    pub start_time: SystemTime,
    pub end_time: SystemTime,
    pub attributes: Vec<opentelemetry::KeyValue>,
    pub dropped_attributes_count: u32,
    pub events: Vec<Event>,
    pub dropped_event_count: u32,
    pub links: Vec<opentelemetry::trace::Link>,
    pub dropped_links_count: u32,
    pub status: opentelemetry::trace::Status,
    pub instrumentation_scope: opentelemetry::InstrumentationScope,
}

fn export_span(s: SpanData) -> ExportSpan; // 1:1 mapping, no tests needed
fn transform_span(s: ExportSpan) -> InternalSpan; // complex logic, but I can create ExportSpan structs for tests

pub fn new(links: Vec<Link>, dropped_count: u32) -> Self {
Self {
links,
dropped_count,
}
}

Check warning on line 40 in opentelemetry-sdk/src/trace/links.rs

View check run for this annotation

Codecov / codecov/patch

opentelemetry-sdk/src/trace/links.rs#L35-L40

Added lines #L35 - L40 were not covered by tests

pub(crate) fn add_link(&mut self, link: Link) {
self.links.push(link);
}
Expand Down