Skip to content

Commit

Permalink
fix: remove call to ActiveSupport::Notifications.notifier#synchronize…
Browse files Browse the repository at this point in the history
… deprecated in Rails 7.2 (#707)

* wrap call to depcrecated private API behind version conditional

* convert Rails version string to Gem::Version

* fix module access?

* check for synchronize method instead of Rails version

* add comment

---------

Co-authored-by: Ariel Valentin <arielvalentin@users.noreply.github.com>
  • Loading branch information
octokatherine and arielvalentin authored Oct 31, 2023
1 parent 331d5d5 commit 828e137
Showing 1 changed file with 17 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,23 @@ def self.subscribe(

subscriber_object = ::ActiveSupport::Notifications.subscribe(pattern, subscriber)

::ActiveSupport::Notifications.notifier.synchronize do
subscribers = ::ActiveSupport::Notifications.notifier.instance_variable_get(:@string_subscribers)[pattern]

if subscribers.nil?
OpenTelemetry.handle_error(
message: 'Unable to move OTEL ActiveSupport Notifications subscriber to the front of the notifications list which may cause incomplete traces.' \
'Please report an issue here: ' \
'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues/new?labels=bug&template=bug_report.md&title=ActiveSupport%20Notifications%20subscribers%20list%20is%20nil'
)
else
subscribers.unshift(
subscribers.delete(subscriber_object)
)
# this can be removed once we drop support for Rails < 7.2
# see https://github.com/open-telemetry/opentelemetry-ruby-contrib/pull/707 for more context
if ::ActiveSupport::Notifications.notifier.respond_to?(:synchronize)
::ActiveSupport::Notifications.notifier.synchronize do
subscribers = ::ActiveSupport::Notifications.notifier.instance_variable_get(:@string_subscribers)[pattern]

if subscribers.nil?
OpenTelemetry.handle_error(
message: 'Unable to move OTEL ActiveSupport Notifications subscriber to the front of the notifications list which may cause incomplete traces.' \
'Please report an issue here: ' \
'https://github.com/open-telemetry/opentelemetry-ruby-contrib/issues/new?labels=bug&template=bug_report.md&title=ActiveSupport%20Notifications%20subscribers%20list%20is%20nil'
)
else
subscribers.unshift(
subscribers.delete(subscriber_object)
)
end
end
end
subscriber_object
Expand Down

0 comments on commit 828e137

Please sign in to comment.