logstash-output-scalyr 0.2.7.beta → 0.2.9.beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,1262 +0,0 @@
1
- # encoding: utf-8
2
- require "logstash/devutils/rspec/spec_helper"
3
- require "logstash/outputs/scalyr"
4
- require "logstash/codecs/plain"
5
- require "logstash/event"
6
- require "json"
7
- require "quantile"
8
-
9
- # Require the specific version of `json` used in logstash
10
- gem 'json', '1.8.6'
11
- require 'json'
12
-
13
- NODE_HOSTNAME = Socket.gethostname
14
-
15
- class MockClientSession
16
- DEFAULT_STATS = {
17
- :total_requests_sent => 20,
18
- :total_requests_failed => 10,
19
- :total_request_bytes_sent => 100,
20
- :total_compressed_request_bytes_sent => 50,
21
- :total_response_bytes_received => 100,
22
- :total_request_latency_secs => 100,
23
- :total_serialization_duration_secs => 100.5,
24
- :total_compression_duration_secs => 10.20,
25
- :compression_type => "deflate",
26
- :compression_level => 9,
27
- }
28
-
29
- def initialize(stats = DEFAULT_STATS)
30
- @stats = stats
31
- @sent_events = []
32
- end
33
-
34
- def get_stats
35
- @stats.clone
36
- end
37
-
38
- def post_add_events(body, is_status, body_serialization_duration = 0)
39
- @sent_events << { :body => body, :body_serialization_duration => body_serialization_duration }
40
- end
41
- end
42
-
43
-
44
- describe LogStash::Outputs::Scalyr do
45
- let(:sample_events) {
46
- events = []
47
- for i in 1..3 do
48
- e = LogStash::Event.new
49
- e.set('source_host', "my host #{i}")
50
- e.set('source_file', "my file #{i}")
51
- e.set('seq', i)
52
- e.set('nested', {'a'=>1, 'b'=>[3,4,5]})
53
- e.set('tags', ['t1', 't2', 't3'])
54
- events.push(e)
55
- end
56
- events
57
- }
58
-
59
- let(:sample_events_with_severity) {
60
- events = []
61
- for i in 0..6 do
62
- # valid severity - integer
63
- e = LogStash::Event.new
64
- e.set('source_host', "my host #{i}")
65
- e.set('source_file', "my file #{i}")
66
- e.set('severity', i)
67
- e.set('seq', i)
68
- e.set('nested', {'a'=>1, 'b'=>[3,4,5]})
69
- e.set('tags', ['t1', 't2', 't3'])
70
- events.push(e)
71
- end
72
- for i in 0..6 do
73
- # valid severity - string
74
- e = LogStash::Event.new
75
- e.set('source_host', "my host #{i}")
76
- e.set('source_file', "my file #{i}")
77
- e.set('severity', i.to_s)
78
- e.set('seq', i)
79
- e.set('nested', {'a'=>1, 'b'=>[3,4,5]})
80
- e.set('tags', ['t1', 't2', 't3'])
81
- events.push(e)
82
- end
83
-
84
- # invalid severity values
85
- e = LogStash::Event.new
86
- e.set('source_host', "my host a")
87
- e.set('severity', -1)
88
- events.push(e)
89
-
90
- e = LogStash::Event.new
91
- e.set('source_host', "my host a")
92
- e.set('severity', 7)
93
- events.push(e)
94
-
95
- e = LogStash::Event.new
96
- e.set('source_host', "my host a")
97
- e.set('severity', "invalid")
98
- events.push(e)
99
-
100
- events
101
- }
102
-
103
- let(:sample_events_with_level) {
104
- events = []
105
- for i in 0..6 do
106
- e = LogStash::Event.new
107
- e.set('source_host', "my host #{i}")
108
- e.set('source_file', "my file #{i}")
109
- e.set('level', i)
110
- e.set('seq', i)
111
- e.set('nested', {'a'=>1, 'b'=>[3,4,5]})
112
- e.set('tags', ['t1', 't2', 't3'])
113
- events.push(e)
114
- end
115
- events
116
- }
117
-
118
- describe "#build_multi_event_request_array" do
119
-
120
- context "test get_stats and send_status" do
121
- plugin = LogStash::Outputs::Scalyr.new({
122
- 'api_write_token' => '1234',
123
- 'perform_connectivity_check' => false,
124
- 'serverhost_field' => 'source_host',
125
- 'log_constants' => ['tags'],
126
- 'flatten_nested_values' => true,
127
- })
128
- plugin.register
129
-
130
- mock_client_session = MockClientSession.new
131
-
132
- it "returns correct stats on get_stats" do
133
- stats = mock_client_session.get_stats
134
- expect(stats[:total_requests_sent]).to eq(20)
135
- end
136
-
137
- it "it doesnt include flatten metrics if flattening is disabled" do
138
- plugin1 = LogStash::Outputs::Scalyr.new({
139
- 'api_write_token' => '1234',
140
- 'perform_connectivity_check' => false,
141
- 'serverhost_field' => 'source_host',
142
- 'log_constants' => ['tags'],
143
- 'flatten_nested_values' => false,
144
- })
145
- plugin1.register()
146
- mock_client_session = MockClientSession.new
147
- plugin1.instance_variable_set(:@last_status_transmit_time, 100)
148
- plugin1.instance_variable_set(:@client_session, mock_client_session)
149
- plugin1.instance_variable_set(:@session_id, "some_session_id")
150
- plugin1.instance_variable_set(:@plugin_metrics, {
151
- :build_multi_duration_secs => Quantile::Estimator.new,
152
- :multi_receive_duration_secs => Quantile::Estimator.new,
153
- :multi_receive_event_count => Quantile::Estimator.new,
154
- :event_attributes_count => Quantile::Estimator.new,
155
- :flatten_values_duration_secs => Quantile::Estimator.new,
156
- :batches_per_multi_receive => Quantile::Estimator.new
157
- })
158
- plugin1.instance_variable_get(:@plugin_metrics)[:multi_receive_duration_secs].observe(1)
159
- plugin1.instance_variable_get(:@plugin_metrics)[:build_multi_duration_secs].observe(1)
160
- plugin1.instance_variable_set(:@multi_receive_statistics, {:total_multi_receive_secs => 0})
161
-
162
- status_event = plugin1.send_status
163
- expect(status_event[:attrs]["message"]).to eq("plugin_status: total_requests_sent=20 total_requests_failed=10 total_request_bytes_sent=100 total_compressed_request_bytes_sent=50 total_response_bytes_received=100 total_request_latency_secs=100 total_serialization_duration_secs=100.5000 total_compression_duration_secs=10.2000 compression_type=deflate compression_level=9 total_multi_receive_secs=0 build_multi_duration_secs_p50=1 build_multi_duration_secs_p90=1 build_multi_duration_secs_p99=1 multi_receive_duration_p50=1 multi_receive_duration_p90=1 multi_receive_duration_p99=1 multi_receive_event_count_p50=0 multi_receive_event_count_p90=0 multi_receive_event_count_p99=0 event_attributes_count_p50=0 event_attributes_count_p90=0 event_attributes_count_p99=0 batches_per_multi_receive_p50=0 batches_per_multi_receive_p90=0 batches_per_multi_receive_p99=0")
164
- end
165
-
166
- it "returns and sends correct status event on send_stats on initial and subsequent send" do
167
- # 1. Initial send
168
- plugin.instance_variable_set(:@last_status_transmit_time, nil)
169
- plugin.instance_variable_set(:@client_session, mock_client_session)
170
- plugin.instance_variable_set(:@session_id, "some_session_id")
171
- status_event = plugin.send_status
172
- expect(status_event[:attrs]["message"]).to eq("Started Scalyr LogStash output plugin %s (compression_type=deflate,compression_level=deflate,json_library=stdlib)." % [PLUGIN_VERSION])
173
-
174
- # 2. Second send
175
- plugin.instance_variable_set(:@last_status_transmit_time, 100)
176
- plugin.instance_variable_set(:@client_session, mock_client_session)
177
- # Setup one quantile calculation to make sure at least one of them calculates as expected
178
- plugin.instance_variable_set(:@plugin_metrics, {
179
- :build_multi_duration_secs => Quantile::Estimator.new,
180
- :multi_receive_duration_secs => Quantile::Estimator.new,
181
- :multi_receive_event_count => Quantile::Estimator.new,
182
- :event_attributes_count => Quantile::Estimator.new,
183
- :flatten_values_duration_secs => Quantile::Estimator.new,
184
- :batches_per_multi_receive => Quantile::Estimator.new
185
- })
186
-
187
- (1..20).each do |n|
188
- plugin.instance_variable_get(:@plugin_metrics)[:multi_receive_duration_secs].observe(n)
189
- end
190
-
191
- plugin.instance_variable_set(:@multi_receive_statistics, {:total_multi_receive_secs => 0})
192
- status_event = plugin.send_status
193
- expect(status_event[:attrs]["message"]).to eq("plugin_status: total_requests_sent=20 total_requests_failed=10 total_request_bytes_sent=100 total_compressed_request_bytes_sent=50 total_response_bytes_received=100 total_request_latency_secs=100 total_serialization_duration_secs=100.5000 total_compression_duration_secs=10.2000 compression_type=deflate compression_level=9 total_multi_receive_secs=0 build_multi_duration_secs_p50=0 build_multi_duration_secs_p90=0 build_multi_duration_secs_p99=0 multi_receive_duration_p50=10 multi_receive_duration_p90=18 multi_receive_duration_p99=19 multi_receive_event_count_p50=0 multi_receive_event_count_p90=0 multi_receive_event_count_p99=0 event_attributes_count_p50=0 event_attributes_count_p90=0 event_attributes_count_p99=0 batches_per_multi_receive_p50=0 batches_per_multi_receive_p90=0 batches_per_multi_receive_p99=0 flatten_values_duration_secs_p50=0 flatten_values_duration_secs_p90=0 flatten_values_duration_secs_p99=0")
194
- end
195
-
196
- it "send_stats is called when events list is empty, but otherwise is noop" do
197
- quantile_estimator = Quantile::Estimator.new
198
- plugin.instance_variable_set(:@plugin_metrics, {
199
- :build_multi_duration_secs => Quantile::Estimator.new,
200
- :multi_receive_duration_secs => Quantile::Estimator.new,
201
- :multi_receive_event_count => Quantile::Estimator.new,
202
- :event_attributes_count => Quantile::Estimator.new,
203
- :flatten_values_duration_secs => Quantile::Estimator.new
204
- })
205
- plugin.instance_variable_set(:@client_session, mock_client_session)
206
- expect(plugin).to receive(:send_status)
207
- expect(quantile_estimator).not_to receive(:observe)
208
- expect(mock_client_session).not_to receive(:post_add_events)
209
- plugin.multi_receive([])
210
- end
211
-
212
- it "send_stats is not called when events list is empty and report_status_for_empty_batches is false" do
213
- plugin2 = LogStash::Outputs::Scalyr.new({
214
- 'api_write_token' => '1234',
215
- 'perform_connectivity_check' => false,
216
- 'serverhost_field' => 'source_host',
217
- 'log_constants' => ['tags'],
218
- 'flatten_nested_values' => false,
219
- 'report_status_for_empty_batches' => false,
220
- })
221
-
222
- mock_client_session = MockClientSession.new
223
- quantile_estimator = Quantile::Estimator.new
224
- plugin2.instance_variable_set(:@plugin_metrics, {
225
- :build_multi_duration_secs => Quantile::Estimator.new,
226
- :multi_receive_duration_secs => Quantile::Estimator.new,
227
- :multi_receive_event_count => Quantile::Estimator.new,
228
- :event_attributes_count => Quantile::Estimator.new,
229
- :flatten_values_duration_secs => Quantile::Estimator.new
230
- })
231
- plugin2.instance_variable_set(:@client_session, mock_client_session)
232
- expect(plugin2).not_to receive(:send_status)
233
- expect(quantile_estimator).not_to receive(:observe)
234
- expect(mock_client_session).not_to receive(:post_add_events)
235
- plugin2.multi_receive([])
236
- end
237
-
238
- # Kind of a weak test but I don't see a decent way to write a stronger one without a live client session
239
- it "send_status only sends posts with is_status = true" do
240
- # 1. Initial send
241
- plugin.instance_variable_set(:@last_status_transmit_time, nil)
242
- plugin.instance_variable_set(:@client_session, mock_client_session)
243
- plugin.instance_variable_set(:@session_id, "some_session_id")
244
- expect(mock_client_session).to receive(:post_add_events).with(anything, true, anything)
245
- plugin.send_status
246
-
247
- # 2. Second send
248
- plugin.instance_variable_set(:@last_status_transmit_time, 100)
249
- plugin.instance_variable_set(:@client_session, mock_client_session)
250
- plugin.instance_variable_set(:@plugin_metrics, {
251
- :build_multi_duration_secs => Quantile::Estimator.new,
252
- :multi_receive_duration_secs => Quantile::Estimator.new,
253
- :multi_receive_event_count => Quantile::Estimator.new,
254
- :event_attributes_count => Quantile::Estimator.new,
255
- :flatten_values_duration_secs => Quantile::Estimator.new,
256
- :batches_per_multi_receive => Quantile::Estimator.new
257
- })
258
- (1..20).each do |n|
259
- plugin.instance_variable_get(:@plugin_metrics)[:multi_receive_duration_secs].observe(n)
260
- end
261
- plugin.instance_variable_set(:@multi_receive_statistics, {:total_multi_receive_secs => 0})
262
- expect(mock_client_session).to receive(:post_add_events).with(anything, true, anything)
263
- plugin.send_status
264
- end
265
- end
266
-
267
- context "when a field is configured as a log attribute" do
268
- it "creates logfile from serverHost" do
269
- plugin = LogStash::Outputs::Scalyr.new({
270
- 'api_write_token' => '1234',
271
- 'perform_connectivity_check' => false,
272
- 'serverhost_field' => 'source_host',
273
- 'log_constants' => ['tags'],
274
- })
275
- allow(plugin).to receive(:send_status).and_return(nil)
276
- plugin.register
277
- result = plugin.build_multi_event_request_array(sample_events)
278
- body = JSON.parse(result[0][:body])
279
- expect(body['events'].size).to eq(3)
280
- logattrs2 = body['logs'][2]['attrs']
281
- expect(logattrs2.fetch(EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME, nil)).to eq('my host 3')
282
- expect(logattrs2.fetch('logfile', nil)).to eq('/logstash/my host 3')
283
- expect(logattrs2.fetch('tags', nil)).to eq(['t1', 't2', 't3'])
284
- end
285
- end
286
-
287
- context "when severity field is configured" do
288
- it "works correctly when severity event attribute is specified" do
289
- plugin = LogStash::Outputs::Scalyr.new({
290
- 'api_write_token' => '1234',
291
- 'perform_connectivity_check' => false,
292
- 'severity_field' => 'severity',
293
- })
294
- allow(plugin).to receive(:send_status).and_return(nil)
295
- plugin.register
296
- result = plugin.build_multi_event_request_array(sample_events_with_severity)
297
- body = JSON.parse(result[0][:body])
298
- expect(body['events'].size).to eq(7 + 7 + 3)
299
-
300
- (0..6).each do |index|
301
- expect(body['events'][index]['attrs'].fetch('severity', nil)).to eq(nil)
302
- expect(body['events'][index]['attrs'].fetch('sev', nil)).to eq(nil)
303
- expect(body['events'][index]['sev']).to eq(index)
304
- end
305
-
306
- (7..13).each do |index|
307
- expect(body['events'][index]['attrs'].fetch('severity', nil)).to eq(nil)
308
- expect(body['events'][index]['attrs'].fetch('sev', nil)).to eq(nil)
309
- expect(body['events'][index]['sev']).to eq(index - 7)
310
- end
311
-
312
- expect(body['events'][14]['attrs'].fetch('severity', nil)).to eq(-1)
313
- expect(body['events'][14].key?("sev")).to eq(false)
314
- expect(body['events'][14]['sev']).to eq(nil)
315
- expect(body['events'][15]['attrs'].fetch('severity', nil)).to eq(7)
316
- expect(body['events'][15].key?("sev")).to eq(false)
317
- expect(body['events'][15]['sev']).to eq(nil)
318
- expect(body['events'][16]['attrs'].fetch('severity', nil)).to eq("invalid")
319
- expect(body['events'][16].key?("sev")).to eq(false)
320
- expect(body['events'][16]['sev']).to eq(nil)
321
- end
322
-
323
- it "works correctly when level event attribute is specified" do
324
- plugin = LogStash::Outputs::Scalyr.new({
325
- 'api_write_token' => '1234',
326
- 'perform_connectivity_check' => false,
327
- 'severity_field' => 'level',
328
- })
329
- allow(plugin).to receive(:send_status).and_return(nil)
330
- plugin.register
331
- result = plugin.build_multi_event_request_array(sample_events_with_level)
332
- body = JSON.parse(result[0][:body])
333
- expect(body['events'].size).to eq(7)
334
-
335
- (0..6).each do |index|
336
- expect(body['events'][index]['attrs'].fetch('level', nil)).to eq(nil)
337
- expect(body['events'][index]['attrs'].fetch('severity', nil)).to eq(nil)
338
- expect(body['events'][index]['attrs'].fetch('sev', nil)).to eq(nil)
339
- expect(body['events'][index]['sev']).to eq(index)
340
- end
341
- end
342
-
343
- it "works correctly when severity event attribute is not specified" do
344
- plugin = LogStash::Outputs::Scalyr.new({
345
- 'api_write_token' => '1234',
346
- 'perform_connectivity_check' => false,
347
- 'severity_field' => 'severity',
348
- })
349
- allow(plugin).to receive(:send_status).and_return(nil)
350
- plugin.register
351
- result = plugin.build_multi_event_request_array(sample_events)
352
- body = JSON.parse(result[0][:body])
353
- expect(body['events'].size).to eq(3)
354
-
355
- (0..2).each do |index|
356
- expect(body['events'][index]['attrs'].fetch('severity', nil)).to eq(nil)
357
- expect(body['events'][index]['attrs'].fetch('sev', nil)).to eq(nil)
358
- expect(body['events'][index]['sev']).to eq(nil)
359
- end
360
- end
361
-
362
- it "works correctly when severity event attribute is not specified but severity field is not set" do
363
- # Since severity_field config option is not set, severity field should be treated as a
364
- # regular event attribute and not as s a special top level Event.sev field
365
- plugin = LogStash::Outputs::Scalyr.new({
366
- 'api_write_token' => '1234',
367
- 'perform_connectivity_check' => false,
368
- 'severity_field' => nil,
369
- })
370
- allow(plugin).to receive(:send_status).and_return(nil)
371
- plugin.register
372
- result = plugin.build_multi_event_request_array(sample_events_with_severity)
373
- body = JSON.parse(result[0][:body])
374
- expect(body['events'].size).to eq(7 + 7 + 3)
375
-
376
- (0..6).each do |index|
377
- expect(body['events'][index]['attrs'].fetch('severity', nil)).to eq(index)
378
- expect(body['events'][index]['sev']).to eq(nil)
379
- end
380
-
381
- (7..13).each do |index|
382
- expect(body['events'][index]['attrs'].fetch('severity', nil)).to eq((index - 7).to_s)
383
- expect(body['events'][index]['sev']).to eq(nil)
384
- end
385
-
386
- expect(body['events'][14]['attrs'].fetch('severity', nil)).to eq(-1)
387
- expect(body['events'][14].key?("sev")).to eq(false)
388
- expect(body['events'][14]['sev']).to eq(nil)
389
- expect(body['events'][15]['attrs'].fetch('severity', nil)).to eq(7)
390
- expect(body['events'][15].key?("sev")).to eq(false)
391
- expect(body['events'][15]['sev']).to eq(nil)
392
- expect(body['events'][16]['attrs'].fetch('severity', nil)).to eq("invalid")
393
- expect(body['events'][16].key?("sev")).to eq(false)
394
- expect(body['events'][16]['sev']).to eq(nil)
395
- end
396
- end
397
-
398
- context "when serverhost_field is missing" do
399
- it "does not contain log file" do
400
- plugin = LogStash::Outputs::Scalyr.new({'api_write_token' => '1234', 'perform_connectivity_check' => false})
401
- allow(plugin).to receive(:send_status).and_return(nil)
402
- plugin.register
403
- result = plugin.build_multi_event_request_array(sample_events)
404
- body = JSON.parse(result[0][:body])
405
- expect(body['events'].size).to eq(3)
406
- expect(body['events'][2]['attrs'].fetch('logfile', nil)).to eq(nil)
407
- end
408
- end
409
-
410
- context "when serverhost_field is present (or mapped)" do
411
- it "creates logfile from serverHost" do
412
- plugin = LogStash::Outputs::Scalyr.new({
413
- 'api_write_token' => '1234',
414
- 'perform_connectivity_check' => false,
415
- 'serverhost_field' => 'source_host',
416
- })
417
- allow(plugin).to receive(:send_status).and_return(nil)
418
- plugin.register
419
- result = plugin.build_multi_event_request_array(sample_events)
420
- body = JSON.parse(result[0][:body])
421
- expect(body['events'].size).to eq(3)
422
- logattrs2 = body['logs'][2]['attrs']
423
- expect(logattrs2.fetch(EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME, nil)).to eq('my host 3')
424
- expect(logattrs2.fetch('logfile', nil)).to eq('/logstash/my host 3')
425
- end
426
- end
427
-
428
- context "when serverhost_field and logfile are present (or mapped)" do
429
- it "does not contain log file" do
430
- plugin = LogStash::Outputs::Scalyr.new({
431
- 'api_write_token' => '1234',
432
- 'perform_connectivity_check' => false,
433
- 'serverhost_field' => 'source_host',
434
- 'logfile_field' => 'source_file',
435
- })
436
- allow(plugin).to receive(:send_status).and_return(nil)
437
- plugin.register
438
- result = plugin.build_multi_event_request_array(sample_events)
439
- body = JSON.parse(result[0][:body])
440
- expect(body['events'].size).to eq(3)
441
- logattrs2 = body['logs'][2]['attrs']
442
- expect(logattrs2.fetch(EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME, nil)).to eq('my host 3')
443
- expect(logattrs2.fetch('logfile', nil)).to eq('my file 3')
444
- end
445
- end
446
-
447
- context "when configured to flatten values with custom delimiter" do
448
- config = {
449
- 'api_write_token' => '1234',
450
- 'perform_connectivity_check' => false,
451
- 'flatten_tags' => true,
452
- 'flat_tag_value' => 'true',
453
- 'flat_tag_prefix' => 'tag_prefix_',
454
- 'flatten_nested_values' => true, # this converts into string 'true'
455
- 'flatten_nested_values_delimiter' => ".",
456
- }
457
- plugin = LogStash::Outputs::Scalyr.new(config)
458
- it "flattens nested values with a period" do
459
- allow(plugin).to receive(:send_status).and_return(nil)
460
- plugin.register
461
- result = plugin.build_multi_event_request_array(sample_events)
462
- body = JSON.parse(result[0][:body])
463
- expect(body['events'].size).to eq(3)
464
- expect(body['events'][2]['attrs']).to eq({
465
- "nested.a" => 1,
466
- "nested.b_0" => 3,
467
- "nested.b_1" => 4,
468
- "nested.b_2" => 5,
469
- 'seq' => 3,
470
- 'source_file' => 'my file 3',
471
- 'source_host' => 'my host 3',
472
- "tag_prefix_t1" => "true",
473
- "tag_prefix_t2" => "true",
474
- "tag_prefix_t3" => "true",
475
- "parser" => "logstashParser",
476
- })
477
- end
478
- end
479
-
480
- context "when configured to flatten values with custom delimiter and deep delimiter fix" do
481
- config = {
482
- 'api_write_token' => '1234',
483
- 'perform_connectivity_check' => false,
484
- 'flatten_tags' => true,
485
- 'flat_tag_value' => 'true',
486
- 'flat_tag_prefix' => 'tag_prefix_',
487
- 'flatten_nested_values' => true, # this converts into string 'true'
488
- 'flatten_nested_values_delimiter' => ".",
489
- 'fix_deep_flattening_delimiters' => true,
490
- }
491
- plugin = LogStash::Outputs::Scalyr.new(config)
492
- it "flattens nested values with a period" do
493
- allow(plugin).to receive(:send_status).and_return(nil)
494
- plugin.register
495
- result = plugin.build_multi_event_request_array(sample_events)
496
- body = JSON.parse(result[0][:body])
497
- expect(body['events'].size).to eq(3)
498
- expect(body['events'][2]['attrs']).to eq({
499
- "nested.a" => 1,
500
- "nested.b.0" => 3,
501
- "nested.b.1" => 4,
502
- "nested.b.2" => 5,
503
- 'seq' => 3,
504
- 'source_file' => 'my file 3',
505
- 'source_host' => 'my host 3',
506
- "tag_prefix_t1" => "true",
507
- "tag_prefix_t2" => "true",
508
- "tag_prefix_t3" => "true",
509
- "parser" => "logstashParser",
510
- })
511
- end
512
- end
513
-
514
- context "when configured to flatten values with custom delimiter, no array flattening" do
515
- config = {
516
- 'api_write_token' => '1234',
517
- 'perform_connectivity_check' => false,
518
- 'flatten_tags' => true,
519
- 'flat_tag_value' => 'true',
520
- 'flat_tag_prefix' => 'tag_prefix_',
521
- 'flatten_nested_values' => true, # this converts into string 'true'
522
- 'flatten_nested_arrays' => false,
523
- 'flatten_nested_values_delimiter' => ".",
524
- }
525
- plugin = LogStash::Outputs::Scalyr.new(config)
526
- it "flattens nested values with a period" do
527
- allow(plugin).to receive(:send_status).and_return(nil)
528
- plugin.register
529
- result = plugin.build_multi_event_request_array(sample_events)
530
- body = JSON.parse(result[0][:body])
531
- expect(body['events'].size).to eq(3)
532
- expect(body['events'][2]['attrs']).to eq({
533
- "nested.a" => 1,
534
- "nested.b" => [3, 4, 5],
535
- 'seq' => 3,
536
- 'source_file' => 'my file 3',
537
- 'source_host' => 'my host 3',
538
- "tag_prefix_t1" => "true",
539
- "tag_prefix_t2" => "true",
540
- "tag_prefix_t3" => "true",
541
- "parser" => "logstashParser",
542
- })
543
- end
544
- end
545
-
546
- context "when configured to flatten values and tags" do
547
- config = {
548
- 'api_write_token' => '1234',
549
- 'perform_connectivity_check' => false,
550
- 'flatten_tags' => true,
551
- 'flat_tag_value' => 'true',
552
- 'flat_tag_prefix' => 'tag_prefix_',
553
- 'flatten_nested_values' => true, # this converts into string 'true'
554
- }
555
- plugin = LogStash::Outputs::Scalyr.new(config)
556
- it "flattens nested values and flattens tags" do
557
- allow(plugin).to receive(:send_status).and_return(nil)
558
- plugin.register
559
- result = plugin.build_multi_event_request_array(sample_events)
560
- body = JSON.parse(result[0][:body])
561
- expect(body['events'].size).to eq(3)
562
- expect(body['events'][2]['attrs']).to eq({
563
- "nested_a" => 1,
564
- "nested_b_0" => 3,
565
- "nested_b_1" => 4,
566
- "nested_b_2" => 5,
567
- 'seq' => 3,
568
- 'source_file' => 'my file 3',
569
- 'source_host' => 'my host 3',
570
- "tag_prefix_t1" => "true",
571
- "tag_prefix_t2" => "true",
572
- "tag_prefix_t3" => "true",
573
- "parser" => "logstashParser",
574
- })
575
- end
576
- end
577
-
578
- context "split large batches into multiple scalyr requests" do
579
- it "estimate_each_event_size is true explicit (default) batch split into 3 scalyr requests" do
580
- config = {
581
- 'api_write_token' => '1234',
582
- 'perform_connectivity_check' => false,
583
- 'flatten_tags' => true,
584
- 'flat_tag_value' => 'true',
585
- 'flat_tag_prefix' => 'tag_prefix_',
586
- 'flatten_nested_values' => true, # this converts into string 'true'
587
- 'max_request_buffer' => 10,
588
- 'estimate_each_event_size' => true
589
- }
590
- plugin = LogStash::Outputs::Scalyr.new(config)
591
-
592
- allow(plugin).to receive(:send_status).and_return(nil)
593
- plugin.register
594
- result = plugin.build_multi_event_request_array(sample_events)
595
- expect(result.size).to eq(3)
596
-
597
- body = JSON.parse(result[0][:body])
598
- expect(body['events'].size).to eq(1)
599
-
600
- body = JSON.parse(result[1][:body])
601
- expect(body['events'].size).to eq(1)
602
-
603
- body = JSON.parse(result[2][:body])
604
- expect(body['events'].size).to eq(1)
605
- expect(body['events'][0]['attrs']).to eq({
606
- "nested_a" => 1,
607
- "nested_b_0" => 3,
608
- "nested_b_1" => 4,
609
- "nested_b_2" => 5,
610
- 'seq' => 3,
611
- 'source_file' => 'my file 3',
612
- 'source_host' => 'my host 3',
613
- "tag_prefix_t1" => "true",
614
- "tag_prefix_t2" => "true",
615
- "tag_prefix_t3" => "true",
616
- "parser" => "logstashParser",
617
- })
618
- end
619
-
620
- it "estimate_each_event_size is true implicit (default) batch split into 3 scalyr requests" do
621
- config = {
622
- 'api_write_token' => '1234',
623
- 'perform_connectivity_check' => false,
624
- 'flatten_tags' => true,
625
- 'flat_tag_value' => 'true',
626
- 'flat_tag_prefix' => 'tag_prefix_',
627
- 'flatten_nested_values' => true, # this converts into string 'true'
628
- 'max_request_buffer' => 10,
629
- }
630
- plugin = LogStash::Outputs::Scalyr.new(config)
631
-
632
- allow(plugin).to receive(:send_status).and_return(nil)
633
- plugin.register
634
- result = plugin.build_multi_event_request_array(sample_events)
635
- expect(result.size).to eq(3)
636
-
637
- body = JSON.parse(result[0][:body])
638
- expect(body['events'].size).to eq(1)
639
-
640
- body = JSON.parse(result[1][:body])
641
- expect(body['events'].size).to eq(1)
642
-
643
- body = JSON.parse(result[2][:body])
644
- expect(body['events'].size).to eq(1)
645
- expect(body['events'][0]['attrs']).to eq({
646
- "nested_a" => 1,
647
- "nested_b_0" => 3,
648
- "nested_b_1" => 4,
649
- "nested_b_2" => 5,
650
- 'seq' => 3,
651
- 'source_file' => 'my file 3',
652
- 'source_host' => 'my host 3',
653
- "tag_prefix_t1" => "true",
654
- "tag_prefix_t2" => "true",
655
- "tag_prefix_t3" => "true",
656
- "parser" => "logstashParser",
657
- })
658
- end
659
-
660
- it "estimate_each_event_size is false batch not split into multiple scalyr requests" do
661
- config = {
662
- 'api_write_token' => '1234',
663
- 'perform_connectivity_check' => false,
664
- 'flatten_tags' => true,
665
- 'flat_tag_value' => 'true',
666
- 'flat_tag_prefix' => 'tag_prefix_',
667
- 'flatten_nested_values' => true, # this converts into string 'true'
668
- 'max_request_buffer' => 10,
669
- 'estimate_each_event_size' => false
670
- }
671
- plugin = LogStash::Outputs::Scalyr.new(config)
672
-
673
- allow(plugin).to receive(:send_status).and_return(nil)
674
- plugin.register
675
- result = plugin.build_multi_event_request_array(sample_events)
676
- expect(result.size).to eq(1)
677
-
678
- body = JSON.parse(result[0][:body])
679
- expect(body['events'].size).to eq(3)
680
- expect(body['events'][2]['attrs']).to eq({
681
- "nested_a" => 1,
682
- "nested_b_0" => 3,
683
- "nested_b_1" => 4,
684
- "nested_b_2" => 5,
685
- 'seq' => 3,
686
- 'source_file' => 'my file 3',
687
- 'source_host' => 'my host 3',
688
- "tag_prefix_t1" => "true",
689
- "tag_prefix_t2" => "true",
690
- "tag_prefix_t3" => "true",
691
- "parser" => "logstashParser",
692
- })
693
- end
694
- end
695
-
696
- context "when not configured to flatten values and tags" do
697
- config = {
698
- 'api_write_token' => '1234',
699
- 'perform_connectivity_check' => false,
700
- }
701
- plugin = LogStash::Outputs::Scalyr.new(config)
702
- it "does not flatten" do
703
- allow(plugin).to receive(:send_status).and_return(nil)
704
- plugin.register
705
- result = plugin.build_multi_event_request_array(sample_events)
706
- body = JSON.parse(result[0][:body])
707
- expect(body['events'].size).to eq(3)
708
- expect(body['events'][2]['attrs']).to eq({
709
- "nested" => {'a'=>1, 'b'=>[3,4,5]},
710
- 'seq' => 3,
711
- 'source_file' => 'my file 3',
712
- 'source_host' => 'my host 3',
713
- "tags" => ["t1", "t2", "t3"],
714
- "parser" => "logstashParser",
715
- })
716
- end
717
- end
718
-
719
- context "when configured to flatten with max keys configured to 3" do
720
- config = {
721
- 'api_write_token' => '1234',
722
- 'perform_connectivity_check' => false,
723
- 'flatten_nested_values' => true, # this converts into string 'true'
724
- 'flattening_max_key_count' => 3,
725
- }
726
- plugin = LogStash::Outputs::Scalyr.new(config)
727
- it "does not flatten" do
728
- allow(plugin).to receive(:send_status).and_return(nil)
729
- plugin.register
730
- allow(plugin.instance_variable_get(:@logger)).to receive(:warn)
731
- result = plugin.build_multi_event_request_array(sample_events)
732
- body = JSON.parse(result[0][:body])
733
- expect(body['events'].size).to eq(3)
734
- expect(body['events'][2]['attrs']).to eq({
735
- "nested" => {'a'=>1, 'b'=>[3,4,5]},
736
- 'seq' => 3,
737
- 'source_file' => 'my file 3',
738
- 'source_host' => 'my host 3',
739
- "tags" => ["t1", "t2", "t3"],
740
- "parser" => "logstashParser",
741
- })
742
- expect(plugin.instance_variable_get(:@logger)).to have_received(:warn).with("Error while flattening record",
743
- {
744
- :error_message=>"Resulting flattened object will contain more keys than the configured flattening_max_key_count of 3",
745
- :sample_keys=>["parser", "tags_2", "tags_1", "tags_0"]
746
- }
747
- ).exactly(3).times
748
- end
749
- end
750
-
751
- context "serverHost attribute handling" do
752
- it "no serverHost defined in server_attributes, no serverHost defined on event level - should use node hostname as the default session level value" do
753
- config = {
754
- 'api_write_token' => '1234',
755
- 'perform_connectivity_check' => false,
756
- }
757
- plugin = LogStash::Outputs::Scalyr.new(config)
758
-
759
- allow(plugin).to receive(:send_status).and_return(nil)
760
- plugin.register
761
- e = LogStash::Event.new
762
- result = plugin.build_multi_event_request_array([e])
763
- body = JSON.parse(result[0][:body])
764
- expect(body['sessionInfo']['serverHost']).to eq(NODE_HOSTNAME)
765
-
766
- expect(body['logs']).to eq([])
767
- expect(body['events'].size).to eq(1)
768
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
769
- end
770
-
771
- it "serverHost defined in server_attributes, nothing defined on event level - server_attributes value should be used" do
772
- config = {
773
- 'api_write_token' => '1234',
774
- 'perform_connectivity_check' => false,
775
- 'server_attributes' => {'serverHost' => 'fooHost'}
776
- }
777
- plugin = LogStash::Outputs::Scalyr.new(config)
778
-
779
- allow(plugin).to receive(:send_status).and_return(nil)
780
- plugin.register
781
- e = LogStash::Event.new
782
- result = plugin.build_multi_event_request_array([e])
783
- body = JSON.parse(result[0][:body])
784
- expect(body['sessionInfo']['serverHost']).to eq('fooHost')
785
- expect(body['events'].size).to eq(1)
786
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
787
- end
788
-
789
- # sessionInfo serverHost always has precedence this means it's important that we don't include it if event level attribute is set, otherwise
790
- # session level one would simply always overwrite event level one which would be ignored
791
- it "serverHost defined in server_attributes (explicitly defined), event level serverHost defined - event level value should be used" do
792
- config = {
793
- 'api_write_token' => '1234',
794
- 'perform_connectivity_check' => false,
795
- 'server_attributes' => {'serverHost' => 'fooHost', 'attr1' => 'val1'}
796
- }
797
- plugin = LogStash::Outputs::Scalyr.new(config)
798
-
799
- allow(plugin).to receive(:send_status).and_return(nil)
800
- plugin.register
801
- expect(plugin.server_attributes['serverHost']).to eq('fooHost')
802
-
803
- e1 = LogStash::Event.new
804
- e1.set('a1', 'v1')
805
- e1.set('serverHost', 'event-host-1')
806
-
807
- e2 = LogStash::Event.new
808
- e2.set('a2', 'v2')
809
- e2.set('serverHost', 'event-host-2')
810
-
811
- e3 = LogStash::Event.new
812
- e3.set('a3', 'v3')
813
- e3.set('serverHost', 'event-host-2')
814
-
815
- e4 = LogStash::Event.new
816
- e4.set('a4', 'v4')
817
- e4.set('serverHost', 'event-host-1')
818
-
819
- result = plugin.build_multi_event_request_array([e1, e2, e3, e4])
820
- body = JSON.parse(result[0][:body])
821
- expect(body['sessionInfo']['serverHost']).to eq(nil)
822
- expect(body['sessionInfo']['attr1']).to eq('val1')
823
-
824
- expect(body['logs'].size).to eq(2)
825
- expect(body['logs'][0]['id']).to eq(1)
826
- expect(body['logs'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-1')
827
- expect(body['logs'][1]['id']).to eq(2)
828
- expect(body['logs'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-2')
829
-
830
- expect(body['events'].size).to eq(4)
831
- expect(body['events'][0]['log']).to eq(1)
832
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
833
- expect(body['events'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
834
-
835
- expect(body['events'][1]['log']).to eq(2)
836
- expect(body['events'][1]['attrs']["serverHost"]).to eq(nil)
837
- expect(body['events'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
838
-
839
- expect(body['events'][2]['log']).to eq(2)
840
- expect(body['events'][2]['attrs']["serverHost"]).to eq(nil)
841
- expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
842
-
843
- expect(body['events'][3]['log']).to eq(1)
844
- expect(body['events'][3]['attrs']["serverHost"]).to eq(nil)
845
- expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
846
- end
847
-
848
- it "serverHost defined in server_attributes (defined via node hostname), event level serverHost defined - event level value should be used" do
849
- config = {
850
- 'api_write_token' => '1234',
851
- 'perform_connectivity_check' => false,
852
- 'server_attributes' => {'attr1' => 'val1'}
853
- }
854
- plugin = LogStash::Outputs::Scalyr.new(config)
855
-
856
- allow(plugin).to receive(:send_status).and_return(nil)
857
- plugin.register
858
-
859
- expect(plugin.server_attributes['serverHost']).to eq(NODE_HOSTNAME)
860
-
861
- e1 = LogStash::Event.new
862
- e1.set('a1', 'v1')
863
- e1.set('serverHost', 'event-host-1')
864
-
865
- e2 = LogStash::Event.new
866
- e2.set('a2', 'v2')
867
- e2.set('serverHost', 'event-host-2')
868
-
869
- e3 = LogStash::Event.new
870
- e3.set('a3', 'v3')
871
- e3.set('serverHost', 'event-host-2')
872
-
873
- e4 = LogStash::Event.new
874
- e4.set('a4', 'v4')
875
- e4.set('serverHost', 'event-host-1')
876
-
877
- result = plugin.build_multi_event_request_array([e1, e2, e3, e4])
878
- body = JSON.parse(result[0][:body])
879
- expect(body['sessionInfo']['serverHost']).to eq(nil)
880
- expect(body['sessionInfo']['attr1']).to eq('val1')
881
-
882
- expect(body['logs'].size).to eq(2)
883
- expect(body['logs'][0]['id']).to eq(1)
884
- expect(body['logs'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-1')
885
- expect(body['logs'][1]['id']).to eq(2)
886
- expect(body['logs'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-2')
887
-
888
- expect(body['events'].size).to eq(4)
889
- expect(body['events'][0]['log']).to eq(1)
890
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
891
- expect(body['events'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
892
-
893
- expect(body['events'][1]['log']).to eq(2)
894
- expect(body['events'][1]['attrs']["serverHost"]).to eq(nil)
895
- expect(body['events'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
896
-
897
- expect(body['events'][2]['log']).to eq(2)
898
- expect(body['events'][2]['attrs']["serverHost"]).to eq(nil)
899
- expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
900
-
901
- expect(body['events'][3]['log']).to eq(1)
902
- expect(body['events'][3]['attrs']["serverHost"]).to eq(nil)
903
- expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
904
- end
905
-
906
- # If set_session_level_serverhost_on_events config option is true, we set session level serverHost on events which don't
907
- # explicitly define this special attribute.
908
- it "serverHost defined in server_attributes (explicitly defined), event level serverHost defined - event level value should be used and server level one for events without server host" do
909
- config = {
910
- 'api_write_token' => '1234',
911
- 'perform_connectivity_check' => false,
912
- 'server_attributes' => {'serverHost' => 'top-level-session-host', 'attr1' => 'val1'}
913
- }
914
- plugin = LogStash::Outputs::Scalyr.new(config)
915
-
916
- allow(plugin).to receive(:send_status).and_return(nil)
917
- plugin.register
918
- expect(plugin.server_attributes['serverHost']).to eq('top-level-session-host')
919
-
920
- e1 = LogStash::Event.new
921
- e1.set('a1', 'v1')
922
- e1.set('serverHost', 'event-host-1')
923
-
924
- e2 = LogStash::Event.new
925
- e2.set('a2', 'v2')
926
-
927
- e3 = LogStash::Event.new
928
- e3.set('a3', 'v3')
929
-
930
- e4 = LogStash::Event.new
931
- e4.set('a4', 'v4')
932
- e4.set('serverHost', 'event-host-1')
933
-
934
- result = plugin.build_multi_event_request_array([e1, e2, e3, e4])
935
- body = JSON.parse(result[0][:body])
936
- expect(body['sessionInfo']['serverHost']).to eq(nil)
937
- expect(body['sessionInfo']['attr1']).to eq('val1')
938
-
939
- expect(body['logs'].size).to eq(1)
940
- expect(body['logs'][0]['id']).to eq(1)
941
- expect(body['logs'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-1')
942
-
943
- expect(body['events'].size).to eq(4)
944
- expect(body['events'][0]['log']).to eq(1)
945
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
946
- expect(body['events'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
947
-
948
- expect(body['events'][1]['log']).to eq(nil)
949
- expect(body['events'][1]['attrs']["serverHost"]).to eq(nil)
950
- expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq("top-level-session-host")
951
-
952
- expect(body['events'][1]['log']).to eq(nil)
953
- expect(body['events'][2]['attrs']["serverHost"]).to eq(nil)
954
- expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq("top-level-session-host")
955
-
956
- expect(body['events'][3]['log']).to eq(1)
957
- expect(body['events'][3]['attrs']["serverHost"]).to eq(nil)
958
- expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
959
- end
960
-
961
- it "no serverHost defined, event level serverHost defined via non-default serverhost_field - event level value should be used" do
962
- config = {
963
- 'api_write_token' => '1234',
964
- 'perform_connectivity_check' => false,
965
- 'server_attributes' => {'attr1' => 'val1'},
966
- 'use_hostname_for_serverhost' => false,
967
- 'serverhost_field' => 'custom_server_host',
968
- }
969
- plugin = LogStash::Outputs::Scalyr.new(config)
970
-
971
- allow(plugin).to receive(:send_status).and_return(nil)
972
- plugin.register
973
-
974
-
975
- expect(plugin.server_attributes['serverHost']).to eq(nil)
976
-
977
- e1 = LogStash::Event.new
978
- e1.set('a1', 'v1')
979
- e1.set('custom_server_host', 'event-host-1')
980
-
981
- e2 = LogStash::Event.new
982
- e2.set('a2', 'v2')
983
- e2.set('custom_server_host', 'event-host-2')
984
-
985
- e3 = LogStash::Event.new
986
- e3.set('a3', 'v3')
987
- e3.set('custom_server_host', 'event-host-2')
988
-
989
- e4 = LogStash::Event.new
990
- e4.set('a4', 'v4')
991
- e4.set('custom_server_host', 'event-host-2')
992
-
993
- result = plugin.build_multi_event_request_array([e1, e2, e3, e4])
994
- body = JSON.parse(result[0][:body])
995
- expect(body['sessionInfo']['serverHost']).to eq(nil)
996
- expect(body['sessionInfo']['attr1']).to eq('val1')
997
-
998
- expect(body['logs'][0]['id']).to eq(1)
999
- expect(body['logs'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-1')
1000
- expect(body['logs'][1]['id']).to eq(2)
1001
- expect(body['logs'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-2')
1002
-
1003
- expect(body['events'].size).to eq(4)
1004
- expect(body['events'][0]['log']).to eq(1)
1005
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
1006
- expect(body['events'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1007
- expect(body['events'][1]['log']).to eq(2)
1008
- expect(body['events'][1]['attrs']["serverHost"]).to eq(nil)
1009
- expect(body['events'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1010
- expect(body['events'][2]['log']).to eq(2)
1011
- expect(body['events'][2]['attrs']["serverHost"]).to eq(nil)
1012
- expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1013
- expect(body['events'][3]['log']).to eq(2)
1014
- expect(body['events'][3]['attrs']["serverHost"]).to eq(nil)
1015
- expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1016
- end
1017
-
1018
- it "no serverHost defined, event level serverHost defined - event level value should be used" do
1019
- config = {
1020
- 'api_write_token' => '1234',
1021
- 'perform_connectivity_check' => false,
1022
- 'server_attributes' => {'attr1' => 'val1'},
1023
- 'use_hostname_for_serverhost' => false
1024
- }
1025
- plugin = LogStash::Outputs::Scalyr.new(config)
1026
-
1027
- allow(plugin).to receive(:send_status).and_return(nil)
1028
- plugin.register
1029
-
1030
- expect(plugin.server_attributes['serverHost']).to eq(nil)
1031
-
1032
- e1 = LogStash::Event.new
1033
- e1.set('a1', 'v1')
1034
- e1.set('serverHost', 'event-host-1')
1035
-
1036
- e2 = LogStash::Event.new
1037
- e2.set('a2', 'v2')
1038
- e2.set('serverHost', 'event-host-2')
1039
-
1040
- e3 = LogStash::Event.new
1041
- e3.set('a3', 'v3')
1042
- e3.set('serverHost', 'event-host-2')
1043
-
1044
- e4 = LogStash::Event.new
1045
- e4.set('a4', 'v4')
1046
- e4.set('serverHost', 'event-host-2')
1047
-
1048
- result = plugin.build_multi_event_request_array([e1, e2, e3, e4])
1049
- body = JSON.parse(result[0][:body])
1050
- expect(body['sessionInfo']['serverHost']).to eq(nil)
1051
- expect(body['sessionInfo']['attr1']).to eq('val1')
1052
-
1053
- expect(body['logs'][0]['id']).to eq(1)
1054
- expect(body['logs'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-1')
1055
- expect(body['logs'][1]['id']).to eq(2)
1056
- expect(body['logs'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq('event-host-2')
1057
-
1058
- expect(body['events'].size).to eq(4)
1059
- expect(body['events'][0]['log']).to eq(1)
1060
- expect(body['events'][0]['attrs']["serverHost"]).to eq(nil)
1061
- expect(body['events'][0]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1062
- expect(body['events'][1]['log']).to eq(2)
1063
- expect(body['events'][1]['attrs']["serverHost"]).to eq(nil)
1064
- expect(body['events'][1]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1065
- expect(body['events'][2]['log']).to eq(2)
1066
- expect(body['events'][2]['attrs']["serverHost"]).to eq(nil)
1067
- expect(body['events'][2]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1068
- expect(body['events'][3]['log']).to eq(2)
1069
- expect(body['events'][3]['attrs']["serverHost"]).to eq(nil)
1070
- expect(body['events'][3]['attrs'][EVENT_LEVEL_SERVER_HOST_ATTRIBUTE_NAME]).to eq(nil)
1071
- end
1072
- end
1073
-
1074
- context "when receiving an event with Bignums" do
1075
- config = {
1076
- 'api_write_token' => '1234',
1077
- 'perform_connectivity_check' => false,
1078
- }
1079
- plugin = LogStash::Outputs::Scalyr.new(config)
1080
- it "doesn't throw an error" do
1081
- allow(plugin).to receive(:send_status).and_return(nil)
1082
- plugin.register
1083
- e = LogStash::Event.new
1084
- e.set('bignumber', 2000023030042002050202030320240)
1085
- allow(plugin.instance_variable_get(:@logger)).to receive(:error)
1086
- result = plugin.build_multi_event_request_array([e])
1087
- body = JSON.parse(result[0][:body])
1088
- expect(body['events'].size).to eq(1)
1089
- expect(body['events'][0]['attrs']['bignumber']).to be_a_kind_of(String)
1090
- expect(plugin.instance_variable_get(:@logger)).to_not receive(:error)
1091
- end
1092
- end
1093
-
1094
- context "host attribute handling" do
1095
- it "host attribute removed by default" do
1096
- config = {
1097
- 'api_write_token' => '1234',
1098
- 'perform_connectivity_check' => false,
1099
- }
1100
- plugin = LogStash::Outputs::Scalyr.new(config)
1101
-
1102
- allow(plugin).to receive(:send_status).and_return(nil)
1103
- plugin.register
1104
-
1105
- expect(plugin.server_attributes['serverHost']).to eq(NODE_HOSTNAME)
1106
-
1107
- e1 = LogStash::Event.new
1108
- e1.set('a1', 'v1')
1109
- e1.set('host', 'event-host-1')
1110
-
1111
- result = plugin.build_multi_event_request_array([e1])
1112
- body = JSON.parse(result[0][:body])
1113
- expect(body['sessionInfo']['serverHost']).to eq(NODE_HOSTNAME)
1114
-
1115
- expect(body['logs'].size).to eq(0)
1116
-
1117
- expect(body['events'].size).to eq(1)
1118
- expect(body['events'][0]['attrs']["host"]).to eq(nil)
1119
- end
1120
-
1121
- it "host attribute not removed if config option set" do
1122
- config = {
1123
- 'api_write_token' => '1234',
1124
- 'perform_connectivity_check' => false,
1125
- 'remove_host_attribute_from_events' => false,
1126
- }
1127
- plugin = LogStash::Outputs::Scalyr.new(config)
1128
-
1129
- allow(plugin).to receive(:send_status).and_return(nil)
1130
- plugin.register
1131
-
1132
- expect(plugin.server_attributes['serverHost']).to eq(NODE_HOSTNAME)
1133
-
1134
- e1 = LogStash::Event.new
1135
- e1.set('a1', 'v1')
1136
- e1.set('host', 'event-host-1')
1137
-
1138
- result = plugin.build_multi_event_request_array([e1])
1139
- body = JSON.parse(result[0][:body])
1140
- expect(body['sessionInfo']['serverHost']).to eq(NODE_HOSTNAME)
1141
-
1142
- expect(body['logs'].size).to eq(0)
1143
-
1144
- expect(body['events'].size).to eq(1)
1145
- expect(body['events'][0]['attrs']["host"]).to eq("event-host-1")
1146
- end
1147
- end
1148
-
1149
- context "when using custom json library" do
1150
- it "stdlib (implicit)" do
1151
- config = {
1152
- 'api_write_token' => '1234',
1153
- 'perform_connectivity_check' => false,
1154
- }
1155
- plugin = LogStash::Outputs::Scalyr.new(config)
1156
-
1157
- allow(plugin).to receive(:send_status).and_return(nil)
1158
- plugin.register
1159
- e = LogStash::Event.new
1160
- e.set('bignumber', 20)
1161
- result = plugin.build_multi_event_request_array([e])
1162
- body = JSON.parse(result[0][:body])
1163
- expect(result[0][:body]).to include(sprintf('{"serverHost":"%s","monitor":"pluginLogstash"}', NODE_HOSTNAME))
1164
- expect(body['events'].size).to eq(1)
1165
- end
1166
-
1167
- it "stdlib (explicit)" do
1168
- config = {
1169
- 'api_write_token' => '1234',
1170
- 'perform_connectivity_check' => false,
1171
- 'json_library' => 'stdlib'
1172
- }
1173
- plugin = LogStash::Outputs::Scalyr.new(config)
1174
-
1175
- allow(plugin).to receive(:send_status).and_return(nil)
1176
- plugin.register
1177
- e = LogStash::Event.new
1178
- e.set('bignumber', 20)
1179
- result = plugin.build_multi_event_request_array([e])
1180
- body = JSON.parse(result[0][:body])
1181
- expect(result[0][:body]).to include(sprintf('{"serverHost":"%s","monitor":"pluginLogstash"}', NODE_HOSTNAME))
1182
- expect(body['events'].size).to eq(1)
1183
- end
1184
-
1185
- it "jrjackson" do
1186
- config = {
1187
- 'api_write_token' => '1234',
1188
- 'perform_connectivity_check' => false,
1189
- 'json_library' => 'jrjackson'
1190
- }
1191
- plugin = LogStash::Outputs::Scalyr.new(config)
1192
-
1193
- allow(plugin).to receive(:send_status).and_return(nil)
1194
- plugin.register
1195
- e = LogStash::Event.new
1196
- e.set('bignumber', 20)
1197
- result = plugin.build_multi_event_request_array([e])
1198
- body = JSON.parse(result[0][:body])
1199
- expect(result[0][:body]).to include(sprintf('{"serverHost":"%s","monitor":"pluginLogstash"}', NODE_HOSTNAME))
1200
- expect(body['events'].size).to eq(1)
1201
- end
1202
- end
1203
-
1204
- context "scalyr_server config option handling and connectivity check" do
1205
- it "doesn't throw an error on valid url" do
1206
- config = {
1207
- 'api_write_token' => '1234',
1208
- 'perform_connectivity_check' => false,
1209
- 'scalyr_server' => 'https://agent.scalyr.com'
1210
- }
1211
- plugin = LogStash::Outputs::Scalyr.new(config)
1212
- plugin.register
1213
-
1214
- config = {
1215
- 'api_write_token' => '1234',
1216
- 'perform_connectivity_check' => false,
1217
- 'scalyr_server' => 'https://eu.scalyr.com'
1218
- }
1219
- plugin = LogStash::Outputs::Scalyr.new(config)
1220
- plugin.register
1221
- end
1222
-
1223
- it "throws on invalid URL" do
1224
- config = {
1225
- 'api_write_token' => '1234',
1226
- 'perform_connectivity_check' => false,
1227
- 'scalyr_server' => 'agent.scalyr.com'
1228
- }
1229
- plugin = LogStash::Outputs::Scalyr.new(config)
1230
- expect { plugin.register }.to raise_error(LogStash::ConfigurationError, /is not a valid URL/)
1231
-
1232
- config = {
1233
- 'api_write_token' => '1234',
1234
- 'perform_connectivity_check' => false,
1235
- 'scalyr_server' => 'eu.scalyr.com'
1236
- }
1237
- plugin = LogStash::Outputs::Scalyr.new(config)
1238
- expect { plugin.register }.to raise_error(LogStash::ConfigurationError, /is not a valid URL/)
1239
- end
1240
-
1241
- it "throws on invalid hostname" do
1242
- config = {
1243
- 'api_write_token' => '1234',
1244
- 'scalyr_server' => 'https://agent.invalid.foo.scalyr.com',
1245
- 'perform_connectivity_check' => true
1246
- }
1247
- plugin = LogStash::Outputs::Scalyr.new(config)
1248
- expect { plugin.register }.to raise_error(LogStash::ConfigurationError, /Received error when trying to communicate/)
1249
- end
1250
-
1251
- it "throws on invalid api key" do
1252
- config = {
1253
- 'api_write_token' => '1234',
1254
- 'scalyr_server' => 'https://agent.scalyr.com',
1255
- 'perform_connectivity_check' => true
1256
- }
1257
- plugin = LogStash::Outputs::Scalyr.new(config)
1258
- expect { plugin.register }.to raise_error(LogStash::ConfigurationError, /Received 401 from Scalyr API during/)
1259
- end
1260
- end
1261
- end
1262
- end