Sha256: d16fb62b0b9514eb7532bba3559fec1458fe4eb60a50d6f14970c1c5fbe96f37

Contents?: true

Size: 1.55 KB

Versions: 12

Compression:

Stored size: 1.55 KB

Contents

# frozen_string_literal: true

require 'active_support/notifications'
require 'elastic_apm/normalizers'

module ElasticAPM
  # @api private
  class Subscriber
    include Log

    def initialize(agent)
      @agent = agent
      @normalizers = Normalizers.build(agent.config)
    end

    def register!
      unregister! if @subscription

      @subscription =
        ActiveSupport::Notifications.subscribe(notifications_regex, self)
    end

    def unregister!
      ActiveSupport::Notifications.unsubscribe @subscription
      @subscription = nil
    end

    # AS::Notifications API

    Notification = Struct.new(:id, :span)

    def start(name, id, payload)
      # debug "AS::Notification#start:#{name}:#{id}"
      return unless (transaction = @agent.current_transaction)

      normalized = @normalizers.normalize(transaction, name, payload)

      span =
        if normalized == :skip
          nil
        else
          name, type, context = normalized
          @agent.span(name, type, context: context)
        end

      transaction.notifications << Notification.new(id, span)
    end

    def finish(_name, id, _payload)
      # debug "AS::Notification#finish:#{name}:#{id}"
      return unless (transaction = @agent.current_transaction)

      while (notification = transaction.notifications.pop)
        next unless notification.id == id

        if (span = notification.span)
          span.done
        end
        return
      end
    end

    private

    def notifications_regex
      @notifications_regex ||= /(#{@normalizers.keys.join('|')})/
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
elastic-apm-1.1.0 lib/elastic_apm/subscriber.rb
elastic-apm-1.0.2 lib/elastic_apm/subscriber.rb
elastic-apm-1.0.1 lib/elastic_apm/subscriber.rb
elastic-apm-1.0.0 lib/elastic_apm/subscriber.rb
elastic-apm-1.0.0.beta2 lib/elastic_apm/subscriber.rb
elastic-apm-1.0.0.beta1 lib/elastic_apm/subscriber.rb
elastic-apm-0.8.0 lib/elastic_apm/subscriber.rb
elastic-apm-0.7.4 lib/elastic_apm/subscriber.rb
elastic-apm-0.7.3 lib/elastic_apm/subscriber.rb
elastic-apm-0.7.2 lib/elastic_apm/subscriber.rb
elastic-apm-0.7.1 lib/elastic_apm/subscriber.rb
elastic-apm-0.7.0 lib/elastic_apm/subscriber.rb