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)

  • values (Object) — default:

Instance Attribute Details

#adapterObject (readonly)

Redefined option reader to get null adapter if metric is excluded from the group or from the only list in group, group-level adapter if not set on metric level, and adapter on metric level if set



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>)


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

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`"

#valuesObject (readonly)

Reader method for the values initializer parameter.



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

option :values, optional: true, default: proc { Concurrent::Hash.new }

Instance Method Details

#adaptersHash<Symbol, Yabeda::BaseAdapter>

Returns the metric adapters

Returns:



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/yabeda/metric.rb', line 39

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



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

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

#increment_value(labels = {}, by: 1) ⇒ Object

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.

Atomically increment the stored value, assumed to be given all labels, including group labels



71
72
73
74
# File 'lib/yabeda/metric.rb', line 71

def increment_value(labels = {}, by: 1)
  atomic_value = values[labels] ||= Concurrent::Atom.new(0)
  atomic_value.swap { |prev| prev + by }
end

#inspectObject



33
34
35
# File 'lib/yabeda/metric.rb', line 33

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