Class: Purplelight::Telemetry
- Inherits:
-
Object
- Object
- Purplelight::Telemetry
- Defined in:
- lib/purplelight/telemetry.rb
Overview
Lightweight, low-overhead timing and counters with a ticket API.
Constant Summary collapse
- NULL =
A disabled singleton for zero overhead checks if needed.
new(enabled: false)
Instance Attribute Summary collapse
-
#counters ⇒ Object
readonly
Returns the value of attribute counters.
-
#timers ⇒ Object
readonly
Returns the value of attribute timers.
Instance Method Summary collapse
- #add(key, count = 1) ⇒ Object
- #enabled? ⇒ Boolean
-
#finish(key, ticket) ⇒ Object
Finish a timer using a ticket from start.
-
#initialize(enabled: true) ⇒ Telemetry
constructor
A new instance of Telemetry.
- #merge!(other) ⇒ Object
-
#start(_key) ⇒ Object
Start a timer.
Constructor Details
#initialize(enabled: true) ⇒ Telemetry
Returns a new instance of Telemetry.
6 7 8 9 10 11 |
# File 'lib/purplelight/telemetry.rb', line 6 def initialize(enabled: true) @enabled = enabled @counters = Hash.new(0) @timers = Hash.new(0.0) @mutex = Mutex.new end |
Instance Attribute Details
#counters ⇒ Object (readonly)
Returns the value of attribute counters.
46 47 48 |
# File 'lib/purplelight/telemetry.rb', line 46 def counters @counters end |
#timers ⇒ Object (readonly)
Returns the value of attribute timers.
46 47 48 |
# File 'lib/purplelight/telemetry.rb', line 46 def timers @timers end |
Instance Method Details
#add(key, count = 1) ⇒ Object
32 33 34 35 36 |
# File 'lib/purplelight/telemetry.rb', line 32 def add(key, count = 1) return unless @enabled @counters[key] += count end |
#enabled? ⇒ Boolean
13 14 15 |
# File 'lib/purplelight/telemetry.rb', line 13 def enabled? @enabled end |
#finish(key, ticket) ⇒ Object
Finish a timer using a ticket from start. No-ops if ticket is nil.
25 26 27 28 29 30 |
# File 'lib/purplelight/telemetry.rb', line 25 def finish(key, ticket) return unless @enabled && ticket dt = Process.clock_gettime(Process::CLOCK_MONOTONIC) - ticket @timers[key] += dt end |
#merge!(other) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/purplelight/telemetry.rb', line 38 def merge!(other) return self unless @enabled other.counters.each { |k, v| @counters[k] += v } other.timers.each { |k, v| @timers[k] += v } self end |
#start(_key) ⇒ Object
Start a timer. Returns a ticket (Float) when enabled, or nil when disabled.
18 19 20 21 22 |
# File 'lib/purplelight/telemetry.rb', line 18 def start(_key) return nil unless @enabled Process.clock_gettime(Process::CLOCK_MONOTONIC) end |