Class: Nunes::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/nunes/subscriber.rb

Constant Summary collapse

BANG =

Private: The bang character that is the first char of some events.

"!".freeze
DOT =

Private: The dot charactor used to determine the method name.

".".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter) ⇒ Subscriber

Internal: Initializes a new instance.

adapter - The adapter instance to send instrumentation to.



30
31
32
# File 'lib/nunes/subscriber.rb', line 30

def initialize(adapter)
  @adapter = Nunes::Adapter.wrap(adapter)
end

Instance Attribute Details

#adapterObject (readonly)

Private: The adapter to send instrumentation to.



25
26
27
# File 'lib/nunes/subscriber.rb', line 25

def adapter
  @adapter
end

Class Method Details

.patternObject



20
21
22
# File 'lib/nunes/subscriber.rb', line 20

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

.subscribe(adapter, options = {}) ⇒ Object

Public: Setup a subscription for the subscriber using the provided adapter.

adapter - The adapter instance to send instrumentation to.



15
16
17
18
# File 'lib/nunes/subscriber.rb', line 15

def self.subscribe(adapter, options = {})
  subscriber = options.fetch(:subscriber) { ActiveSupport::Notifications }
  subscriber.subscribe pattern, new(adapter)
end

Instance Method Details

#call(name, start, ending, transaction_id, payload) ⇒ Object

Private: Dispatcher that converts incoming events to method calls.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/nunes/subscriber.rb', line 35

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(DOT).first

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

#increment(metric, value = 1) ⇒ Object

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.



53
54
55
# File 'lib/nunes/subscriber.rb', line 53

def increment(metric, value = 1)
  @adapter.increment metric, value
end

#timing(metric, value) ⇒ Object

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.



63
64
65
# File 'lib/nunes/subscriber.rb', line 63

def timing(metric, value)
  @adapter.timing metric, value
end