diff --git a/README.md b/README.md index c1bdf71..d3ad707 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,7 @@ For details of each metric type, see [Prometheus documentation](http://prometheu type counter desc The total number of foo in message. key foo + label_key bar tag ${tag} host ${hostname} @@ -244,10 +245,21 @@ For details of each metric type, see [Prometheus documentation](http://prometheu - `type`: metric type (required) - `desc`: description of this metric (required) - `key`: key name of record for instrumentation (**optional**) +- `label_key`: key name of record with label values (optional) - ``: additional labels for this metric (optional). See [Labels](#labels) If key is empty, the metric values is treated as 1, so the counter increments by 1 on each record regardless of contents of the record. +If label_key is specified, a label with that name will be added with the value from the record. If the value is an array, +a counter for each value is incremented. E.g. if you have `label_key exeception` and a record with `record['exception'] = ['MQException', 'InvalidDestinationException']`, you will get these metrics: + +``` +metric_name{exception="MQException"} 1.0 +metric_name{exception="InvalidDestinationException"} 1.0 +``` + +Other labels will be added as specified. + ### gauge type ``` diff --git a/fluent-plugin-prometheus.gemspec b/fluent-plugin-prometheus.gemspec index 8915534..69bfd0a 100644 --- a/fluent-plugin-prometheus.gemspec +++ b/fluent-plugin-prometheus.gemspec @@ -1,6 +1,6 @@ Gem::Specification.new do |spec| spec.name = "fluent-plugin-prometheus" - spec.version = "1.0.1" + spec.version = "1.1.0" spec.authors = ["Masahiro Sano"] spec.email = ["sabottenda@gmail.com"] spec.summary = %q{A fluent plugin that collects metrics and exposes for Prometheus.} diff --git a/lib/fluent/plugin/prometheus.rb b/lib/fluent/plugin/prometheus.rb index 10e7cb0..5ebc59f 100644 --- a/lib/fluent/plugin/prometheus.rb +++ b/lib/fluent/plugin/prometheus.rb @@ -144,6 +144,7 @@ def instrument(record, expander, placeholders) class Counter < Metric def initialize(element, registry, labels) super + @label_key = element['label_key'] begin @counter = registry.counter(element['name'].to_sym, element['desc']) rescue ::Prometheus::Client::Registry::AlreadyRegisteredError @@ -158,7 +159,14 @@ def instrument(record, expander, placeholders) # ignore if record value is nil return if value.nil? - @counter.increment(labels(record, expander, placeholders), value) + if @label_key + # add label from record + Array(record[@label_key]).each do |label_value| + @counter.increment(labels(record, expander, placeholders).merge(@label_key.to_sym => label_value), value) + end + else + @counter.increment(labels(record, expander, placeholders), value) + end end end