module Sqreen::PerformanceNotifications

This module enable us to keep track of sqreen resource usage

It is inspired by ActiveSupport::Notifications

Public Class Methods

instrument(rule, cb, meta = {}) { || ... } click to toggle source

Instrument a call identified by key

# File lib/sqreen/performance_notifications.rb, line 31
def instrument(rule, cb, meta = {}, &block)
  return yield unless listen_for?
  _instrument(rule, cb, meta, &block)
end
listen_for?() click to toggle source

Is there a subscriber

# File lib/sqreen/performance_notifications.rb, line 26
def listen_for?
  !@subscriptions_all.empty?
end
notify(rule, cb, start, stop, meta = {}) click to toggle source
# File lib/sqreen/performance_notifications.rb, line 36
def notify(rule, cb, start, stop, meta = {})
  return unless listen_for?
  notifiers.each do |callable|
    callable.call(rule, cb, start, stop, meta)
  end
end
subscribe(&block) click to toggle source

Subscribe to receive notifications about an event returns a subscription identification

# File lib/sqreen/performance_notifications.rb, line 19
def subscribe(&block)
  id = (@subscription_id += 1)
  @subscriptions_all[id] = block
  id
end
unsubscribe(subscription) click to toggle source

Unsubscrube for a given subscription

# File lib/sqreen/performance_notifications.rb, line 44
def unsubscribe(subscription)
  return unless @subscriptions_all.delete(subscription).nil?
end
unsubscribe_all!() click to toggle source

Unsubscribe from everything not threadsafe

# File lib/sqreen/performance_notifications.rb, line 50
def unsubscribe_all!
  @subscriptions_all.clear
end

Private Class Methods

_instrument(rule, cb, meta) { || ... } click to toggle source
# File lib/sqreen/performance_notifications.rb, line 60
def _instrument(rule, cb, meta)
  start = Sqreen.time
  yield
ensure
  stop = Sqreen.time
  notify(rule, cb, start, stop, meta)
end
notifiers() click to toggle source
# File lib/sqreen/performance_notifications.rb, line 56
def notifiers
  @subscriptions_all.values
end