fluent-plugin-mackerel 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZmY3Njg2MTIyNGI1Mzc5ODViZmNhMGQ4ZGYzNWE0ZWViMzM1ZTJhOA==
4
+ YmUyN2IyOTAzYjEwYTBmZGViNzkzYWEzYzY0MjMwNjE1MTE1OTFkNw==
5
5
  data.tar.gz: !binary |-
6
- NjM0ZDA1YjNlMjU2OWUyOGRkZjk3YTI1NjVkMjA0ZmE3ZDg2ZDRjZA==
6
+ NTVlZTE0ZDc3N2M0OWY2YTE2ZWE4ZDhjYzU2ZTJkN2RjMzllMmYxOQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZGYzY2ExMjJhZWY4MGM2Y2QwMjE1ZGRlMGIxOTFhMzdkYWM4ZTQ3ZTIwZDM5
10
- MWUxNTQ4MzJhZTAxMmNiOTUxOTQ3YmJiMGY5YWNmMzViYjY1NDdkMzgxZTQw
11
- MWMwNDE1MTZlNmIzNTFjNjUwMzFmYTExNTYwOTE0ZTZiNjgyYTk=
9
+ NGQ1ODE0MjRmZGMzNDlkNmU4YmZhY2RmN2IwYzQ2MDYwNWMyOWY1NGYwZTY4
10
+ NTA5YTc1N2I3NDBhNjU1NGJlODJlODg2ZWNjYTdmNDhhNjIzMjg2ZGI0YThk
11
+ Y2FjMWEyNzVkNDZkOThlN2RiNzQ5MDM5ZTA1MjUxNDk4N2I2ZWU=
12
12
  data.tar.gz: !binary |-
13
- YTFlYjgwOTIyY2M1MGNkN2Q1NjBmZmE5ZDIwODFjMzk2N2ZlMGJiNDk2MWEy
14
- OThiYmRkMWViZTVmNTNmZjQ0ZGJkMjBmY2I2YmFkZTdkOGIwOTY1MzI1Y2E3
15
- YzI3OWU3Y2MxN2JiZDYyOTljNDEwMDA3YmMwMjRjMzdhMmRkZWM=
13
+ OWNjZWZlNjYzZjdmMDBjMWM0MWM2NzJjMWVhNTYwZDMxZTM4YTc4Y2FmZTk0
14
+ MTNlYWNjMTFiOWI1YjIyNGUzZjMwYWNiOTQ2NWI2MGMwM2EyODkyYTk3ZDdj
15
+ YTc5OTk2YjFhMTFhNDBhMDg2Yjk0MjM4Y2Y5ODZiZTZlZjg3Nzc=
data/README.md CHANGED
@@ -4,6 +4,8 @@
4
4
 
