Sha256: ed9f40be131b6d4357624ea4f894b95cba473de30696cdade3934c1f2a6ccae4

Contents?: true

Size: 1.61 KB

Versions: 9

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

begin
  require "active_support"
  require "active_support/notifications"
rescue LoadError
  nil
end

module PlainApm
  module Hooks
    class ActiveSupportSubscriber
      include EventAttributes

      def install
        return unless defined?(::ActiveSupport::Notifications)

        asn = ::ActiveSupport::Notifications

        # Rails 6.0 has a slightly different arity check in the subscriber
        # constructor, so we need to use a proc, method(:collect) won't work.
        # https://github.com/rails/rails/blob/28bb76d3efc39b2ef663dfe2346f7c2621343cd6/activesupport/lib/active_support/notifications/fanout.rb#L93-L100
        # vs
        # https://github.com/rails/rails/blob/7c70791470fc517deb7c640bead9f1b47efb5539/activesupport/lib/active_support/notifications/fanout.rb#L132-L139
        subscriber = proc { |event| collect(event) }

        # #monotonic_subscribe got added in Rails >= 6.1
        @subscription = if asn.respond_to?(:monotonic_subscribe)
          asn.monotonic_subscribe(notification_pattern, subscriber)
        else
          asn.subscribe(notification_pattern, subscriber)
        end
      end

      def uninstall
        ::ActiveSupport::Notifications.unsubscribe(@subscription) if @subscription
      end

      def collect(event)
        # id / transaction_id is by instrumenter and thread
        payload = payload(event)

        return if payload.nil?

        ::PlainApm.agent.collect(payload)
      end

      private

      def pattern
        raise "Not implemented"
      end

      def payload(event)
        raise "Not implemented"
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
plain_apm-0.9.2 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.9.1 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.9.0 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.8.8 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.8.7 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.8.6 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.8.5 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.8.4 lib/plain_apm/hooks/active_support_subscriber.rb
plain_apm-0.8.3 lib/plain_apm/hooks/active_support_subscriber.rb