fluentd 1.13.0 → 1.14.0.rc

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of fluentd might be problematic. Click here for more details.

Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/.github/ISSUE_TEMPLATE/bug_report.yaml +69 -0
  3. data/.github/ISSUE_TEMPLATE/feature_request.yaml +38 -0
  4. data/.github/workflows/linux-test.yaml +1 -1
  5. data/.github/workflows/windows-test.yaml +1 -1
  6. data/.gitlab-ci.yml +0 -22
  7. data/CHANGELOG.md +87 -0
  8. data/README.md +2 -2
  9. data/example/v0_12_filter.conf +2 -2
  10. data/fluentd.gemspec +1 -1
  11. data/lib/fluent/command/fluentd.rb +8 -0
  12. data/lib/fluent/command/plugin_generator.rb +15 -5
  13. data/lib/fluent/compat/output.rb +9 -6
  14. data/lib/fluent/config.rb +1 -1
  15. data/lib/fluent/config/section.rb +5 -0
  16. data/lib/fluent/config/types.rb +15 -0
  17. data/lib/fluent/config/v1_parser.rb +3 -2
  18. data/lib/fluent/env.rb +2 -1
  19. data/lib/fluent/event_router.rb +28 -1
  20. data/lib/fluent/oj_options.rb +62 -0
  21. data/lib/fluent/plugin.rb +10 -1
  22. data/lib/fluent/plugin/bare_output.rb +49 -8
  23. data/lib/fluent/plugin/buffer.rb +84 -22
  24. data/lib/fluent/plugin/file_wrapper.rb +22 -0
  25. data/lib/fluent/plugin/filter.rb +35 -1
  26. data/lib/fluent/plugin/formatter.rb +1 -0
  27. data/lib/fluent/plugin/formatter_json.rb +9 -7
  28. data/lib/fluent/plugin/in_monitor_agent.rb +4 -2
  29. data/lib/fluent/plugin/in_syslog.rb +13 -1
  30. data/lib/fluent/plugin/in_tail.rb +45 -4
  31. data/lib/fluent/plugin/in_tail/position_file.rb +20 -18
  32. data/lib/fluent/plugin/input.rb +39 -1
  33. data/lib/fluent/plugin/metrics.rb +119 -0
  34. data/lib/fluent/plugin/metrics_local.rb +96 -0
  35. data/lib/fluent/plugin/multi_output.rb +43 -6
  36. data/lib/fluent/plugin/out_forward.rb +1 -3
  37. data/lib/fluent/plugin/output.rb +74 -33
  38. data/lib/fluent/plugin/parser_json.rb +2 -3
  39. data/lib/fluent/plugin/service_discovery.rb +0 -15
  40. data/lib/fluent/plugin_helper.rb +1 -0
  41. data/lib/fluent/plugin_helper/event_emitter.rb +8 -1
  42. data/lib/fluent/plugin_helper/http_server/router.rb +1 -1
  43. data/lib/fluent/plugin_helper/metrics.rb +129 -0
  44. data/lib/fluent/plugin_helper/server.rb +4 -2
  45. data/lib/fluent/root_agent.rb +6 -0
  46. data/lib/fluent/supervisor.rb +2 -0
  47. data/lib/fluent/system_config.rb +9 -1
  48. data/lib/fluent/test/driver/storage.rb +30 -0
  49. data/lib/fluent/version.rb +1 -1
  50. data/templates/new_gem/lib/fluent/plugin/storage.rb.erb +40 -0
  51. data/templates/new_gem/test/plugin/test_storage.rb.erb +18 -0
  52. data/test/command/test_cat.rb +11 -8
  53. data/test/command/test_plugin_generator.rb +2 -1
  54. data/test/config/test_section.rb +9 -0
  55. data/test/config/test_system_config.rb +6 -0
  56. data/test/config/test_types.rb +7 -0
  57. data/test/plugin/in_tail/test_position_file.rb +48 -8
  58. data/test/plugin/test_bare_output.rb +13 -0
  59. data/test/plugin/test_buffer.rb +8 -2
  60. data/test/plugin/test_file_wrapper.rb +11 -0
  61. data/test/plugin/test_filter.rb +11 -0
  62. data/test/plugin/test_in_forward.rb +59 -83
  63. data/test/plugin/test_in_http.rb +46 -43
  64. data/test/plugin/test_in_monitor_agent.rb +214 -8
  65. data/test/plugin/test_in_syslog.rb +101 -56
  66. data/test/plugin/test_in_tail.rb +149 -48
  67. data/test/plugin/test_in_tcp.rb +45 -32
  68. data/test/plugin/test_in_udp.rb +47 -33
  69. data/test/plugin/test_input.rb +11 -0
  70. data/test/plugin/test_metrics.rb +294 -0
  71. data/test/plugin/test_metrics_local.rb +96 -0
  72. data/test/plugin/test_multi_output.rb +25 -1
  73. data/test/plugin/test_out_forward.rb +103 -89
  74. data/test/plugin/test_out_stream.rb +18 -8
  75. data/test/plugin/test_output.rb +16 -0
  76. data/test/plugin_helper/http_server/test_route.rb +1 -1
  77. data/test/plugin_helper/test_child_process.rb +1 -1
  78. data/test/plugin_helper/test_event_emitter.rb +29 -0
  79. data/test/plugin_helper/test_http_server_helper.rb +33 -26
  80. data/test/plugin_helper/test_metrics.rb +137 -0
  81. data/test/plugin_helper/test_server.rb +137 -138
  82. data/test/plugin_helper/test_socket.rb +16 -9
  83. data/test/test_event_time.rb +2 -2
  84. data/test/test_oj_options.rb +55 -0
  85. data/test/test_plugin_classes.rb +102 -0
  86. data/test/test_root_agent.rb +30 -1
  87. metadata +27 -12
  88. data/.github/ISSUE_TEMPLATE/bug_report.md +0 -40
  89. data/.github/ISSUE_TEMPLATE/feature_request.md +0 -23
