diff --git a/CHANGELOG.md b/CHANGELOG.md index 74aec6d1b..f318d44c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Features - Expose `span_id` in `Span` constructor [#1945](https://github.com/getsentry/sentry-ruby/pull/1945) +- Expose `end_timestamp` in `Span#finish` and `Transaction#finish` [#1946](https://github.com/getsentry/sentry-ruby/pull/1946) - Add OpenTelemetry support with new `sentry-opentelemetry` gem - Add `config.instrumenter` to switch between `:sentry` and `:otel` instrumentation [#1944](https://github.com/getsentry/sentry-ruby/pull/1944) diff --git a/sentry-ruby/lib/sentry/span.rb b/sentry-ruby/lib/sentry/span.rb index 9ca450e16..7e1baffd8 100644 --- a/sentry-ruby/lib/sentry/span.rb +++ b/sentry-ruby/lib/sentry/span.rb @@ -90,11 +90,8 @@ def initialize( # Finishes the span by adding a timestamp. # @return [self] - def finish - # already finished - return if @timestamp - - @timestamp = Sentry.utc_now.to_f + def finish(end_timestamp: nil) + @timestamp = end_timestamp || @timestamp || Sentry.utc_now.to_f self end diff --git a/sentry-ruby/lib/sentry/transaction.rb b/sentry-ruby/lib/sentry/transaction.rb index 2a5ab0f0c..e237b5a9b 100644 --- a/sentry-ruby/lib/sentry/transaction.rb +++ b/sentry-ruby/lib/sentry/transaction.rb @@ -210,7 +210,7 @@ def set_initial_sample_decision(sampling_context:) # Finishes the transaction's recording and send it to Sentry. # @param hub [Hub] the hub that'll send this transaction. (Deprecated) # @return [TransactionEvent] - def finish(hub: nil) + def finish(hub: nil, end_timestamp: nil) if hub log_warn( <<~MSG @@ -222,7 +222,7 @@ def finish(hub: nil) hub ||= @hub - super() # Span#finish doesn't take arguments + super(end_timestamp: end_timestamp) if @name.nil? @name = UNLABELD_NAME diff --git a/sentry-ruby/spec/sentry/transaction_spec.rb b/sentry-ruby/spec/sentry/transaction_spec.rb index 2d4716f66..da7f54a68 100644 --- a/sentry-ruby/spec/sentry/transaction_spec.rb +++ b/sentry-ruby/spec/sentry/transaction_spec.rb @@ -391,6 +391,16 @@ expect(event[:transaction]).to eq("foo") end + it "finishes the transaction with explicit timestamp" do + timestamp = Sentry.utc_now.to_f + subject.finish(end_timestamp: timestamp) + + expect(events.count).to eq(1) + event = events.last.to_hash + + expect(event[:timestamp]).to eq(timestamp) + end + it "assigns the transaction's tags" do Sentry.set_tags(name: "apple")