File: metric.rb

package info (click to toggle)
ruby-ddmetrics 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 240 kB
  • sloc: ruby: 791; makefile: 3
file content (37 lines) | stat: -rw-r--r-- 681 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# frozen_string_literal: true

module DDMetrics
  class Metric
    include Enumerable

    def initialize
      @basic_metrics = {}
    end

    def get(label)
      basic_metric_for(label, BasicCounter)
    end

    def labels
      @basic_metrics.keys
    end

    def each
      @basic_metrics.each_key do |label|
        yield(label, get(label))
      end
    end

    # @api private
    def basic_metric_for(label, basic_class)
      @basic_metrics.fetch(label) { @basic_metrics[label] = basic_class.new }
    end

    # @api private
    def validate_label(label)
      return if label.is_a?(Hash)

      raise ArgumentError, 'label argument must be a hash'
    end
  end
end