@@ -5,21 +5,33 @@ require 'fluent/plugin/in_tcp'
5
5
  class TcpInputTest < Test::Unit::TestCase
6
6
  def setup
7
7
  Fluent::Test.setup
8
+ @port = unused_port
8
9
  end
9
10
 
10
- PORT = unused_port
11
- BASE_CONFIG = %[
12
- port #{PORT}
13
- tag tcp
14
- ]
15
- CONFIG = BASE_CONFIG + %[
16
- bind 127.0.0.1
17
- format none
18
- ]
19
- IPv6_CONFIG = BASE_CONFIG + %[
20
- bind ::1
21
- format none
22
- ]
11
+ def teardown
12
+ @port = nil
13
+ end
14
+
15
+ def base_config
16
+ %[
17
+ port #{@port}
18
+ tag tcp
19
+ ]
20
+ end
21
+
22
+ def ipv4_config
23
+ base_config + %[
24
+ bind 127.0.0.1
25
+ format none
26
+ ]
27
+ end
28
+
29
+ def ipv6_config
30
+ base_config + %[
31
+ bind ::1
32
+ format none
33
+ ]
34
+ end
23
35
 
24
36
  def create_driver(conf)
25
37
  Fluent::Test::Driver::Input.new(Fluent::Plugin::TcpInput).configure(conf)
@@ -35,22 +47,23 @@ class TcpInputTest < Test::Unit::TestCase
35
47
 
36
48
 
37
49
  data(
38
- 'ipv4' => [CONFIG, '127.0.0.1', :ipv4],
39
- 'ipv6' => [IPv6_CONFIG, '::1', :ipv6],
50
+ 'ipv4' => ['127.0.0.1', :ipv4],
51
+ 'ipv6' => ['::1', :ipv6],
40
52
  )
41
53
  test 'configure' do |data|
42
- conf, bind, protocol = data
54
+ bind, protocol = data
55
+ conf = send("#{protocol}_config")
43
56
  omit "IPv6 is not supported on this environment" if protocol == :ipv6 && !ipv6_enabled?
44
57
 
45
58
  d = create_driver(conf)
46
- assert_equal PORT, d.instance.port
59
+ assert_equal @port, d.instance.port
47
60
  assert_equal bind, d.instance.bind
48
61
  assert_equal "\n", d.instance.delimiter
49
62
  end
50
63
 
51
64
  test ' configure w/o parse section' do
52
65
  assert_raise(Fluent::ConfigError.new("<parse> section is required.")) {
53
- create_driver(BASE_CONFIG)
66
+ create_driver(base_config)
54
67
  }
55
68
  end
56
69
 
@@ -82,10 +95,10 @@ class TcpInputTest < Test::Unit::TestCase
82
95
  payloads = data['payloads']
83
96
  expecteds = data['expecteds']
84
97
 
