Class: Yabeda::Metric
- Inherits:
-
Object
- Object
- Yabeda::Metric
- Extended by:
- Dry::Initializer
- Defined in:
- lib/yabeda/metric.rb
Overview
Base class for all metrics
Instance Attribute Summary collapse
-
#adapter ⇒ Object
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.
-
#aggregation ⇒ Object
readonly
How adapters should aggregate values from different processes.
-
#comment ⇒ Object
readonly
Documentation string.
-
#group ⇒ Object
readonly
Category name for grouping metrics.
-
#name ⇒ Object
readonly
Metric name.
-
#per ⇒ Object
readonly
Per which unit is measured
unit. -
#tags ⇒ Array<Symbol>
readonly
Returns allowed tags for metric (with account for global and group-level
default_tags). -
#unit ⇒ Object
readonly
In which units it is measured.
-
#values ⇒ Object
readonly
Reader method for the
valuesinitializer parameter.
Instance Method Summary collapse
-
#adapters ⇒ Hash<Symbol, Yabeda::BaseAdapter>
Returns the metric adapters.
-
#get(labels = {}) ⇒ Object
Returns the value for the given label set.
-
#increment_value(labels = {}, by: 1) ⇒ Object
private
Atomically increment the stored value, assumed to be given all labels, including group labels.
- #initialize(name, **options) ⇒ Object constructor
- #inspect ⇒ Object
Constructor Details
#initialize(name, **options) ⇒ Object
Instance Attribute Details
#adapter ⇒ Object (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)" |
#aggregation ⇒ Object (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" |
#comment ⇒ Object (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." |
#group ⇒ Object (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" |
#name ⇒ Object (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" |
#per ⇒ Object (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" |
#tags ⇒ Array<Symbol> (readonly)
Returns allowed tags for metric (with account for global and group-level default_tags)
29 |
# File 'lib/yabeda/metric.rb', line 29 option :tags, optional: true, comment: "Allowed labels to be set. Required by some adapters." |
#unit ⇒ Object (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`" |
#values ⇒ Object (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
#adapters ⇒ Hash<Symbol, Yabeda::BaseAdapter>
Returns the metric adapters
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 |
#inspect ⇒ Object
33 34 35 |
# File 'lib/yabeda/metric.rb', line 33 def inspect "#<#{self.class.name}: #{[@group, @name].compact.join('.')}>" end |