Sha256: 0d7ecc53bfe058358dcd002533875b131fed281f0dc082896605e864a7006613
Contents?: true
Size: 1.66 KB
Versions: 10
Compression:
Stored size: 1.66 KB
Contents
# typed: ignore # Copyright (c) 2015 Sqreen. All Rights Reserved. # Please refer to our terms for more information: https://www.sqreen.com/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
10 entries across 10 versions & 1 rubygems