Class: Yabeda::Histogram

Inherits:
Metric
  • Object
show all
Defined in:
lib/yabeda/histogram.rb

Overview

Base class for complex metric for measuring time values that allow to calculate averages, percentiles, and so on.

Instance Attribute Summary collapse

Attributes inherited from Metric

#adapter, #aggregation, #comment, #group, #name, #per, #tags, #unit

Instance Method Summary collapse

Methods inherited from Metric

#adapters, #get, #inspect, #values

Constructor Details

#initialize(name, **options) ⇒ Object

Parameters:

  • name

    Metric name. Use snake_case. E.g. job_runtime

  • options (Hash)

Options Hash (**options):

  • comment (Object) — default: nil

    Documentation string. Required by some adapters.

  • tags (Object) — default: nil

    Allowed labels to be set. Required by some adapters.

  • unit (Object) — default: nil

    In which units it is measured. E.g. ‘seconds`

  • per (Object) — default: nil

    Per which unit is measured ‘unit`. E.g. `call` as in seconds per call

  • group (Object) — default: nil

    Category name for grouping metrics

  • aggregation (Object) — default: nil

    How adapters should aggregate values from different processes

  • adapter (Object) — default: nil

    Monitoring system adapter to register metric in and report metric values to (other adapters won’t be used)

  • buckets (Object)

Instance Attribute Details

#bucketsObject (readonly)

Reader method for the buckets initializer parameter.



7
# File 'lib/yabeda/histogram.rb', line 7

option :buckets

Instance Method Details

#measure(tags = {}, value = nil) ⇒ Object

rubocop: disable Metrics/MethodLength



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/yabeda/histogram.rb', line 10

def measure(tags = {}, value = nil)
  if value.nil? ^ block_given?
    raise ArgumentError, "You must provide either numeric value or block for Yabeda::Histogram#measure!"
  end

  if block_given?
    starting = Process.clock_gettime(Process::CLOCK_MONOTONIC)
    yield
    value = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - starting)
  end

  all_tags = ::Yabeda::Tags.build(tags, group)
  values[all_tags] = value
  adapters.each_value do |adapter|
    adapter.perform_histogram_measure!(self, all_tags, value)
  end
  value
end