Sha256: f501dab8be523a7a058b7f6c74c5552ff47bcccda8726b2c68d1ab2326c64eb0

Contents?: true

Size: 1.86 KB

Versions: 4

Compression:

Stored size: 1.86 KB

Contents

require "active_support/notifications"

module Nunes
  class Subscriber
    # Private: The bang character that is the first char of some events.
    BANG = '!'

    # Public: Setup a subscription for the subscriber using the
    # provided adapter.
    #
    # adapter - The adapter instance to send instrumentation to.
    def self.subscribe(adapter, options = {})
      subscriber = options.fetch(:subscriber) { ActiveSupport::Notifications }
      subscriber.subscribe pattern, new(adapter)
    end

    def self.pattern
      raise "Not Implemented, override in subclass and provide a regex or string."
    end

    # Private: The adapter to send instrumentation to.
    attr_reader :adapter

    # Internal: Initializes a new instance.
    #
    # adapter - The adapter instance to send instrumentation to.
    def initialize(adapter)
      @adapter = Nunes::Adapter.wrap(adapter)
    end

    # Private: Dispatcher that converts incoming events to method calls.
    def call(name, start, ending, transaction_id, payload)
      # rails doesn't recommend instrumenting methods that start with bang
      # when in production
      return if name.start_with?(BANG)

      method_name = name.split('.').first

      if respond_to?(method_name)
        send(method_name, start, ending, transaction_id, payload)
      end
    end

    # Internal: Increment a metric for the client.
    #
    # metric - The String name of the metric to increment.
    # value - The Integer value to increment by.
    #
    # Returns nothing.
    def increment(metric, value = 1)
      @adapter.increment metric, value
    end

    # Internal: Track the timing of a metric for the client.
    #
    # metric - The String name of the metric.
    # value - The Integer duration of the event in milliseconds.
    #
    # Returns nothing.
    def timing(metric, value)
      @adapter.timing metric, value
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
nunes-0.4.0 lib/nunes/subscriber.rb
nunes-0.3.1 lib/nunes/subscriber.rb
nunes-0.3.0 lib/nunes/subscriber.rb
nunes-0.2.0 lib/nunes/subscriber.rb