Sha256: b8862acebd19ac178724b3ab8337186bd8c0026e4815764a801f215866a000ec
Contents?: true
Size: 1.64 KB
Versions: 38
Compression:
Stored size: 1.64 KB
Contents
# Copyright (c) 2015 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.io/terms.html require 'sqreen/mono_time' module Sqreen # This module enable us to keep track of sqreen resource usage # # It is inspired by ActiveSupport::Notifications # module PerformanceNotifications @subscriptions_all = {} @subscription_id = 0 class << self # Subscribe to receive notifications about an event # returns a subscription identification def subscribe(&block) id = (@subscription_id += 1) @subscriptions_all[id] = block id end # Is there a subscriber def listen_for? !@subscriptions_all.empty? end # Instrument a call identified by key def instrument(rule, cb, meta = {}, &block) return yield unless listen_for? _instrument(rule, cb, meta, &block) end def notify(rule, cb, start, stop, meta = {}) return unless listen_for? notifiers.each do |callable| callable.call(rule, cb, start, stop, meta) end end # Unsubscrube for a given subscription def unsubscribe(subscription) return unless @subscriptions_all.delete(subscription).nil? end # Unsubscribe from everything # not threadsafe def unsubscribe_all! @subscriptions_all.clear end private def notifiers @subscriptions_all.values end def _instrument(rule, cb, meta) start = Sqreen.time yield ensure stop = Sqreen.time notify(rule, cb, start, stop, meta) end end end end
Version data entries
38 entries across 38 versions & 1 rubygems