From cdce3cc1195a35cbaa48d00bea60e5fa64e19ab6 Mon Sep 17 00:00:00 2001 From: Gustavo Pantuza Date: Tue, 1 Oct 2024 14:08:59 -0300 Subject: [PATCH] Adds on_ending callback to allow processors to mutate spans before End operation (#1713) * feature: adds on_ending method as an optional callback for span processors Signed-off-by: Gustavo Pantuza * feature: calls every span processor that has on_ending implemented right after setting the end timestamp Signed-off-by: Gustavo Pantuza * test: adds tests for the new on_ending method from span processors Signed-off-by: Gustavo Pantuza * docs: Informs the on_ending callback is an experimental features from official spec Signed-off-by: Gustavo Pantuza * Update sdk/lib/opentelemetry/sdk/trace/span_processor.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> * Update sdk/lib/opentelemetry/sdk/trace/span_processor.rb Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> * refactor: switch method name from on_ending to on_finish to comply with project name strategy Signed-off-by: Gustavo Pantuza --------- Signed-off-by: Gustavo Pantuza Co-authored-by: Kayla Reopelle <87386821+kaylareopelle@users.noreply.github.com> --- sdk/lib/opentelemetry/sdk/trace/span.rb | 3 +++ .../opentelemetry/sdk/trace/span_processor.rb | 18 ++++++++++++++++++ .../sdk/trace/span_processor_test.rb | 4 ++++ 3 files changed, 25 insertions(+) diff --git a/sdk/lib/opentelemetry/sdk/trace/span.rb b/sdk/lib/opentelemetry/sdk/trace/span.rb index 274ac4c24d..c1f32657f8 100644 --- a/sdk/lib/opentelemetry/sdk/trace/span.rb +++ b/sdk/lib/opentelemetry/sdk/trace/span.rb @@ -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 diff --git a/sdk/lib/opentelemetry/sdk/trace/span_processor.rb b/sdk/lib/opentelemetry/sdk/trace/span_processor.rb index f1874f6504..2a60c3ba53 100644 --- a/sdk/lib/opentelemetry/sdk/trace/span_processor.rb +++ b/sdk/lib/opentelemetry/sdk/trace/span_processor.rb @@ -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. # diff --git a/sdk/test/opentelemetry/sdk/trace/span_processor_test.rb b/sdk/test/opentelemetry/sdk/trace/span_processor_test.rb index 262ad53b92..2bfc1532ba 100644 --- a/sdk/test/opentelemetry/sdk/trace/span_processor_test.rb +++ b/sdk/test/opentelemetry/sdk/trace/span_processor_test.rb @@ -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