85
- d = create_driver(BASE_CONFIG + "format #{format}")
98
+ d = create_driver(base_config + "format #{format}")
86
99
  d.run(expect_records: 2) do
87
100
  payloads.each do |payload|
88
- create_tcp_socket('127.0.0.1', PORT) do |sock|
101
+ create_tcp_socket('127.0.0.1', @port) do |sock|
89
102
  sock.send(payload, 0)
90
103
  end
91
104
  end
@@ -105,9 +118,9 @@ class TcpInputTest < Test::Unit::TestCase
105
118
  payloads = data['payloads']
106
119
  expecteds = data['expecteds']
107
120
 
108
- d = create_driver(BASE_CONFIG + "format #{format}")
121
+ d = create_driver(base_config + "format #{format}")
109
122
  d.run(expect_records: 2) do
110
- create_tcp_socket('127.0.0.1', PORT) do |sock|
123
+ create_tcp_socket('127.0.0.1', @port) do |sock|
111
124
  payloads.each do |payload|
112
125
  sock.send(payload, 0)
113
126
  end
@@ -123,13 +136,13 @@ class TcpInputTest < Test::Unit::TestCase
123
136
  end
124
137
 
125
138
  test 'source_hostname_key' do
126
- d = create_driver(BASE_CONFIG + %!
139
+ d = create_driver(base_config + %!
127
140
  format none
128
141
  source_hostname_key host
129
142
  !)
130
143
  hostname = nil
131
144
  d.run(expect_records: 1) do
132
- create_tcp_socket('127.0.0.1', PORT) do |sock|
145
+ create_tcp_socket('127.0.0.1', @port) do |sock|
133
146
  sock.do_not_reverse_lookup = false
134
147
  hostname = sock.peeraddr[2]
135
148
  sock.send("test\n", 0)
@@ -144,13 +157,13 @@ class TcpInputTest < Test::Unit::TestCase
144
157
  end
145
158
 
146
159
  test 'source_address_key' do
147
- d = create_driver(BASE_CONFIG + %!
160
+ d = create_driver(base_config + %!
148
161
  format none
149
162
  source_address_key addr
150
163
  !)
151
164
  address = nil
152
165
  d.run(expect_records: 1) do
153
- create_tcp_socket('127.0.0.1', PORT) do |sock|
166
+ create_tcp_socket('127.0.0.1', @port) do |sock|
154
167
  address = sock.peeraddr[3]
155
168
  sock.send("test\n", 0)
156
169
  end
@@ -165,7 +178,7 @@ class TcpInputTest < Test::Unit::TestCase
165
178
 
166
179
  sub_test_case '<security>' do
167
180
  test 'accept from allowed client' do
168
- d = create_driver(CONFIG + %!
181
+ d = create_driver(ipv4_config + %!
169
182
  <security>
170
183
  <client>
171
184
  network 127.0.0.1
@@ -173,7 +186,7 @@ class TcpInputTest < Test::Unit::TestCase
173
186
  </security>
174
187
  !)
175
188
  d.run(expect_records: 1) do
176
- create_tcp_socket('127.0.0.1', PORT) do |sock|
189
+ create_tcp_socket('127.0.0.1', @port) do |sock|
177
190
  sock.send("hello\n", 0)
178
191
  end
179
192
  end
@@ -185,7 +198,7 @@ class TcpInputTest < Test::Unit::TestCase
185
198
  end
186
199
 
187
200
  test 'deny from disallowed client' do
188
- d = create_driver(CONFIG + %!
201
+ d = create_driver(ipv4_config + %!
189
202
  <security>
190
203
  <client>
191
204
  network 200.0.0.0
@@ -193,7 +206,7 @@ class TcpInputTest < Test::Unit::TestCase
193
206
  </security>
194
207
  !)
195
208
  d.run(shutdown: false, expect_records: 1, timeout: 2) do
196
- create_tcp_socket('127.0.0.1', PORT) do |sock|
209
+ create_tcp_socket('127.0.0.1', @port) do |sock|
197
210
  sock.send("hello\n", 0)
198
211
  end
199
212
  end
@@ -205,7 +218,7 @@ class TcpInputTest < Test::Unit::TestCase
205
218
 
206
219
  sub_test_case '<extract>' do
207
220
  test 'extract tag from record field' do
208
- d = create_driver(BASE_CONFIG + %!
221
+ d = create_driver(base_config + %!
209
222
  <parse>
210
223
  @type json
211
224
  </parse>
@@ -214,7 +227,7 @@ class TcpInputTest < Test::Unit::TestCase
214
227
  </extract>
215
228
  !)
216
229
  d.run(expect_records: 1) do
217
- create_tcp_socket('127.0.0.1', PORT) do |sock|
230
+ create_tcp_socket('127.0.0.1', @port) do |sock|
218
231
  data = {'msg' => 'hello', 'tag' => 'helper_test'}
219
232
  sock.send("#{data.to_json}\n", 0)
220
233
  end
@@ -5,21 +5,33 @@ require 'fluent/plugin/in_udp'
5
5
  class UdpInputTest < Test::Unit::TestCase
6
6
  def setup
7
7
  Fluent::Test.setup
8
+ @port = unused_port
8
9
  end
9
10
 
10
- PORT = unused_port
11
- BASE_CONFIG = %[
12
- port #{PORT}
13
- tag udp
14
- ]
15
- CONFIG = BASE_CONFIG + %!
16
- bind 127.0.0.1
17
- format /^\\[(?<time>[^\\]]*)\\] (?<message>.*)/
18
- !
19
- IPv6_CONFIG = BASE_CONFIG + %!
20
- bind ::1
21
- format /^\\[(?<time>[^\\]]*)\\] (?<message>.*)/
22
- !
11
+ def teardown
12
+ @port = nil
13
+ end
14
+
15
+ def base_config
16
+ %[
17
+ port #{@port}
18
+ tag udp
19
+ ]
20
+ end
21
+
22
+ def ipv4_config
23
+ base_config + %!
24
+ bind 127.0.0.1
25
+ format /^\\[(?<time>[^\\]]*)\\] (?<message>.*)/
26
+ !
27
+ end
28
+
29
+ def ipv6_config
30
+ base_config + %!
31
+ bind ::1
32
+ format /^\\[(?<time>[^\\]]*)\\] (?<message>.*)/
33
+ !
34
+ end
23
35
 
24
36
  def create_driver(conf)
25
37
  Fluent::Test::Driver::Input.new(Fluent::Plugin::UdpInput).configure(conf)
@@ -45,15 +57,16 @@ class UdpInputTest < Test::Unit::TestCase
45
57
  end
46
58
 
47
59
  data(
48
- 'ipv4' => [CONFIG, '127.0.0.1', :ipv4],
49
- 'ipv6' => [IPv6_CONFIG, '::1', :ipv6],
60
+ 'ipv4' => ['127.0.0.1', :ipv4],
61
+ 'ipv6' => ['::1', :ipv6],
50
62
  )
51
63
  test 'configure' do |data|
52
- conf, bind, protocol = data
64
+ bind, protocol = data
65
+ conf = send("#{protocol}_config")
53
66
  omit "IPv6 is not supported on this environment" if protocol == :ipv6 && !ipv6_enabled?
54
67
 
55
68
  d = create_driver(conf)
56
- assert_equal PORT, d.instance.port
69
+ assert_equal @port, d.instance.port
57
70
  assert_equal bind, d.instance.bind
58
71
  assert_equal 4096, d.instance.message_length_limit
59
72
  assert_equal nil, d.instance.receive_buffer_size
@@ -61,16 +74,17 @@ class UdpInputTest < Test::Unit::TestCase
61
74
 
62
75
  test ' configure w/o parse section' do
63
76
  assert_raise(Fluent::ConfigError.new("<parse> section is required.")) {
64
- create_driver(BASE_CONFIG)
77
+ create_driver(base_config)
65
78
  }
66
79
  end
67
80
 
68
81
  data(
69
- 'ipv4' => [CONFIG, '127.0.0.1', :ipv4],
70
- 'ipv6' => [IPv6_CONFIG, '::1', :ipv6],
82
+ 'ipv4' => ['127.0.0.1', :ipv4],
83
+ 'ipv6' => ['::1', :ipv6],
71
84
  )
72
85
  test 'time_format' do |data|
73
- conf, bind, protocol = data
86
+ bind, protocol = data
87
+ conf = send("#{protocol}_config")
74
88
  omit "IPv6 is not supported on this environment" if protocol == :ipv6 && !ipv6_enabled?
75
89
 
76
90
  d = create_driver(conf)
@@ -81,7 +95,7 @@ class UdpInputTest < Test::Unit::TestCase
81
95
  ]
