Class: RackServerTiming::Recorder
- Inherits:
-
Object
- Object
- RackServerTiming::Recorder
- Defined in:
- lib/rack-server-timing/recorder.rb
Instance Attribute Summary collapse
-
#metrics ⇒ Object
readonly
Returns the value of attribute metrics.
Instance Method Summary collapse
-
#benchmark(name = nil, description = nil, **kwargs, &block) ⇒ Object
Record the metric of :name: with an optional :description:, evaluating the provided block for :duration:.
- #header_name ⇒ Object
- #header_value ⇒ Object
-
#increment(name = nil, duration = nil, description = nil, **kwargs) ⇒ Object
Increment the metric of :name: by :duration:, creating the metric if it did not already exist.
-
#initialize ⇒ Recorder
constructor
A new instance of Recorder.
-
#record(name = nil, duration = nil, description = nil, **kwargs) ⇒ Object
Record the metric of :name: with :duration: and optionally :description:.
Constructor Details
#initialize ⇒ Recorder
Returns a new instance of Recorder.
9 10 11 |
# File 'lib/rack-server-timing/recorder.rb', line 9 def initialize @metrics = {} end |
Instance Attribute Details
#metrics ⇒ Object (readonly)
Returns the value of attribute metrics.
7 8 9 |
# File 'lib/rack-server-timing/recorder.rb', line 7 def metrics @metrics end |
Instance Method Details
#benchmark(name = nil, description = nil, **kwargs, &block) ⇒ Object
Record the metric of :name: with an optional :description:, evaluating the provided block for :duration:.
Accepts arguments as positional arguments or keyword arguments.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/rack-server-timing/recorder.rb', line 52 def benchmark(name = nil, description = nil, **kwargs, &block) result = nil name ||= kwargs[:name] description ||= kwargs[:description] duration = Benchmark.realtime { result = block.call } metrics[name] = Metric.new( name: name, duration: duration, description: description, ) result end |
#header_name ⇒ Object
67 68 69 |
# File 'lib/rack-server-timing/recorder.rb', line 67 def header_name "Server-Timing" end |
#header_value ⇒ Object
71 72 73 |
# File 'lib/rack-server-timing/recorder.rb', line 71 def header_value metrics.map {|_name, metric| metric.formatted }.join(", ") end |
#increment(name = nil, duration = nil, description = nil, **kwargs) ⇒ Object
Increment the metric of :name: by :duration:, creating the metric if it did not already exist.
Accepts arguments as positional arguments or keyword arguments.
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/rack-server-timing/recorder.rb', line 18 def increment(name = nil, duration = nil, description = nil, **kwargs) name = kwargs[:name] if name.nil? duration = kwargs[:duration] if duration.nil? description = kwargs[:description] if description.nil? if (metric = metrics[name]) metric.increment(duration) else metric = Metric.new(name: name, duration: duration, description: description) metrics[name] = metric end end |
#record(name = nil, duration = nil, description = nil, **kwargs) ⇒ Object
Record the metric of :name: with :duration: and optionally :description:.
Accepts arguments as positional arguments or keyword arguments.
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/rack-server-timing/recorder.rb', line 35 def record(name = nil, duration = nil, description = nil, **kwargs) name ||= kwargs[:name] duration ||= kwargs[:duration] description ||= kwargs[:description] metrics[name] = Metric.new( name: name, duration: duration, description: description ) end |