|
| 1 | +# Plugin Helper: Metrics |
| 2 | + |
| 3 | +The `metrics` plugin helper manages the metrics values in plugins. |
| 4 | + |
| 5 | +Here is an example: |
| 6 | + |
| 7 | +```ruby |
| 8 | +require 'fluent/plugin/input' |
| 9 | + |
| 10 | +module Fluent::Plugin |
| 11 | + class ExampleInput < Input |
| 12 | + Fluent::Plugin.register_input('example', self) |
| 13 | + |
| 14 | + # 1. Load metrics helper |
| 15 | + helpers :metrics |
| 16 | + |
| 17 | + def configure(conf) |
| 18 | + super |
| 19 | + |
| 20 | + # 2. Create parser plugin instance |
| 21 | + @metrics = metrics_create(namespace: "fluentd", subsystem: "input", name: "example", help_text: "Example metrics") |
| 22 | + end |
| 23 | + |
| 24 | + def start |
| 25 | + super |
| 26 | + |
| 27 | + # 3. Increase metrics value |
| 28 | + @metrics.inc |
| 29 | + |
| 30 | + end |
| 31 | + |
| 32 | + def statistics |
| 33 | + stats = super |
| 34 | + |
| 35 | + # 4. Retrieve metrics value |
| 36 | + stats = { |
| 37 | + 'input' => stats["input"].merge({ 'example' => @metrics.get }) |
| 38 | + } |
| 39 | + stats |
| 40 | + end |
| 41 | +end |
| 42 | +``` |
| 43 | + |
| 44 | +For more details, see the following articles: |
| 45 | + |
| 46 | +* [Metrics Plugins Overview](../metrics/) |
| 47 | + |
| 48 | +## Methods |
| 49 | + |
| 50 | +### `metrics_create(namespace: "fluentd", subsystem: "metrics", name:, help_text:, labels: {}, prefer_gauge: false)` |
| 51 | + |
| 52 | +This method creates a metrics instance. |
| 53 | + |
| 54 | +* `namespace`: The namespace for the metrics. |
| 55 | +* `subsystem`: The names that represent specific functions or components. |
| 56 | +* `name`: The metrics name. |
| 57 | +* `help_text`: The description for metrics. |
| 58 | +* `labels`: The key/value pair for metrics labels. |
| 59 | +* `prefer_gauge`: Use gauge instead of counter for the metrics if `true`. |
| 60 | + |
| 61 | +Since 1.19.0, `metrics_create` method generates a getter method with the specified `name` on the calling instance. |
| 62 | + |
| 63 | +## Plugins using `metrics` |
| 64 | + |
| 65 | +* [`buffer`](../buffer/) |
| 66 | +* [`filter`](../filter/) |
| 67 | +* [`input`](../input/) |
| 68 | +* [`output`](../output/) |
| 69 | + |
| 70 | +If this article is incorrect or outdated, or omits critical information, please [let us know](https://github.com/fluent/fluentd-docs-gitbook/issues?state=open). [Fluentd](http://www.fluentd.org/) is an open-source project under [Cloud Native Computing Foundation \(CNCF\)](https://cncf.io/). All components are available under the Apache 2 License. |
| 71 | + |
0 commit comments