lib/frankenstein/collected_metric.rb in frankenstein-1.2.0 vs lib/frankenstein/collected_metric.rb in frankenstein-2.0.0
- old
+ new
@@ -63,10 +63,13 @@
# meet [the guidelines for metric naming](https://prometheus.io/docs/practices/naming/),
# unless you like being shunned at parties.
#
# @param docstring [#to_s] the descriptive help text for the metric.
#
+ # @param labels [Array<Symbol>] the labels which all time series for this
+ # metric must possess.
+ #
# @param type [Symbol] what type of metric you're returning. It's uncommon
# to want anything other than `:gauge` here (the default), because
# when you're collecting external data it's unlikely you'll be able to
# trust that your external data source will behave like a proper
# counter (or histogram or summary), but if you want the flexibility,
@@ -85,12 +88,12 @@
# metric will also be registered here, so you'll know if a collection
# fails.
#
# @param collector [Proc] the code to run on every scrape request.
#
- def initialize(name, docstring, type: :gauge, logger: Logger.new('/dev/null'), registry: Prometheus::Client.registry, &collector)
- @validator = Prometheus::Client::LabelSetValidator.new
+ def initialize(name, docstring:, labels: [], type: :gauge, logger: Logger.new('/dev/null'), registry: Prometheus::Client.registry, &collector)
+ @validator = Prometheus::Client::LabelSetValidator.new(expected_labels: labels)
validate_name(name)
validate_docstring(docstring)
@name = name
@@ -102,18 +105,18 @@
@type = type
@logger = logger
@registry = registry
@collector = collector
- @errors_metric = @registry.counter(:"#{@name}_collection_errors_total", "Errors encountered while collecting for #{@name}")
+ @errors_metric = @registry.counter(:"#{@name}_collection_errors_total", docstring: "Errors encountered while collecting for #{@name}")
@registry.register(self)
end
# Retrieve the value for the given labelset.
#
def get(labels = {})
- @validator.validate(labels)
+ @validator.validate_labelset!(labels)
values[labels]
end
# Retrieve a complete set of labels and values for the metric.
@@ -124,10 +127,10 @@
unless results.is_a?(Hash)
@logger.error(progname) { "Collector proc did not return a hash, got #{results.inspect}" }
@errors_metric.increment(class: "NotAHashError")
return {}
end
- results.keys.each { |labelset| @validator.validate(labelset) }
+ results.keys.each { |labelset| @validator.validate_labelset!(labelset) }
end
rescue StandardError => ex
@logger.error(progname) { (["Exception in collection: #{ex.message} (#{ex.class})"] + ex.backtrace).join("\n ") }
@errors_metric.increment(class: ex.class.to_s)