82
96
 
83
97
  d.run(expect_records: 2) do
84
- create_udp_socket(bind, PORT) do |u|
98
+ create_udp_socket(bind, @port) do |u|
85
99
  tests.each do |test|
86
100
  u.send(test['msg'], 0)
87
101
  end
@@ -100,7 +114,7 @@ class UdpInputTest < Test::Unit::TestCase
100
114
  )
101
115
  test 'message_length_limit/body_size_limit compatibility' do |param|
102
116
 
103
- d = create_driver(CONFIG + param)
117
+ d = create_driver(ipv4_config + param)
104
118
  assert_equal 2048, d.instance.message_length_limit
105
119
  end
106
120
 
@@ -141,9 +155,9 @@ class UdpInputTest < Test::Unit::TestCase
141
155
  payloads = data['payloads']
142
156
  expecteds = data['expecteds']
143
157
 
144
- d = create_driver(BASE_CONFIG + "format #{format}")
158
+ d = create_driver(base_config + "format #{format}")
145
159
  d.run(expect_records: 2) do
146
- create_udp_socket('127.0.0.1', PORT) do |u|
160
+ create_udp_socket('127.0.0.1', @port) do |u|
147
161
  payloads.each do |payload|
148
162
  u.send(payload, 0)
149
163
  end
