-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathinfluxdb_spec.rb
More file actions
347 lines (268 loc) · 11.9 KB
/
Copy pathinfluxdb_spec.rb
File metadata and controls
347 lines (268 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# encoding: utf-8
require "logstash/devutils/rspec/spec_helper"
require "logstash/outputs/influxdb"
describe LogStash::Outputs::InfluxDB do
subject { LogStash::Outputs::InfluxDB.new(config) }
context "validate minimal default config" do
let(:config) do
{
"host" => "testhost",
"use_event_fields_for_data_points" => true
}
end
before do
subject.register
subject.close
end
it "sets correct influx client settings" do
config = subject.instance_variable_get(:@influxdbClient).config
expect(config.next_host).to eq "testhost"
expect(config.instance_variable_get(:@port)).to eq 8086
expect(config.instance_variable_get(:@time_precision)).to eq "ms"
expect(config.instance_variable_get(:@auth_method)).to eq "none".freeze
expect(config.instance_variable_get(:@initial_delay)).to eq 1
expect(config.instance_variable_get(:@retry)).to eq 3
expect(config.instance_variable_get(:@use_ssl)).to eq false
expect(config.instance_variable_get(:@username)).to eq nil
expect(config.instance_variable_get(:@password)).to eq nil
end
end
context "validate non default config" do
let(:config) do
{
"host" => "localhost",
"use_event_fields_for_data_points" => true,
"port" => 9999,
"ssl" => true,
"user" => "my_user",
"password" => "my_pass",
"initial_delay" => 5,
"max_retries" => 8,
"time_precision" => "s"
}
end
before do
subject.register
subject.close
end
it "sets correct influx client settings" do
config = subject.instance_variable_get(:@influxdbClient).config
expect(config.instance_variable_get(:@port)).to eq 9999
expect(config.instance_variable_get(:@time_precision)).to eq "s"
expect(config.instance_variable_get(:@initial_delay)).to eq 5
expect(config.instance_variable_get(:@retry)).to eq 8
expect(config.instance_variable_get(:@use_ssl)).to eq true
expect(config.instance_variable_get(:@username)).to eq "my_user"
expect(config.instance_variable_get(:@password)).to eq "my_pass"
expect(config.instance_variable_get(:@auth_method)).to eq "params".freeze
end
end
context "complete pipeline run with 2 events" do
let(:config) do
{
"host" => "localhost",
"user" => "someuser",
"password" => "somepwd",
"allow_time_override" => true,
"data_points" => {
"foo" => "%{foo}",
"bar" => "%{bar}",
"time" => "%{time}"
}
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite).with(result, "statistics")
2.times do
subject.receive(LogStash::Event.new("foo" => "1", "bar" => "2", "time" => "3", "type" => "generator"))
end
# Close / flush the buffer
subject.close
end
#let(:result) { "logstash foo=\"1\",bar=\"2\" 3\nlogstash foo=\"1\",bar=\"2\" 3" }
let(:result) {[{:series=>"logstash", :timestamp=>"3", :values=>{"foo"=>"1", "bar"=>"2"}}, {:series=>"logstash", :timestamp=>"3", :values=>{"foo"=>"1", "bar"=>"2"}}]}
it "should receive 2 events, flush and call post with 2 items json array" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
context "using event fields as data points" do
let(:config) do
{
"host" => "localhost",
"measurement" => "my_series",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite).with(result, "statistics")
subject.receive(LogStash::Event.new("foo" => "1", "bar" => "2", "time" => "3", "type" => "generator"))
# Close / flush the buffer
subject.close
end
let(:result) {[{:series=>"my_series", :timestamp=>"3", :values=>{"foo"=>"1", "bar"=>"2"}}]}
it "should use the event fields as the data points, excluding @version and @timestamp by default as well as any fields configured by exclude_fields" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
context "sending some fields as Influxdb tags" do
let(:config) do
{
"host" => "localhost",
"measurement" => "my_series",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true,
"send_as_tags" => ["bar", "baz", "qux"]
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite).with(result, "statistics")
subject.receive(LogStash::Event.new("foo" => "1", "bar" => "2", "baz" => "3", "time" => "4", "type" => "generator"))
# Close / flush the buffer
subject.close
end
let(:result) {[{:series=>"my_series", :timestamp=>"4", :tags=>{"bar"=>"2", "baz"=>"3"}, :values=>{"foo"=>"1"}}]}
it "should use the event fields as the data points, excluding @version and @timestamp by default as well as any fields configured by exclude_fields" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
context "when fields data contains a list of tags" do
let(:config) do
{
"host" => "localhost",
"measurement" => "my_series",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true,
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite).with(result, "statistics")
subject.receive(event)
# Close / flush the buffer
subject.close
end
let(:event) {LogStash::Event.new("foo" => "1", "time" => "2", "tags" => ["tagged"], "type" => "generator")}
let(:result) {[{:series=>"my_series", :timestamp=>"2", :tags=>{"tagged"=>"true"}, :values=>{"foo"=>"1"}}]}
it "should use the event fields as the data points, excluding @version and @timestamp by default as well as any fields configured by exclude_fields" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
context "read data-points/tags from speicifc event fields" do
let(:config) do
{
"host" => "localhost",
"measurement" => "my_series",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true,
"use_hash_field_for_data_points" => "[pts]",
"send_array_field_as_tags" => "[tags]",
}
end
before do
subject.register
allow(subject).to receive(:dowrite).with(result, "statistics")
subject.receive(event)
subject.close
end
let(:event) {LogStash::Event.new("pts" => {"foo" => "bar","fee" =>"buzz","time" => 22}, "tags" => ["foo"], "type" => "generator")}
let(:result) {[{:series=>"my_series", :timestamp=>22, :tags=>{"foo"=>"bar"}, :values=>{"fee"=>"buzz"}}]}
it "should use the hash field entries as the data points and array field as tags" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
context "when fields are coerced to numerics" do
let(:config) do
{
"host" => "localhost",
"measurement" => "my_series",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true,
"coerce_values" => { "foo" => "integer", "bar" => "float" }
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite).with(result, "statistics")
subject.receive(LogStash::Event.new("foo" => "1", "bar" => "2.0", "baz"=>"\\\"quotes\\\"", "time"=>3, "type" => "generator"))
# Close / flush the buffer
subject.close
end
let(:result) {[{:series=>"my_series", :timestamp=>3, :values=>{"foo"=>1, "bar"=>2.0, "baz"=>"\\\"quotes\\\"" }}]}
it "should use the event fields as the data points, excluding @version and @timestamp by default as well as any fields configured by exclude_fields" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
# Test issue #31 - Run "db" parameter through event.sprintf() to support...
# -------------------------------------------------------------------------
# This test is intended to verify writes to multiple measurements in A SINGLE
# DATABASE continue to work *after* implementing #31. Also verifies that
# sprintf formatting is supported in the measurement name.
context "receiving 3 points between 2 measurements in 1 database" do
let(:config) do
{
"host" => "localhost",
"measurement" => "%{baz}",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite)
subject.receive(LogStash::Event.new("foo"=>"1", "bar"=>"2", "baz" => "m1", "time" => "1", "type" => "generator"))
subject.receive(LogStash::Event.new("foo"=>"3", "bar"=>"4", "baz" => "m2", "time" => "2", "type" => "generator"))
subject.receive(LogStash::Event.new("foo"=>"5", "bar"=>"6", "baz" => "m2", "time" => "3", "type" => "generator"))
# Close / flush the buffer
subject.close
end
let(:result) {[{:series=>"m1", :timestamp=>"1", :values=>{"foo"=>"1", "bar"=>"2", "baz" => "m1" }},{:series=>"m2", :timestamp=>"2", :values=>{"foo"=>"3", "bar"=>"4", "baz" => "m2"}},{:series=>"m2", :timestamp=>"3", :values=>{"foo"=>"5", "bar"=>"6", "baz" => "m2" }}]}
it "should use the event fields as the data points, excluding @version and @timestamp by default as well as any fields configured by exclude_fields" do
expect(subject).to have_received(:dowrite).with(result, "statistics")
end
end
# Test issue #31 - Run "db" parameter through event.sprintf() to support...
# -------------------------------------------------------------------------
# This test is intended to verify writes to multiple measurements in MULTIPLE
# DATABASES result in separate bulk POSTs (one for each database in the
# buffer), and the correct measurements being written to the correct db.
# Also verifies that sprintf formatting is correctly supported in the
# database name.
context "receiving 4 points between 2 measurements in 2 databases" do
let(:config) do
{
"host" => "localhost",
"db" => "%{bar}",
"measurement" => "%{baz}",
"allow_time_override" => true,
"use_event_fields_for_data_points" => true,
}
end
before do
subject.register
# Added db name parameter to post - M.Laws
allow(subject).to receive(:dowrite)
subject.receive(LogStash::Event.new("foo"=>"1", "bar"=>"db1", "baz" => "m1", "time" => "1", "type" => "generator"))
subject.receive(LogStash::Event.new("foo"=>"2", "bar"=>"db1", "baz" => "m1", "time" => "2", "type" => "generator"))
subject.receive(LogStash::Event.new("foo"=>"3", "bar"=>"db2", "baz" => "m2", "time" => "3", "type" => "generator"))
subject.receive(LogStash::Event.new("foo"=>"4", "bar"=>"db2", "baz" => "m2", "time" => "4", "type" => "generator"))
# Close / flush the buffer
subject.close
end
let(:resultdb1) {[{:series=>"m1", :timestamp=>"1", :values=>{"foo"=>"1", "bar"=>"db1", "baz" => "m1" }},{:series=>"m1", :timestamp=>"2", :values=>{"foo"=>"2", "bar"=>"db1", "baz" => "m1" }}]}
let(:resultdb2) {[{:series=>"m2", :timestamp=>"3", :values=>{"foo"=>"3", "bar"=>"db2", "baz" => "m2" }},{:series=>"m2", :timestamp=>"4", :values=>{"foo"=>"4", "bar"=>"db2", "baz" => "m2" }}]}
it "should use the event fields as the data points, excluding @version and @timestamp by default as well as any fields configured by exclude_fields" do
expect(subject).to have_received(:dowrite).with(resultdb1, "db1").once
expect(subject).to have_received(:dowrite).with(resultdb2, "db2").once
end
end
end