Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added label_key option for counters #47

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<labels>
tag ${tag}
host ${hostname}
Expand All @@ -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)
- `<labels>`: 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

```
Expand Down
2 changes: 1 addition & 1 deletion fluent-plugin-prometheus.gemspec
Original file line number Diff line number Diff line change
@@ -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 = ["[email protected]"]
spec.summary = %q{A fluent plugin that collects metrics and exposes for Prometheus.}
Expand Down
10 changes: 9 additions & 1 deletion lib/fluent/plugin/prometheus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down