Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add add_link to span api/sdk #1623

Merged
merged 5 commits into from
Apr 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
22 changes: 22 additions & 0 deletions api/lib/opentelemetry/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,28 @@ def add_attributes(attributes)
self
end

# Add a link to a {Span}.
#
# Adding links at span creation using the `links` option is preferred
# to calling add_link later, because head sampling decisions can only
# consider information present during span creation.
#
# Example:
#
# span.add_link(OpenTelemetry::Trace::Link.new(span_to_link_from.context))
#
# Note that the OpenTelemetry project
# {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
# documents} certain "standard attributes" that have prescribed semantic
# meanings.
#
# @param [OpenTelemetry::Trace::Link] the link object to add on the {Span}.
#
# @return [self] returns itself
def add_link(link)
self
end

# Add an event to a {Span}.
#
# Example:
Expand Down
6 changes: 6 additions & 0 deletions api/test/opentelemetry/trace/span_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
end
end

describe '#add_link' do
it 'returns self' do
_(span.add_link(OpenTelemetry::Trace::Link.new(span_context))).must_equal(span)
end
end

describe '#add_event' do
it 'returns self' do
_(span.add_event('event-name')).must_equal(span)
Expand Down
31 changes: 31 additions & 0 deletions sdk/lib/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,37 @@ def add_attributes(attributes)
self
end

# Add a link to a {Span}.
#
# Adding links at span creation using the `links` option is preferred
# to calling add_link later, because head sampling decisions can only
# consider information present during span creation.
#
# Example:
#
# span.add_link(OpenTelemetry::Trace::Link.new(span_to_link_from.context))
#
# Note that the OpenTelemetry project
# {https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/data-semantic-conventions.md
# documents} certain "standard attributes" that have prescribed semantic
# meanings.
#
# @param [OpenTelemetry::Trace::Link] the link object to add on the {Span}.
#
# @return [self] returns itself
def add_link(link)
@mutex.synchronize do
if @ended
OpenTelemetry.logger.warn('Calling add_link on an ended Span.')
else
@links ||= []
@links = trim_links(@links + [link], @span_limits.link_count_limit, @span_limits.link_attribute_count_limit)
dmathieu marked this conversation as resolved.
Show resolved Hide resolved
@total_recorded_links += 1
end
end
self
end

# Add an Event to a {Span}.
#
# Example:
Expand Down
41 changes: 41 additions & 0 deletions sdk/test/opentelemetry/sdk/trace/span_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,47 @@
end
end

describe '#add_link' do
it 'does not add a link if span is ended' do
span.finish
span.add_link(OpenTelemetry::Trace::Link.new(context))
_(span.links).must_be_nil
end

it 'adds a link' do
span.add_link(OpenTelemetry::Trace::Link.new(context))
links = span.links
_(links.size).must_equal(1)
_(links.first.span_context).must_equal(context)
end

it 'adds a link with attributes' do
attrs = { 'foo' => 'bar' }
span.add_link(OpenTelemetry::Trace::Link.new(context, attrs))
links = span.links
_(links.size).must_equal(1)
_(links.first.attributes).must_equal(attrs)
end

it 'updates the links count' do
span.add_link(OpenTelemetry::Trace::Link.new(context))
span.add_link(OpenTelemetry::Trace::Link.new(context))
_(span.to_span_data.total_recorded_links).must_equal(2)
end

it 'trims excess links' do
span.add_link(OpenTelemetry::Trace::Link.new(context))
span.add_link(OpenTelemetry::Trace::Link.new(context))
_(span.links.size).must_equal(1)
end

it 'prunes invalid links' do
invalid_context = OpenTelemetry::Trace::SpanContext.new(trace_id: OpenTelemetry::Trace::INVALID_TRACE_ID)
span.add_link(OpenTelemetry::Trace::Link.new(invalid_context))
_(span.links.size).must_equal(0)
end
end

describe '#add_event' do
it 'add a named event' do
span.add_event('added')
Expand Down
Loading