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

Adds on_ending callback to allow processors to mutate spans before End operation #1713

Merged
merged 11 commits into from
Oct 1, 2024
Merged
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
3 changes: 3 additions & 0 deletions sdk/lib/opentelemetry/sdk/trace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ def finish(end_timestamp: nil)
return self
end
@end_timestamp = relative_timestamp(end_timestamp)
@span_processors.each do |processor|
processor.on_finishing(self) if processor.respond_to?(:on_finishing)
end
@attributes = validated_attributes(@attributes).freeze
@events.freeze
@links.freeze
Expand Down
18 changes: 18 additions & 0 deletions sdk/lib/opentelemetry/sdk/trace/span_processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,24 @@ class SpanProcessor
# started span.
def on_start(span, parent_context); end

# The on_finishing method is an experimental feature and may have breaking changes.
# The OpenTelemetry specification defines it as "On Ending". As `end` is a reserved
# keyword in Ruby, we are using `on_finishing` instead.
#
# Called when a {Span} is ending, after the end timestamp has been set
# but before span becomes immutable. This allows for updating the span
# by setting attributes or adding links and events.
#
# This method is called synchronously and should not block the current
# thread nor throw exceptions.
#
# This method is optional on the Span Processor interface. It will only
# get called if it exists within the processor.
#
# @param [Span] span the {Span} that just is ending (still mutable).
# @return [void]
def on_finishing(span); end

# Called when a {Span} is ended, if the {Span#recording?}
# returns true.
#
Expand Down
4 changes: 4 additions & 0 deletions sdk/test/opentelemetry/sdk/trace/span_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
processor.on_start(span, context)
end

it 'implements #on_finishing' do
processor.on_finishing(span)
end

it 'implements #on_finish' do
processor.on_finish(span)
end
Expand Down
Loading