fluent-plugin-cmetrics 0.1.0.rc → 0.1.0.rc5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 69a99953b806c28c73bf3e734c2419e3ce5df8974e6bdd12a93d0e0d51096ceb
4
- data.tar.gz: bfe77f7b81c5f10ce5657bd5ea24b30c29f2ebd9b62fe5d10f82988d06f2e85c
3
+ metadata.gz: 14dc765336d42503cd80ded601ff4c275a38cec0cc754b31f1f1a86d333520ac
4
+ data.tar.gz: 89cac70e738c7b3a4d65189be8a78ab9cba97210864e38e8ef47bf11aa5c7940
5
5
  SHA512:
6
- metadata.gz: 82b2b4e6bcc4f95c02e4eabfe000f6010f8e2ee07a7b7491057e30d39fc2bb9cc85555a3f7c9da0a60a901f2faba10882694ee1c08e8bf928614140302b330a6
7
- data.tar.gz: ab04d2523a2ed3bec53859b419b0ac77d25fd27dfbc7462fa7ece07bb8b63cd0f8889cd89d027414b9713f2f3f30caad8d9a96eb3e0015cd6a324dc19b062bee
6
+ metadata.gz: e11f1f3c56baba25406a0294e8c84458c26f15d1070b3f9a125838e9ac536386f32b705a5cbcbbddf242234a728be397bb0f12e5dd8e72c89a09917e4799bc69
7
+ data.tar.gz: 28a10bb177b9758ac19a25abce5f04746051fb26e23842b6eef488b18c390e309ddba487656fb5df01c6975a0b34a4d5a829a1ec5add63be5f58b5fabb42ad36
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # fluent-plugin-cmetrics
2
2
 
