Sha256: 3ed9defa961c2d52d99df5e1533659655ae8cec53e14c1c7adc7d45c68127258
Contents?: true
Size: 1.63 KB
Versions: 1
Compression:
Stored size: 1.63 KB
Contents
require 'dry-equalizer' require 'dry/monitor/rack/middleware' require 'dry/monitor/logger' require 'dry/monitor/sql/logger' module Dry module Monitor class Clock def measure(&block) start = current result = block.() stop = current [result, ((stop - start) * 1000).round(2)] end def current Time.now end end CLOCK = Clock.new class Event attr_reader :id attr_reader :info def initialize(id, info = {}) @id = id @info = info end end class Notifications attr_reader :id attr_reader :events attr_reader :listeners attr_reader :clock def initialize(id) @id = id @listeners = Hash.new { |h, k| h[k] = [] } @events = {} @clock = CLOCK end def event(id, info = {}) events[id] = Event.new(id, info) self end def subscribe(event_id, listener = nil, &block) listeners[event_id] << (listener || block) self end def start(event_id, payload) instrument(event_id, payload) end def stop(event_id, payload) instrument(event_id, payload) end def instrument(event_id, payload = nil, &block) event = events[event_id] if block result, time = clock.measure(&block) end listeners[event_id].each do |listener| if time listener.(time, event.id, payload.merge(event.info)) else listener.(event.id, payload.merge(event.info)) end end result end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-monitor-0.0.1 | lib/dry/monitor.rb |