class Atatus::Metrics::Set

@api private

Constants

DISTINCT_LABEL_LIMIT

Attributes

metrics[R]

Public Class Methods

new(config) click to toggle source
# File lib/atatus/metrics/set.rb, line 31
def initialize(config)
  @config = config
  @metrics = {}
  @disabled = false
  @lock = Mutex.new
end

Public Instance Methods

collect() click to toggle source
# File lib/atatus/metrics/set.rb, line 88
def collect
  return if disabled?

  @lock.synchronize do
    metrics.each_with_object({}) do |(key, metric), sets|
      next unless (value = metric.collect)

      # metrics have a key of name and flat array of key-value pairs
      #   eg [name, key, value, key, value]
      # they can be sent in the same metricset but not if they have
      # differing tags. So, we split the resulting sets by tags first.
      name, *tags = key
      sets[tags] ||= Metricset.new

      # then we set the `samples` value for the metricset
      set = sets[tags]
      set.samples[name] = value

      # and finally we copy the tags from the Metric to the Metricset
      set.merge_tags! metric.tags
    end.values
  end
end
counter(key, tags: nil, **args) click to toggle source
# File lib/atatus/metrics/set.rb, line 48
def counter(key, tags: nil, **args)
  metric(Counter, key, tags: tags, **args)
end
disable!() click to toggle source
# File lib/atatus/metrics/set.rb, line 40
def disable!
  @disabled = true
end
disabled?() click to toggle source
# File lib/atatus/metrics/set.rb, line 44
def disabled?
  @disabled
end
gauge(key, tags: nil, **args) click to toggle source
# File lib/atatus/metrics/set.rb, line 52
def gauge(key, tags: nil, **args)
  metric(Gauge, key, tags: tags, **args)
end
metric(kls, key, tags: nil, **args) click to toggle source
# File lib/atatus/metrics/set.rb, line 60
def metric(kls, key, tags: nil, **args)
  if @config.disable_metrics.any? { |p| p.match? key }
    return NOOP
  end

  key = key_with_tags(key, tags)
  return metrics[key] if metrics[key]

  @lock.synchronize do
    return metrics[key] if metrics[key]

    metrics[key] =
      if metrics.length < DISTINCT_LABEL_LIMIT
        kls.new(key, tags: tags, **args)
      else
        unless @label_limit_logged
          warn(
            'The limit of %d metricsets has been reached, no new ' \
             'metricsets will be created.', DISTINCT_LABEL_LIMIT
          )
          @label_limit_logged = true
        end

        NOOP
      end
  end
end
timer(key, tags: nil, **args) click to toggle source
# File lib/atatus/metrics/set.rb, line 56
def timer(key, tags: nil, **args)
  metric(Timer, key, tags: tags, **args)
end

Private Instance Methods

key_with_tags(key, tags) click to toggle source
# File lib/atatus/metrics/set.rb, line 114
def key_with_tags(key, tags)
  return key unless tags

  tuple = tags.keys.zip(tags.values)
  tuple.flatten!
  tuple.unshift(key)
end