3
- [Fluentd](https://fluentd.org/) parser plugin to do something.
4
-
5
- TODO: write description for you plugin.
3
+ [Fluentd](https://fluentd.org/) plugin for cmetrics format handling.
6
4
 
7
5
  ## Installation
8
6
 
@@ -26,15 +24,71 @@ And then execute:
26
24
  $ bundle
27
25
  ```
28
26
 
29
- ## Configuration
27
+ ## Plugin helpers
30
28
 
31
- You can generate configuration template:
29
+ * [record_accessor](https://docs.fluentd.org/v/1.0/plugin-helper-overview/api-plugin-helper-record_accessor)
32
30
 
33
- ```
34
- $ fluent-plugin-config-format parser cmetrics
35
- ```
31
+ * See also: [Filter Plugin Overview](https://docs.fluentd.org/v/1.0/filter#overview)
32
+
33
+ ## Fluent::Plugin::CMetricsParserFilter
34
+
35
+ ### cmetrics_metric_key (string) (optional)
36
+
37
+ cmetrics metric key
38
+
39
+ Default value: `cmetrics`.
40
+
41
+ ### cmetrics_labels_key (string) (optional)
42
+
43
+ cmetrics labels key
44
+
45
+ Default value: `labels`.
46
+
47
+ ### format_to_splunk_metric (bool) (optional)
48
+
49
+ format to Splunk metrics
50
+
51
+ ### dimensions_key (string) (optional)
52
+
53
+ dimensions key
54
+
55
+ ## Fluent::Plugin::CMetricsSplunkMetricPayloadFormatter
56
+
57
+ ### cmetrics_name_key (string) (optional)
58
+
59
+ cmetrics metrics name key
60
+
61
+ Default value: `name`.
62
+
63
+ ### cmetrics_value_key (string) (optional)
64
+
65
+ cmetrics metrics value key
66
+
67
+ Default value: `value`.
68
+
69
+ ### cmetrics_dims_key (string) (optional)
70
+
71
+ cmetrics metrics dimensions key
72
+
73
+ Default value: `dims`.
74
+
75
+ ### host_key (string) (optional)
76
+
77
+ Specify host key
78
+
79
+ Default value: `host`.
80
+
81
+ ### index (string) (optional)
82
+
83
+ Specify splunk index name
84
+
85
+ ### source (string) (optional)
86
+
87
+ Specify splunk source name
88
+
89
+ ### sourcetype (string) (optional)
36
90
 
37
- You can copy and paste generated documents here.
91
+ Specify splunk sourcetype name
38
92
 
39
93
  ## Copyright
40
94
 
@@ -3,7 +3,7 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
 
4
4
  Gem::Specification.new do |spec|
5
5
  spec.name = "fluent-plugin-cmetrics"
6
- spec.version = "0.1.0.rc"
6
+ spec.version = "0.1.0.rc5"
7
7
  spec.authors = ["Hiroshi Hatake"]
8
8
  spec.email = ["cosmo0920.oucc@gmail.com"]
9
9
 
@@ -27,17 +27,32 @@ module Fluent
27
27
  helpers :record_accessor
28
28
 
29
29
  desc "cmetrics metric key"
30
- config_param :cmetric_metric_key, :string, default: "cmetric"
30
+ config_param :cmetrics_metric_key, :string, default: "cmetrics"
31
31
  desc "cmetrics labels key"
32
- config_param :cmetric_labels_key, :string, default: "labels"
33
- desc "format name key for Splunk metrics"
34
- config_param :format_name_key_for_splunk_metric, :bool, default: false
32
+ config_param :cmetrics_labels_key, :string, default: "labels"
33
+ desc "format to Splunk metrics"
34
+ config_param :format_to_splunk_metric, :bool, default: false
35
+ desc "dimensions key"
36
+ config_param :dimensions_key, :string, default: nil
35
37
 
36
38
  def configure(conf)
37
39
  super
38
40
  @serde = ::CMetrics::Serde.new
39
- @record_accessor = record_accessor_create(@cmetric_metric_key)
40
- @labels_accessor = record_accessor_create(@cmetric_labels_key)
41
+ @record_accessor = record_accessor_create(@cmetrics_metric_key)
42
+ @labels_accessor = record_accessor_create(@cmetrics_labels_key)
43
+ end
44
+
45
+ def format_to_splunk_style_with_dims(inner)
46
+ subsystem = inner.delete("subsystem")
47
+ # labels will be treated as dimensions.
48
+ dimensions = Hash.new(0)
49
+ if labels = @labels_accessor.call(inner)
50
+ labels.map {|k,v|
51
+ dimensions[k] = v
52
+ }
53
+ end
54
+ name = inner.delete("name")
55
+ return [subsystem, name].compact.reject{|e| e.empty?}.join("."), dimensions
41
56
  end
42
57
 
43
58
  def filter_stream(tag, es)
@@ -50,10 +65,13 @@ module Fluent
50
65
  next if metric.empty?
51
66
 
52
67
  metric.each do |inner|
53
- if @format_name_key_for_splunk_metric && labels = @labels_accessor.call(inner)
54
- name = inner.delete("name")
55
- labels_str = labels.map {|k,v| "#{k}_#{v}"}.join("_")
56
- inner["name"] = "#{labels_str}_#{name}"
68
+ if @format_to_splunk_metric
69
+ inner["name"], dims = format_to_splunk_style_with_dims(inner)
70
+ if @dimensions_key
71
+ inner[@dimensions_key] = dims
72
+ else
73
+ inner.merge!(dims)
74
+ end
57
75
  end
58
76
  time = Time.at(inner.delete("timestamp"))
59
77
  new_es.add(Fluent::EventTime.new(time.to_i, time.nsec), inner)
@@ -0,0 +1,89 @@
1
+ #
2
+ # Copyright 2021- Calyptia Inc.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ require "fluent/plugin/formatter"
17
+ require 'fluent/plugin_helper/record_accessor'
18
+ require 'fluent/plugin_helper'
19
+ require "fluent/event"
20
+ require "fluent/time"
21
+ require "yajl"
22
+ require "socket"
23
+
24
+ module Fluent
25
+ module Plugin
26
+ class CMetricsSplunkMetricPayloadFormatter < Fluent::Plugin::Formatter
27
+ include PluginHelper::Mixin
28
+
29
+ Fluent::Plugin.register_formatter('cmetrics_splunk_metric_payload', self)
30
+
31
+ helpers :record_accessor
32
+
33
+ desc "cmetrics metrics name key"
34
+ config_param :cmetrics_name_key, :string, default: "name"
35
+ desc "cmetrics metrics value key"
36
+ config_param :cmetrics_value_key, :string, default: "value"
37
+ desc "cmetrics metrics dimensions key"
38
+ config_param :cmetrics_dims_key, :string, default: "dims"
39
+ desc "Specify host key"
40
+ config_param :host_key, :string, default: "host"
41
+ desc "Specify splunk index name"
42
+ config_param :index, :string, default: nil
43
+ desc "Specify splunk source name"
44
+ config_param :source, :string, default: nil
45
+ desc "Specify splunk sourcetype name"
46
+ config_param :sourcetype, :string, default: nil
47
+
48
+ def initialize
49
+ super
50
+ @default_host = Socket.gethostname
51
+ end
52
+
53
+ def configure(conf)
54
+ super
55
+
56
+ @cmetrics_name_accessor = record_accessor_create(@cmetrics_name_key)
57
+ @cmetrics_value_accessor = record_accessor_create(@cmetrics_value_key)
58
+ @cmetrics_dims_accessor = record_accessor_create(@cmetrics_dims_key)
59
+ @host_key_accessor = record_accessor_create(@host_key)
60
+ end
61
+
62
+ def format(tag, time, record)
63
+ host = if host = @host_key_accessor.call(record)
64
+ host
65
+ else
66
+ @default_host
67
+ end
68
+ payload = {
69
+ host: host,
70
+ # From the API reference
71
+ # https://docs.splunk.com/Documentation/Splunk/latest/RESTREF/RESTinput#services.2Fcollector
72
+ time: time.to_f.to_s,
73
+ event: 'metric',
74
+ }
75
+ payload[:index] = @index if @index
76
+ payload[:source] = @source if @source
77
+ payload[:sourcetype] = @sourcetype if @sourcetype
78
+ fields = {
79
+ "metric_name:#{@cmetrics_name_accessor.call(record)}" => @cmetrics_value_accessor.call(record)
80
+ }
81
+ if dims = @cmetrics_dims_accessor.call(record)
82
+ fields.merge!(dims)
83
+ end
84
+ payload.merge!(fields)
85
+ Yajl.dump(payload)
86
+ end
87
+ end
88
+ end
89
+ end
Binary file
data/test/helper.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  $LOAD_PATH.unshift(File.expand_path("../../", __FILE__))
2
2
  require "test-unit"
3
3
  require "fluent/test"
4
- require "fluent/test/driver/parser"
4
+ require "fluent/test/driver/filter"
5
5
  require "fluent/test/helpers"
6
6
 
7
7
  Test::Unit::TestCase.include(Fluent::Test::Helpers)
@@ -6,8 +6,53 @@ class CmetricsParserTest < Test::Unit::TestCase
6
6
  Fluent::Test.setup
7
7
  end
8
8
 
9
- test "failure" do
10
- flunk
9
+ data("cpu" => ['{"namespace":"node","subsystem":"cpu","labels":{"cpu":"10","mode":"system"},"name":"seconds_total","description":"Seconds the CPUs spent in each mode.","value":13153.09}', "cpu.seconds_total", {"cpu" => "10", "mode" => "system"}],
10
+ "filefd" => ['{"namespace":"node","subsystem":"filefd","name":"maximum","description":"File descriptor statistics: maximum.","value":9.223372036854776e+18}', "filefd.maximum", {}],
11
+ "disk" => ['{"namespace":"node","subsystem":"disk","labels":{"device":"nvme0n1"},"name":"io_now","description":"The number of I/Os currently in progress.","value":0.0}', "disk.io_now", {"device" => "nvme0n1"}],
12
+ "network" => ['{"namespace":"node","subsystem":"network","labels":{"device":"eth0"},"name":"transmit_bytes_total","description":"Network device statistic bytes.","value":997193.0}', "network.transmit_bytes_total", {"device" => "eth0"}],
13
+ "none" => ['{"namespace":"node","subsystem":"","name":"load5","description":"5m load average.","value":0.94}', "load5", {}])
14
+ test "#format_record_key_to_splunk_style" do |(json_str, expected_format_key, expected_dims)|
15
+ json = Yajl.load(json_str)
16
+ d = create_driver(%[
17
+ format_to_splunk_metric true
18
+ dimensions_key dims
19
+ ])
20
+ assert_true d.instance.format_to_splunk_metric
21
+ formatted_key, dims = d.instance.format_to_splunk_style_with_dims(json)
22
+ assert_equal expected_format_key, formatted_key
23
+ assert_equal expected_dims, dims
24
+ end
25
+
26
+ sub_test_case "Actual filtering" do
27
+ setup do
28
+ @binary_path = File.join(File.dirname(__dir__), "fixtures", "cmetrics.bin")
29
+ @binary = File.read(@binary_path)
30
+ end
31
+
32
+ data("with dimensions" => "dims",
33
+ "without dimensions" => nil)
34
+ test "#filter_stream" do |data|
35
+ use_dimensions = data
36
+ d = if use_dimensions
37
+ create_driver(%[
38
+ format_to_splunk_metric true
39
+ dimensions_key dims
40
+ ])
41
+ else
42
+ create_driver(%[
43
+ format_to_splunk_metric true
44
+ ])
45
+ end
46
+ time = event_time("2012-01-02 13:14:15")
47
+ record = {"cmetrics" => @binary}
48
+ d.run(default_tag: 'test') do
49
+ d.feed(time, record)
50
+ end
51
+ d.filtered.map {|e| assert_equal(!!use_dimensions, e.last.has_key?("dims"))}
52
+ assert do
53
+ d.filtered.size > 0
54
+ end
55
+ end
11
56
  end
12
57
 
13
58
  private
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-cmetrics
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.rc
4
+ version: 0.1.0.rc5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Hatake
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-08-24 00:00:00.000000000 Z
11
+ date: 2021-09-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -100,6 +100,8 @@ files:
100
100
  - Rakefile
101
101
  - fluent-plugin-cmetrics.gemspec
102
102
  - lib/fluent/plugin/filter_cmetrics_parser.rb
103
+ - lib/fluent/plugin/formatter_cmetrics_splunk_metric_payload.rb
104
+ - test/fixtures/cmetrics.bin
103
105
  - test/helper.rb
104
106
  - test/plugin/test_filter_cmetrics_parser.rb
105
107
  homepage: https://github.com/calyptia/fluent-plugin-cmetrics
@@ -126,5 +128,6 @@ signing_key:
126
128
  specification_version: 4
127
129
  summary: Fluentd plugin for cmetrics format handling.
128
130
  test_files:
131
+ - test/fixtures/cmetrics.bin
129
132
  - test/helper.rb
130
133
  - test/plugin/test_filter_cmetrics_parser.rb