@@ -159,13 +173,13 @@ class UdpInputTest < Test::Unit::TestCase
159
173
  end
160
174
 
161
175
  test 'remove_newline' do
162
- d = create_driver(BASE_CONFIG + %!
176
+ d = create_driver(base_config + %!
163
177
  format none
164
178
  remove_newline false
165
179
  !)
166
180
  payloads = ["test1\n", "test2\n"]
167
181
  d.run(expect_records: 2) do
168
- create_udp_socket('127.0.0.1', PORT) do |u|
182
+ create_udp_socket('127.0.0.1', @port) do |u|
169
183
  payloads.each do |payload|
170
184
  u.send(payload, 0)
171
185
  end
@@ -182,13 +196,13 @@ class UdpInputTest < Test::Unit::TestCase
182
196
  end
183
197
 
184
198
  test 'source_hostname_key' do
185
- d = create_driver(BASE_CONFIG + %!
199
+ d = create_driver(base_config + %!
186
200
  format none
187
201
  source_hostname_key host
188
202
  !)
189
203
  hostname = nil
190
204
  d.run(expect_records: 1) do
191
- create_udp_socket('127.0.0.1', PORT) do |u|
205
+ create_udp_socket('127.0.0.1', @port) do |u|
192
206
  u.send("test", 0)
193
207
  hostname = u.peeraddr[2]
194
208
  end
@@ -201,13 +215,13 @@ class UdpInputTest < Test::Unit::TestCase
201
215
  end
202
216
 
203
217
  test 'source_address_key' do
204
- d = create_driver(BASE_CONFIG + %!
218
+ d = create_driver(base_config + %!
205
219
  format none
206
220
  source_address_key addr
207
221
  !)
208
222
  address = nil
209
223
  d.run(expect_records: 1) do
210
- create_udp_socket('127.0.0.1', PORT) do |u|
224
+ create_udp_socket('127.0.0.1', @port) do |u|
211
225
  u.send("test", 0)
212
226
  address = u.peeraddr[3]
213
227
  end
@@ -223,7 +237,7 @@ class UdpInputTest < Test::Unit::TestCase
223
237
  # doesn't check exact value because it depends on platform and condition
224
238
 
225
239
  # check if default socket and in_udp's one without receive_buffer_size have same size buffer
226
- d0 = create_driver(BASE_CONFIG + %!
240
+ d0 = create_driver(base_config + %!
227
241
  format none
228
242
  !)
229
243
  d0.run do
@@ -237,7 +251,7 @@ class UdpInputTest < Test::Unit::TestCase
237
251
  end
238
252
 
239
253
  # check if default socket and in_udp's one with receive_buffer_size have different size buffer
240
- d1 = create_driver(BASE_CONFIG + %!
254
+ d1 = create_driver(base_config + %!
241
255
  format none
242
256
  receive_buffer_size 1001
243
257
  !)
@@ -85,6 +85,17 @@ class InputTest < Test::Unit::TestCase
85
85
  end
86
86
  end
87
87
 
88
+ test 'can use metrics plugins and fallback methods' do
89
+ @p.configure(config_element('ROOT', '', {'@log_level' => 'debug'}))
90
+
91
+ %w[emit_size_metrics emit_records_metrics].each do |metric_name|
92
+ assert_true @p.instance_variable_get(:"@#{metric_name}").is_a?(Fluent::Plugin::Metrics)
93
+ end
94
+
95
+ assert_equal 0, @p.emit_size
96
+ assert_equal 0, @p.emit_records
97
+ end
98
+
88
99
  test 'are not available with multi workers configuration in default' do
89
100
  assert_false @p.multi_workers_ready?
90
101
  end
@@ -0,0 +1,294 @@
1
+ require_relative '../helper'
2
+ require 'fluent/plugin/metrics'
3
+ require 'fluent/plugin/base'
4
+ require 'fluent/system_config'
5
+
6
+ class BareMetrics < Fluent::Plugin::Metrics
7
+ Fluent::Plugin.register_metrics('bare', self)
8
+
9
+ private
10
+
11
+ # Just override for tests.
12
+ def has_methods_for_counter?
13
+ false
14
+ end
15
+ end
16
+
17
+ class BasicCounterMetrics < Fluent::Plugin::Metrics
18
+ Fluent::Plugin.register_metrics('example', self)
19
+
20
+ attr_reader :data
21
+
22
+ def initialize
23
+ super
24
+ @data = 0
25
+ end
26
+ def get
27
+ @data
28
+ end
29
+ def inc
30
+ @data +=1
31
+ end
32
+ def add(value)
33
+ @data += value
34
+ end
35
+ def set(value)
36
+ @data = value
37
+ end
38
+ def close
39
+ @data = 0
40
+ super
41
+ end
42
+ end
43
+
44
+ class AliasedCounterMetrics < Fluent::Plugin::Metrics
45
+ Fluent::Plugin.register_metrics('example', self)
46
+
47
+ attr_reader :data
48
+
49
+ def initialize
50
+ super
51
+ @data = 0
52
+ end
53
+ def configure(conf)
54
+ super
55
+ class << self
56
+ alias_method :set, :set_counter
57
+ end
58
+ end
59
+ def get
60
+ @data
61
+ end
62
+ def inc
63
+ @data +=1
64
+ end
65
+ def add(value)
66
+ @data += value
67
+ end
68
+ def set_counter(value)
69
+ @data = value
70
+ end
71
+ def close
72
+ @data = 0
73
+ super
74
+ end
75
+ end
76
+
77
+ class BasicGaugeMetrics < Fluent::Plugin::Metrics
78
+ Fluent::Plugin.register_metrics('example', self)
79
+
80
+ attr_reader :data
81
+
82
+ def initialize
83
+ super
84
+ @data = 0
85
+ end
86
+ def get
87
+ @data
88
+ end
89
+ def inc
90
+ @data +=1
91
+ end
92
+ def dec
93
+ @data -=1
94
+ end
95
+ def add(value)
96
+ @data += value
97
+ end
98
+ def sub(value)
99
+ @data -= value
100
+ end
101
+ def set(value)
102
+ @data = value
103
+ end
104
+ def close
105
+ @data = 0
106
+ super
107
+ end
108
+ end
109
+
110
+ class AliasedGaugeMetrics < Fluent::Plugin::Metrics
111
+ Fluent::Plugin.register_metrics('example', self)
112
+
113
+ attr_reader :data
114
+
115
+ def initialize
116
+ super
117
+ @data = 0
118
+ end
119
+ def configure(conf)
120
+ super
121
+ class << self
122
+ alias_method :dec, :dec_gauge
123
+ alias_method :set, :set_gauge
124
+ alias_method :sub, :sub_gauge
125
+ end
126
+ end
127
+ def get
128
+ @data
129
+ end
130
+ def inc
131
+ @data +=1
132
+ end
133
+ def dec_gauge
134
+ @data -=1
135
+ end
136
+ def add(value)
137
+ @data += value
138
+ end
139
+ def sub_gauge(value)
140
+ @data -= value
141
+ end
142
+ def set_gauge(value)
143
+ @data = value
144
+ end
145
+ def close
146
+ @data = 0
147
+ super
148
+ end
149
+ end
150
+
151
+ class StorageTest < Test::Unit::TestCase
152
+ sub_test_case 'BareMetrics' do
153
+ setup do
154
+ @m = BareMetrics.new
155
+ @m.configure(config_element())
156
+ end
157
+
158
+ test 'is configured with plugin information and system config' do
159
+ m = BareMetrics.new
160
+ m.configure(config_element('metrics', '', {}))
161
+
162
+ assert_false m.use_gauge_metric
163
+ assert_false m.has_methods_for_counter
164
+ assert_false m.has_methods_for_gauge
165
+ end
166
+
167
+ test 'all bare operations are not defined yet' do
168
+ assert_raise NotImplementedError do
169
+ @m.get
170
+ end
171
+ assert_raise NotImplementedError do
172
+ @m.inc
173
+ end
174
+ assert_raise NotImplementedError do
175
+ @m.dec
176
+ end
177
+ assert_raise NotImplementedError do
178
+ @m.add(10)
179
+ end
180
+ assert_raise NotImplementedError do
181
+ @m.sub(11)
182
+ end
183
+ assert_raise NotImplementedError do
184
+ @m.set(123)
185
+ end
186
+ end
187
+ end
188
+
189
+ sub_test_case 'BasicCounterMetric' do
190
+ setup do
191
+ @m = BasicCounterMetrics.new
192
+ @m.configure(config_element('metrics', '', {'@id' => '1'}))
193
+ end
194
+
195
+ test 'all basic counter operations work well' do
196
+ assert_true @m.has_methods_for_counter
197
+ assert_false @m.has_methods_for_gauge
198
+
199
+ assert_equal 0, @m.get
200
+ assert_equal 1, @m.inc
201
+
202
+ @m.add(20)
203
+ assert_equal 21, @m.get
204
+ assert_raise NotImplementedError do
205
+ @m.dec
206
+ end
207
+
208
+ @m.set(100)
209
+ assert_equal 100, @m.get
210
+ assert_raise NotImplementedError do
211
+ @m.sub(11)
212
+ end
213
+ end
214
+ end
215
+
216
+ sub_test_case 'AliasedCounterMetric' do
217
+ setup do
218
+ @m = AliasedCounterMetrics.new
219
+ @m.configure(config_element('metrics', '', {}))
220
+ end
221
+
222
+ test 'all aliased counter operations work well' do
223
+ assert_true @m.has_methods_for_counter
224
+ assert_false @m.has_methods_for_gauge
225
+
226
+ assert_equal 0, @m.get
227
+ assert_equal 1, @m.inc
228
+
229
+ @m.add(20)
230
+ assert_equal 21, @m.get
231
+ assert_raise NotImplementedError do
232
+ @m.dec
233
+ end
234
+
235
+ @m.set(100)
236
+ assert_equal 100, @m.get
237
+ assert_raise NotImplementedError do
238
+ @m.sub(11)
239
+ end
240
+ end
241
+ end
242
+
243
+ sub_test_case 'BasicGaugeMetric' do
244
+ setup do
245
+ @m = BasicGaugeMetrics.new
246
+ @m.use_gauge_metric = true
247
+ @m.configure(config_element('metrics', '', {}))
248
+ end
249
+
250
+ test 'all basic gauge operations work well' do
251
+ assert_false @m.has_methods_for_counter
252
+ assert_true @m.has_methods_for_gauge
253
+
254
+ assert_equal 0, @m.get
255
+ assert_equal 1, @m.inc
256
+
257
+ @m.add(20)
258
+ assert_equal 21, @m.get
259
+ @m.dec
260
+ assert_equal 20, @m.get
261
+
262
+ @m.set(100)
263
+ assert_equal 100, @m.get
264
+ @m.sub(11)
265
+ assert_equal 89, @m.get
266
+ end
267
+ end
268
+
269
+ sub_test_case 'AliasedGaugeMetric' do
270
+ setup do
271
+ @m = AliasedGaugeMetrics.new
272
+ @m.use_gauge_metric = true
273
+ @m.configure(config_element('metrics', '', {}))
274
+ end
275
+
276
+ test 'all aliased gauge operations work well' do
277
+ assert_false @m.has_methods_for_counter
278
+ assert_true @m.has_methods_for_gauge
279
+
280
+ assert_equal 0, @m.get
281
+ assert_equal 1, @m.inc
282
+
283
+ @m.add(20)
284
+ assert_equal 21, @m.get
285
+ @m.dec
286
+ assert_equal 20, @m.get
287
+
288
+ @m.set(100)
289
+ assert_equal 100, @m.get
290
+ @m.sub(11)
291
+ assert_equal 89, @m.get
292
+ end
293
+ end
294
+ end