Skip to content

Commit

Permalink
Adds on_ending callback to allow processors to mutate spans before En…
Browse files Browse the repository at this point in the history
…d operation (#1713)

* feature: adds on_ending method as an optional callback for span processors

Signed-off-by: Gustavo Pantuza <[email protected]>

* feature: calls every span processor that has on_ending implemented right after setting the end timestamp

Signed-off-by: Gustavo Pantuza <[email protected]>

* test: adds tests for the new on_ending method from span processors

Signed-off-by: Gustavo Pantuza <[email protected]>

* docs: Informs the on_ending callback is an experimental features from official spec

Signed-off-by: Gustavo Pantuza <[email protected]>

* Update sdk/lib/opentelemetry/sdk/trace/span_processor.rb

Co-authored-by: Kayla Reopelle <[email protected]>

* Update sdk/lib/opentelemetry/sdk/trace/span_processor.rb

Co-authored-by: Kayla Reopelle <[email protected]>

* refactor: switch method name from on_ending to on_finish to comply with project name strategy

Signed-off-by: Gustavo Pantuza <[email protected]>

---------

Signed-off-by: Gustavo Pantuza <[email protected]>
Co-authored-by: Kayla Reopelle <[email protected]>
  • Loading branch information
pantuza and kaylareopelle authored Oct 1, 2024
1 parent e16803c commit cdce3cc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
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

0 comments on commit cdce3cc

Please sign in to comment.