Sha256: d96ce1e903f5f0289dc7c309ba9e2b960e0fd5aada0e8e4a0f70f596393a674d

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

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)
        # drop immediately if there's no ongoing transaction
        return if PlainApm::Extensions::Context.trace_id.nil?

        # 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

1 entries across 1 versions & 1 rubygems

Version Path
plain_apm-0.10.2 lib/plain_apm/hooks/active_support_subscriber.rb