Class: Yabeda::Metric

Inherits:
Object
  • Object
show all
Extended by:
Dry::Initializer
Defined in:
lib/yabeda/metric.rb

Overview

Base class for all metrics

Direct Known Subclasses

Counter, Gauge, Histogram, Summary

Instance Attribute Summary collapse

Instance Method Summary collapse

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)

Instance Attribute Details

#adapterObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Redefined option reader to get group-level adapter if not set on metric level



18
# File 'lib/yabeda/metric.rb', line 18

option :adapter, optional: true, comment: "Monitoring system adapter to register metric in and report metric values to (other adapters won't be used)"

#aggregationObject (readonly)

How adapters should aggregate values from different processes

Reader method for the aggregation initializer parameter.



16
# File 'lib/yabeda/metric.rb', line 16

option :aggregation, optional: true, comment: "How adapters should aggregate values from different processes"

#commentObject (readonly)

Documentation string. Required by some adapters.

Reader method for the comment initializer parameter.



11
# File 'lib/yabeda/metric.rb', line 11

option :comment, optional: true, comment: "Documentation string. Required by some adapters."

#groupObject (readonly)

Category name for grouping metrics

Reader method for the group initializer parameter.



15
# File 'lib/yabeda/metric.rb', line 15

option :group,   optional: true, comment: "Category name for grouping metrics"

#nameObject (readonly)

Metric name. Use snake_case. E.g. job_runtime

Reader method for the name initializer parameter.



10
# File 'lib/yabeda/metric.rb', line 10

param  :name,                    comment: "Metric name. Use snake_case. E.g. job_runtime"

#perObject (readonly)

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

Reader method for the per initializer parameter.



14
# File 'lib/yabeda/metric.rb', line 14

option :per,     optional: true, comment: "Per which unit is measured `unit`. E.g. `call` as in seconds per call"

#tagsArray<Symbol> (readonly)

Returns allowed tags for metric (with account for global and group-level default_tags)

Returns:

  • (Array<Symbol>)


32
# File 'lib/yabeda/metric.rb', line 32

option :tags,    optional: true, comment: "Allowed labels to be set. Required by some adapters."

#unitObject (readonly)

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

Reader method for the unit initializer parameter.



13
# File 'lib/yabeda/metric.rb', line 13

option :unit,    optional: true, comment: "In which units it is measured. E.g. `seconds`"

Instance Method Details

#adaptersHash<Symbol, Yabeda::BaseAdapter>

Returns the metric adapters

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/yabeda/metric.rb', line 42

def adapters
  return ::Yabeda.adapters unless adapter

  @adapters ||= begin
    adapter_names = Array(adapter)
    unknown_adapters = adapter_names - ::Yabeda.adapters.keys

    if unknown_adapters.any?
      raise ConfigurationError,
            "invalid adapter option #{adapter.inspect} in metric #{inspect}"
    end

    ::Yabeda.adapters.slice(*adapter_names)
  end
end

#get(labels = {}) ⇒ Object

Returns the value for the given label set



22
23
24
# File 'lib/yabeda/metric.rb', line 22

def get(labels = {})
  values[::Yabeda::Tags.build(labels, group)]
end

#inspectObject



36
37
38
# File 'lib/yabeda/metric.rb', line 36

def inspect
  "#<#{self.class.name}: #{[@group, @name].compact.join('.')}>"
end

#valuesObject



26
27
28
# File 'lib/yabeda/metric.rb', line 26

def values
  @values ||= Concurrent::Hash.new
end