5
5
  [Fluentd](http://fluentd.org) plugin to send metrics to [mackerel.io](http://mackerel.io/).
6
6
 
7
+ This plugin includes two components, namely MackerelOutput and MackerelHostidTagOutput. The former is used to send metrics to mackerel and the latter is used to append mackerel hostid to tag or record.
8
+
7
9
  ## Installation
8
10
 
9
11
  Install with gem or fluent-gem command as:
@@ -18,7 +20,7 @@ $ sudo /usr/lib64/fluent/ruby/bin/fluent-gem install fluent-plugin-mackerel
18
20
 
19
21
  ## Configuration
20
22
 
21
- ### Usage
23
+ ### MackerelOutput
22
24
 
23
25
  This plugin uses [APIv0](http://help-ja.mackerel.io/entry/spec/api/v0) of mackerel.io.
24
26
  ```
@@ -26,11 +28,10 @@ This plugin uses [APIv0](http://help-ja.mackerel.io/entry/spec/api/v0) of macker
26
28
  type mackerel
27
29
  api_key 123456
28
30
  hostid xyz
29
- metrics_prefix custom.http_status
31
+ metrics_name http_status.${out_key}
30
32
  out_keys 2xx_count,3xx_count,4xx_count,5xx_count
31
33
  </match>
32
34
  ```
33
- When metrics_prefix doesn't start with "custom.", "custom." is automatically appended at its beginning.
34
35
 
35
36
  Then the sent metric data will look like this:
36
37
  ```
@@ -41,10 +42,43 @@ Then the sent metric data will look like this:
41
42
  "value": 100.0
42
43
  }
43
44
  ```
44
- As shown above, metric name will be a concatenation of `metrics_prefix` and `out_keys` values.
45
+ As shown above, metric name will be a concatenation of `metrics_name` and `out_keys` values when you use ${out_key} in metrics_name definition. In addition, "custom" will be appended before sending metrics to mackerel automatically.
45
46
 
47
+ You can also send ["service" metric](http://help-ja.mackerel.io/entry/spec/api/v0#service-metric-value-post) as follows.
48
+ ```
49
+ <match ...>
50
+ type mackerel
51
+ api_key 123456
52
+ service yourservice
53
+ metrics_name http_status.${out_key}
54
+ out_keys 2xx_count,3xx_count,4xx_count,5xx_count
55
+ </match>
56
+ ```
46
57
  `flush_interval` is not allowed to set less than 60 secs not to send API requests more than once in a minute.
47
58
 
59
+ Since version 0.0.4, metrics_prefix was removed and you should use metrics_name instead.
60
+
61
+ ### MackerelHostidTagOutput
62
+
63
+ If you want to add the hostid to the record with a certain key name, do the following.
64
+ ```
65
+ <match ...>
66
+ type mackerel_hostid_tag
67
+ add_to record
68
+ key_name mackerel_hostid
69
+ </match>
70
+ ```
71
+ As shown above, key_name field is required. Supposed host_id is "xyz" and input is `["test", 1407650400, {"val1"=>1, "val2"=>2, "val3"=>3, "val4"=>4}]`, then you can get `["test", 1407650400, {"val1"=>1, "val2"=>2, "val3"=>3, "val4"=>4, "mackerel_hostid"=>"xyz"}]`
72
+
73
+ To append hostid to the tag, you can simply configure "add_to" as "tag" like this.
74
+ ```
75
+ <match ...>
76
+ type mackerel_hostid_tag
77
+ add_to tag
78
+ </match>
79
+ ```
80
+ When input is `["test", 1407650400, {"val1"=>1, "val2"=>2, "val3"=>3, "val4"=>4}]`, then the output will be `["test.xyz", 1407650400, {"val1"=>1, "val2"=>2, "val3"=>3, "val4"=>4}]`
81
+
48
82
  ## TODO
49
83
 
50
84
  Pull requests are very welcome!!
@@ -4,8 +4,8 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "fluent-plugin-mackerel"
7
- spec.version = "0.0.3"
8
- spec.authors = ["tksmd"]
7
+ spec.version = "0.0.4"
8
+ spec.authors = ["tksmd","hatz48"]
9
9
  spec.email = ["someda@isenshi.com"]
10
10
  spec.description = %q{fluent plugin to send metrics to mackerel.io}
11
11
  spec.summary = spec.description
@@ -17,6 +17,8 @@ Gem::Specification.new do |spec|
17
17
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
18
  spec.require_paths = ["lib"]
19
19
 
20
+ spec.add_dependency "mackerel-client"
21
+
20
22
  spec.add_development_dependency "bundler", "~> 1.3"
21
23
  spec.add_development_dependency "rake"
22
24
  spec.add_development_dependency "rr", ">= 1.0.0"
@@ -1,3 +1,5 @@
1
+ require 'mackerel/client'
2
+
1
3
  module Fluent
2
4
  class MackerelOutput < Fluent::BufferedOutput
3
5
  Fluent::Plugin.register_output('mackerel', self)
@@ -5,14 +7,10 @@ module Fluent
5
7
  config_param :api_key, :string
6
8
  config_param :hostid, :string, :default => nil
7
9
  config_param :hostid_path, :string, :default => nil
10
+ config_param :service, :string, :default => nil
11
+ config_param :metrics_name, :string, :default => nil
8
12
  config_param :out_keys, :string
9
13
 
10
- config_param :metrics_prefix do |val|
11
- val.chomp!(".")
12
- raise Fluent::ConfigError, "metrics_prefix is not allowed to be blank" if val.empty?
13
- val.start_with?("custom.") ? val : "custom." << val
14
- end
15
-
16
14
  attr_reader :mackerel
17
15
 
18
16
  # Define `log` method for v0.10.42 or earlier
@@ -27,23 +25,50 @@ module Fluent
27
25
  def configure(conf)
28
26
  super
29
27
 
30
- @mackerel = Mackerel.new(conf['api_key'])
28
+ @mackerel = Mackerel::Client.new(:mackerel_api_key => conf['api_key'])
31
29
  @out_keys = @out_keys.split(',')
32
30
 
33
31
  if @flush_interval < 60
34
- log.info("flush_interval less than 60s is not allowed and overwriteen to 60s")
32
+ log.info("flush_interval less than 60s is not allowed and overwritten to 60s")
35
33
  @flush_interval = 60
36
34
  end
37
35
 
38
- if @hostid.nil? and @hostid_path.nil?
39
- raise Fluent::ConfigError, "Either 'hostid' or 'hostid_path' must be specifed."
40
- end
41
-
42
36
  unless @hostid_path.nil?
43
37
  @hostid = File.open(@hostid_path).read
44
38
  end
45
39
 
46
- log.info("metrics_prefix is configured to #{@metrics_prefix}")
40
+ if @hostid.nil? and @service.nil?
41
+ raise Fluent::ConfigError, "Either 'hostid' or 'hostid_path' or 'service' must be specifed."
42
+ end
43
+
44
+ if @hostid and @service
45
+ raise Fluent::ConfigError, "Niether 'hostid' and 'service' cannot be specifed."
46
+ end
47
+
48
+ unless @hostid.nil?
49
+ if matched = @hostid.match(/^\${tag_parts\[(\d+)\]}$/)
50
+ hostid_idx = matched[1].to_i
51
+ @hostid_processor = Proc.new{ |args| args[:tokens][hostid_idx] }
52
+ else
53
+ @hostid_processor = Proc.new{ @hostid }
54
+ end
55
+ end
56
+
57
+ if @metrics_name
58
+ @name_processor = @metrics_name.split('.').map{ |token|
59
+ if token.start_with?('$')
60
+ token = token[2..-2]
61
+ if token == 'out_key'
62
+ Proc.new{ |args| args[:out_key] }
63
+ else
64
+ idx = token.match(/\[(-?\d+)\]/)[1].to_i
65
+ Proc.new{ |args| args[:tokens][idx] }
66
+ end
67
+ else
68
+ Proc.new{ token }
69
+ end
70
+ }
71
+ end
47
72
  end
48
73
 
49
74
  def start
@@ -61,18 +86,35 @@ module Fluent
61
86
  def write(chunk)
62
87
  metrics = []
63
88
  chunk.msgpack_each do |(tag,time,record)|
89
+
90
+ tokens = tag.split('.')
64
91
  out_keys.map do |key|
65
- metrics << {
66
- 'hostId' => @hostid,
92
+ name = @name_processor.nil? ? key :
93
+ @name_processor.map{ |p| p.call(:out_key => key, :tokens => tokens) }.join('.')
94
+
95
+ metric = {
67
96
  'value' => record[key].to_f,
68
97
  'time' => time,
69
- 'name' => "%s.%s" % [@metrics_prefix, key]
98
+ 'name' => "%s.%s" % ['custom', name]
70
99
  }
100
+ metric['hostId'] = @hostid_processor.call(:tokens => tokens) if @hostid
101
+
102
+ metrics << metric
71
103
  end
72
104
  end
73
105
 
74
106
  begin
75
- @mackerel.post_metrics(metrics) unless metrics.empty?
107
+ while true
108
+ partial = metrics.slice!(0, 100)
109
+ if partial.empty?
110
+ break
111
+ end
112
+ if @hostid
113
+ @mackerel.post_metrics(partial)
114
+ else
115
+ @mackerel.post_service_metrics(@service, partial)
116
+ end
117
+ end
76
118
  rescue => e
77
119
  log.error("out_mackerel:", :error_class => e.class, :error => e.message)
78
120
  end
@@ -80,50 +122,4 @@ module Fluent
80
122
  end
81
123
  end
82
124
 
83
- class Mackerel
84
-
85
- USER_AGENT = "fluent-plugin-mackerel Ruby/#{RUBY_VERSION}"
86
-
87
- def initialize(api_key)
88
- require 'net/http'
89
- require 'json'
90
-
91
- @api_key = api_key
92
- @http = Net::HTTP.new('mackerel.io', 443)
93
- @http.use_ssl = true
94
- end
95
-
96
- def post_metrics(metrics)
97
-
98
- wait_for_minute
99
-
100
- req = Net::HTTP::Post.new('/api/v0/tsdb', initheader = {
101
- 'X-Api-Key' => @api_key,
102
- 'Content-Type' =>'application/json',
103
- 'User-Agent' => USER_AGENT
104
- })
105
- req.body = metrics.to_json
106
- res = @http.request(req)
107
-
108
- if res.is_a?(Net::HTTPUnauthorized)
109
- raise MackerelError, "invalid api key used. check api_key in your configuration."
110
- end
111
-
112
- unless res and res.is_a?(Net::HTTPSuccess)
113
- raise MackerelError, "failed to post, code: #{res.code}"
114
- end
115
- end
116
-
117
- def wait_for_minute
118
- # limit request once per minute
119
- wait_secs = @last_posted ? @last_posted + 60 - Fluent::Engine.now : 0
120
- sleep wait_secs if wait_secs > 0
121
- @last_posted = Fluent::Engine.now
122
- wait_secs > 0
123
- end
124
-
125
- end
126
-
127
- class MackerelError < RuntimeError; end
128
-
129
- end
125
+ end
@@ -0,0 +1,60 @@
1
+ module Fluent
2
+
3
+ class MackerelHostidTagOutput < Fluent::Output
4
+ Fluent::Plugin.register_output('mackerel_hostid_tag', self)
5
+
6
+ config_param :hostid_path, :string, :default => '/var/lib/mackerel-agent/id'
7
+ config_param :add_to, :string
8
+ config_param :key_name, :default => nil
9
+ config_param :add_prefix, :string, :default => nil
10
+ config_param :remove_prefix, :string, :default => nil
11
+
12
+ # Define `log` method for v0.10.42 or earlier
13
+ unless method_defined?(:log)
14
+ define_method("log") { $log }
15
+ end
16
+
17
+ def initialize
18
+ super
19
+ end
20
+
21
+ def configure(conf)
22
+ super
23
+ @hostid = File.open(@hostid_path).read
24
+ if @add_to == 'record' and @key_name.nil?
25
+ raise Fluent::ConfigError, "'key_name' must be specified"
26
+ end
27
+ if @remove_prefix
28
+ @removed_prefix_string = @remove_prefix + '.'
29
+ @removed_length = @removed_prefix_string.length
30
+ end
31
+ @added_prefix_string = @add_prefix + '.' unless @add_prefix.nil?
32
+ end
33
+
34
+ def emit(tag, es, chain)
35
+ if @remove_prefix and
36
+ ( (tag.start_with?(@removed_prefix_string) and tag.length > @removed_length) or tag == @remove_prefix)
37
+ tag = tag[@removed_length..-1]
38
+ end
39
+ if tag.length > 0
40
+ tag = @added_prefix_string + tag if @added_prefix_string
41
+ else
42
+ tag = @add_prefix
43
+ end
44
+ if @add_to == 'tag'
45
+ tag = [tag, @hostid].join('.')
46
+ end
47
+
48
+ es.each do |time, record|
49
+ if @add_to == 'record'
50
+ record[@key_name] = @hostid
51
+ end
52
+ Fluent::Engine.emit(tag, time, record)
53
+ end
54
+
55
+ chain.next
56
+ end
57
+
58
+ end
59
+
60
+ end
data/test/helper.rb CHANGED
@@ -24,6 +24,7 @@ unless ENV.has_key?('VERBOSE')
24
24
  end
25
25
 
26
26
  require 'fluent/plugin/out_mackerel'
27
+ require 'fluent/plugin/out_mackerel_hostid_tag'
27
28
 
28
29
  class Test::Unit::TestCase
29
30
  end
@@ -0,0 +1 @@
1
+ xyz
@@ -10,14 +10,14 @@ class MackerelOutputTest < Test::Unit::TestCase
10
10
  type mackerel
11
11
  api_key 123456
12
12
  hostid xyz
13
- metrics_prefix custom.service
13
+ metrics_name service.${out_key}
14
14
  out_keys val1,val2,val3
15
15
  ]
16
16
 
17
17
  CONFIG_NOHOST = %[
18
18
  type mackerel
19
19
  api_key 123456
20
- metrics_prefix service
20
+ metrics_name service.${out_key}
21
21
  out_keys val1,val2,val3
22
22
  ]
23
23
 
@@ -32,16 +32,7 @@ class MackerelOutputTest < Test::Unit::TestCase
32
32
  type mackerel
33
33
  api_key 123456
34
34
  hostid xyz
35
- metrics_prefix custom.service
36
- out_keys val1,val2,val3
37
- flush_interval 1s
38
- ]
39
-
40
- CONFIG_NOT_INSUFFICIENT_PREFIX = %[
41
- type mackerel
42
- api_key 123456
43
- hostid xyz
44
- metrics_prefix service
35
+ metrics_name service.${out_key}
45
36
  out_keys val1,val2,val3
46
37
  flush_interval 1s
47
38
  ]
@@ -67,13 +58,10 @@ class MackerelOutputTest < Test::Unit::TestCase
67
58
  d = create_driver(CONFIG_SMALL_FLUSH_INTERVAL)
68
59
  assert_equal d.instance.instance_variable_get(:@flush_interval), 60
69
60
 
70
- d = create_driver(CONFIG_NOT_INSUFFICIENT_PREFIX)
71
- assert_equal d.instance.instance_variable_get(:@metrics_prefix), "custom.service"
72
-
73
61
  d = create_driver()
74
62
  assert_equal d.instance.instance_variable_get(:@api_key), '123456'
75
63
  assert_equal d.instance.instance_variable_get(:@hostid), 'xyz'
76
- assert_equal d.instance.instance_variable_get(:@metrics_prefix), 'custom.service'
64
+ assert_equal d.instance.instance_variable_get(:@metrics_name), 'service.${out_key}'
77
65
  assert_equal d.instance.instance_variable_get(:@out_keys), ['val1','val2','val3']
78
66
  assert_equal d.instance.instance_variable_get(:@flush_interval), 60
79
67
  end
@@ -96,13 +84,4 @@ class MackerelOutputTest < Test::Unit::TestCase
96
84
  d.run()
97
85
  end
98
86
 
99
- def test_wait_for_minute
100
- d = create_driver()
101
- mackerel = d.instance.mackerel
102
- assert_equal mackerel.wait_for_minute, false
103
- assert_equal mackerel.wait_for_minute, true
104
- sleep 60
105
- assert_equal mackerel.wait_for_minute, false
106
- end
107
-
108
- end
87
+ end
@@ -0,0 +1,243 @@
1
+ # coding: utf-8
2
+
3
+ require 'helper'
4
+
5
+ class MackerelHostidTagOutputTest < Test::Unit::TestCase
6
+
7
+ def setup
8
+ Fluent::Test.setup
9
+ end
10
+
11
+ HOSTID_PATH = File.dirname(__FILE__) + "/hostid"
12
+
13
+ CONFIG = %[
14
+ type mackerel_hostid_tag
15
+ hostid_path #{HOSTID_PATH}
16
+ add_to tag
17
+ ]
18
+
19
+ CONFIG_RECORD = %[
20
+ type mackerel_hostid_tag
21
+ hostid_path #{HOSTID_PATH}
22
+ add_to record
23
+ key_name mackerel_hostid
24
+ ]
25
+
26
+ CONFIG_RECORD_NO_KEY_NAME = %[
27
+ type mackerel_hostid_tag
28
+ hostid_path #{HOSTID_PATH}
29
+ add_to record
30
+ ]
31
+
32
+ CONFIG_TAG_REMOVE = %[
33
+ type mackerel_hostid_tag
34
+ hostid_path #{HOSTID_PATH}
35
+ add_to record
36
+ key_name mackerel_hostid
37
+ remove_prefix test
38
+ ]
39
+
40
+ CONFIG_TAG_ADD = %[
41
+ type mackerel_hostid_tag
42
+ hostid_path #{HOSTID_PATH}
43
+ add_to record
44
+ key_name mackerel_hostid
45
+ add_prefix mackerel
46
+ ]
47
+
48
+ CONFIG_TAG_BOTH = %[
49
+ type mackerel_hostid_tag
50
+ hostid_path #{HOSTID_PATH}
51
+ add_to record
52
+ key_name mackerel_hostid
53
+ remove_prefix test
54
+ add_prefix mackerel
55
+ ]
56
+
57
+ CONFIG_TAG_BOTH_TAG = %[
58
+ type mackerel_hostid_tag
59
+ hostid_path #{HOSTID_PATH}
60
+ add_to tag
61
+ remove_prefix test
62
+ add_prefix mackerel
63
+ ]
64
+
65
+ def create_driver(conf = CONFIG, tag='test')
66
+ Fluent::Test::OutputTestDriver.new(Fluent::MackerelHostidTagOutput, tag).configure(conf)
67
+ end
68
+
69
+ def test_configure
70
+
71
+ assert_raise(Fluent::ConfigError) {
72
+ d = create_driver('')
73
+ }
74
+
75
+ assert_raise(Fluent::ConfigError) {
76
+ d = create_driver(CONFIG_RECORD_NO_KEY_NAME)
77
+ }
78
+
79
+ d = create_driver()
80
+ assert_equal d.instance.instance_variable_get(:@hostid), 'xyz'
81
+ assert_equal d.instance.instance_variable_get(:@add_to), 'tag'
82
+
83
+ d = create_driver(CONFIG_RECORD)
84
+ assert_equal d.instance.instance_variable_get(:@hostid), 'xyz'
85
+ assert_equal d.instance.instance_variable_get(:@add_to), 'record'
86
+ assert_equal d.instance.instance_variable_get(:@key_name), 'mackerel_hostid'
87
+
88
+ end
89
+
90
+ def test_write_tag
91
+
92
+ d = create_driver()
93
+
94
+ ENV["TZ"]="Asia/Tokyo"
95
+ t = Time.strptime('2014-08-10 15:00:00', '%Y-%m-%d %T')
96
+ d.run do
97
+ d.emit({'val1' => 1, 'val2' => 2, 'val3' => 3, 'val4' => 4}, t)
98
+ d.emit({'val1' => 5, 'val2' => 6, 'val3' => 7, 'val4' => 8}, t)
99
+ end
100
+
101
+ emits = d.emits
102
+ assert_equal 2, emits.length
103
+ assert_equal 'test.xyz', emits[0][0] # tag
104
+ assert_equal 1407650400, emits[0][1] # time
105
+ assert_equal 4, emits[0][2].length # record
106
+ assert_equal 1, emits[0][2]["val1"] # record
107
+ assert_equal 2, emits[0][2]["val2"] # record
108
+ assert_equal 3, emits[0][2]["val3"] # record
109
+ assert_equal 4, emits[0][2]["val4"] # record
110
+
111
+ assert_equal 'test.xyz', emits[1][0] # tag
112
+ assert_equal 1407650400, emits[1][1] # time
113
+ assert_equal 4, emits[1][2].length # record
114
+ assert_equal 5, emits[1][2]["val1"] # record
115
+ assert_equal 6, emits[1][2]["val2"] # record
116
+ assert_equal 7, emits[1][2]["val3"] # record
117
+ assert_equal 8, emits[1][2]["val4"] # record
118
+ end
119
+
120
+ def test_write_record
121
+
122
+ d = create_driver(CONFIG_RECORD)
123
+
124
+ ENV["TZ"]="Asia/Tokyo"
125
+ t = Time.strptime('2014-08-10 15:00:00', '%Y-%m-%d %T')
126
+ d.run do
127
+ d.emit({'val1' => 1, 'val2' => 2, 'val3' => 3, 'val4' => 4}, t)
128
+ d.emit({'val1' => 5, 'val2' => 6, 'val3' => 7, 'val4' => 8}, t)
129
+ end
130
+
131
+ emits = d.emits
132
+ assert_equal 2, emits.length
133
+ assert_equal 'test', emits[0][0] # tag
134
+ assert_equal 1407650400, emits[0][1] # time
135
+ assert_equal 5, emits[0][2].length # record length
136
+ assert_equal 1, emits[0][2]["val1"] # record
137
+ assert_equal 2, emits[0][2]["val2"] # record
138
+ assert_equal 3, emits[0][2]["val3"] # record
139
+ assert_equal 4, emits[0][2]["val4"] # record
140
+ assert_equal "xyz", emits[0][2]["mackerel_hostid"] # record
141
+
142
+ assert_equal 'test', emits[1][0] # tag
143
+ assert_equal 1407650400, emits[1][1] # time
144
+ assert_equal 5, emits[1][2].length # record length
145
+ assert_equal 5, emits[1][2]["val1"] # record
146
+ assert_equal 6, emits[1][2]["val2"] # record
147
+ assert_equal 7, emits[1][2]["val3"] # record
148
+ assert_equal 8, emits[1][2]["val4"] # record
149
+ assert_equal "xyz", emits[1][2]["mackerel_hostid"] # record
150
+ end
151
+
152
+ def test_write_record_remove_prefix
153
+
154
+ d = create_driver(CONFIG_TAG_REMOVE, 'test.service')
155
+
156
+ ENV["TZ"]="Asia/Tokyo"
157
+ t = Time.strptime('2014-08-10 15:00:00', '%Y-%m-%d %T')
158
+ d.run do
159
+ d.emit({'val1' => 1, 'val2' => 2, 'val3' => 3, 'val4' => 4}, t)
160
+ end
161
+
162
+ emits = d.emits
163
+ assert_equal 1, emits.length
164
+ assert_equal 'service', emits[0][0] # tag
165
+ assert_equal 1407650400, emits[0][1] # time
166
+ assert_equal 5, emits[0][2].length # record length
167
+ assert_equal 1, emits[0][2]["val1"] # record
168
+ assert_equal 2, emits[0][2]["val2"] # record
169
+ assert_equal 3, emits[0][2]["val3"] # record
170
+ assert_equal 4, emits[0][2]["val4"] # record
171
+ assert_equal "xyz", emits[0][2]["mackerel_hostid"] # record
172
+
173
+ end
174
+
175
+ def test_write_record_add_prefix
176
+
177
+ d = create_driver(CONFIG_TAG_ADD)
178
+
179
+ ENV["TZ"]="Asia/Tokyo"
180
+ t = Time.strptime('2014-08-10 15:00:00', '%Y-%m-%d %T')
181
+ d.run do
182
+ d.emit({'val1' => 1, 'val2' => 2, 'val3' => 3, 'val4' => 4}, t)
183
+ end
184
+
185
+ emits = d.emits
186
+ assert_equal 1, emits.length
187
+ assert_equal 'mackerel.test', emits[0][0] # tag
188
+ assert_equal 1407650400, emits[0][1] # time
189
+ assert_equal 5, emits[0][2].length # record length
190
+ assert_equal 1, emits[0][2]["val1"] # record
191
+ assert_equal 2, emits[0][2]["val2"] # record
192
+ assert_equal 3, emits[0][2]["val3"] # record
193
+ assert_equal 4, emits[0][2]["val4"] # record
194
+ assert_equal "xyz", emits[0][2]["mackerel_hostid"] # record
195
+
196
+ end
197
+
198
+ def test_write_record_remove_and_add_prefix
199
+
200
+ d = create_driver(CONFIG_TAG_BOTH, 'test.service')
201
+
202
+ ENV["TZ"]="Asia/Tokyo"
203
+ t = Time.strptime('2014-08-10 15:00:00', '%Y-%m-%d %T')
204
+ d.run do
205
+ d.emit({'val1' => 1, 'val2' => 2, 'val3' => 3, 'val4' => 4}, t)
206
+ end
207
+
208
+ emits = d.emits
209
+ assert_equal 1, emits.length
210
+ assert_equal 'mackerel.service', emits[0][0] # tag
211
+ assert_equal 1407650400, emits[0][1] # time
212
+ assert_equal 5, emits[0][2].length # record length
213
+ assert_equal 1, emits[0][2]["val1"] # record
214
+ assert_equal 2, emits[0][2]["val2"] # record
215
+ assert_equal 3, emits[0][2]["val3"] # record
216
+ assert_equal 4, emits[0][2]["val4"] # record
217
+ assert_equal "xyz", emits[0][2]["mackerel_hostid"] # record
218
+
219
+ end
220
+
221
+ def test_write_tag_remove_and_add_prefix
222
+
223
+ d = create_driver(CONFIG_TAG_BOTH_TAG, 'test.service')
224
+
225
+ ENV["TZ"]="Asia/Tokyo"
226
+ t = Time.strptime('2014-08-10 15:00:00', '%Y-%m-%d %T')
227
+ d.run do
228
+ d.emit({'val1' => 1, 'val2' => 2, 'val3' => 3, 'val4' => 4}, t)
229
+ end
230
+
231
+ emits = d.emits
232
+ assert_equal 1, emits.length
233
+ assert_equal 'mackerel.service.xyz', emits[0][0] # tag
234
+ assert_equal 1407650400, emits[0][1] # time
235
+ assert_equal 4, emits[0][2].length # record length
236
+ assert_equal 1, emits[0][2]["val1"] # record
237
+ assert_equal 2, emits[0][2]["val2"] # record
238
+ assert_equal 3, emits[0][2]["val3"] # record
239
+ assert_equal 4, emits[0][2]["val4"] # record
240
+
241
+ end
242
+
243
+ end
metadata CHANGED
@@ -1,15 +1,30 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fluent-plugin-mackerel
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - tksmd
8
+ - hatz48
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2014-05-26 00:00:00.000000000 Z
12
+ date: 2014-08-10 00:00:00.000000000 Z
12
13
  dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mackerel-client
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ! '>='
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
13
28
  - !ruby/object:Gem::Dependency
14
29
  name: bundler
15
30
  requirement: !ruby/object:Gem::Requirement
@@ -81,8 +96,11 @@ files:
81
96
  - Rakefile
82
97
  - fluent-plugin-mackerel.gemspec
83
98
  - lib/fluent/plugin/out_mackerel.rb
99
+ - lib/fluent/plugin/out_mackerel_hostid_tag.rb
84
100
  - test/helper.rb
101
+ - test/plugin/hostid
85
102
  - test/plugin/test_out_mackerel.rb
103
+ - test/plugin/test_out_mackerel_hostid_tag.rb
86
104
  homepage: https://github.com/tksmd/fluent-plugin-mackerel
87
105
  licenses:
88
106
  - Apache-2.0
@@ -109,4 +127,6 @@ specification_version: 4
109
127
  summary: fluent plugin to send metrics to mackerel.io
110
128
  test_files:
111
129
  - test/helper.rb
130
+ - test/plugin/hostid
112
131
  - test/plugin/test_out_mackerel.rb
132
+ - test/plugin/test_out_mackerel_hostid_tag.rb