fluent-plugin-cloudwatch-logs-foxtrot9 0.0.1
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.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/ISSUE_TEMPLATE.md +21 -0
- data/LICENSE.txt +22 -0
- data/README.md +368 -0
- data/Rakefile +10 -0
- data/example/fluentd.conf +23 -0
- data/fluent-plugin-cloudwatch-logs-foxtrot9.gemspec +28 -0
- data/lib/fluent/plugin/cloudwatch/logs.rb +11 -0
- data/lib/fluent/plugin/cloudwatch/logs/version.rb +9 -0
- data/lib/fluent/plugin/in_cloudwatch_logs.rb +203 -0
- data/lib/fluent/plugin/out_cloudwatch_logs.rb +473 -0
- data/test/plugin/test_in_cloudwatch_logs.rb +524 -0
- data/test/plugin/test_out_cloudwatch_logs.rb +757 -0
- data/test/test_helper.rb +106 -0
- metadata +161 -0
@@ -0,0 +1,524 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'fluent/test/driver/input'
|
3
|
+
require 'fluent/test/helpers'
|
4
|
+
require 'date'
|
5
|
+
require 'fluent/plugin/in_cloudwatch_logs'
|
6
|
+
|
7
|
+
class CloudwatchLogsInputTest < Test::Unit::TestCase
|
8
|
+
include CloudwatchLogsTestHelper
|
9
|
+
include Fluent::Test::Helpers
|
10
|
+
|
11
|
+
def setup
|
12
|
+
Fluent::Test.setup
|
13
|
+
end
|
14
|
+
|
15
|
+
sub_test_case "configure" do
|
16
|
+
def test_configure
|
17
|
+
d = create_driver(<<-EOC)
|
18
|
+
@type cloudwatch_logs
|
19
|
+
aws_key_id test_id
|
20
|
+
aws_sec_key test_key
|
21
|
+
region us-east-1
|
22
|
+
tag test
|
23
|
+
log_group_name group
|
24
|
+
log_stream_name stream
|
25
|
+
use_log_stream_name_prefix true
|
26
|
+
state_file /tmp/state
|
27
|
+
use_aws_timestamp true
|
28
|
+
EOC
|
29
|
+
|
30
|
+
assert_equal('test_id', d.instance.aws_key_id)
|
31
|
+
assert_equal('test_key', d.instance.aws_sec_key)
|
32
|
+
assert_equal('us-east-1', d.instance.region)
|
33
|
+
assert_equal('test', d.instance.tag)
|
34
|
+
assert_equal('group', d.instance.log_group_name)
|
35
|
+
assert_equal('stream', d.instance.log_stream_name)
|
36
|
+
assert_equal(true, d.instance.use_log_stream_name_prefix)
|
37
|
+
assert_equal('/tmp/state', d.instance.state_file)
|
38
|
+
assert_equal(:yajl, d.instance.json_handler)
|
39
|
+
assert_equal(true, d.instance.use_aws_timestamp)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
sub_test_case "real world" do
|
44
|
+
def setup
|
45
|
+
omit if ENV["CI"] == "true"
|
46
|
+
end
|
47
|
+
|
48
|
+
def teardown
|
49
|
+
return if ENV["CI"] == "true"
|
50
|
+
|
51
|
+
clear_log_group
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_emit
|
55
|
+
create_log_stream
|
56
|
+
|
57
|
+
time_ms = (Time.now.to_f * 1000).floor
|
58
|
+
put_log_events([
|
59
|
+
{timestamp: time_ms, message: '{"cloudwatch":"logs1"}'},
|
60
|
+
{timestamp: time_ms, message: '{"cloudwatch":"logs2"}'},
|
61
|
+
])
|
62
|
+
|
63
|
+
sleep 5
|
64
|
+
|
65
|
+
d = create_driver
|
66
|
+
d.run(expect_emits: 2, timeout: 5)
|
67
|
+
|
68
|
+
emits = d.events
|
69
|
+
assert_equal(2, emits.size)
|
70
|
+
assert_equal(['test', (time_ms / 1000).floor, {'cloudwatch' => 'logs1'}], emits[0])
|
71
|
+
assert_equal(['test', (time_ms / 1000).floor, {'cloudwatch' => 'logs2'}], emits[1])
|
72
|
+
end
|
73
|
+
|
74
|
+
def test_emit_with_aws_timestamp
|
75
|
+
create_log_stream
|
76
|
+
|
77
|
+
time_ms = (Time.now.to_f * 1000).floor
|
78
|
+
log_time_ms = time_ms - 10000
|
79
|
+
put_log_events([
|
80
|
+
{timestamp: time_ms, message: Time.at(log_time_ms/1000.floor).to_s + ",Cloudwatch non json logs1"},
|
81
|
+
{timestamp: time_ms, message: Time.at(log_time_ms/1000.floor).to_s + ",Cloudwatch non json logs2"},
|
82
|
+
])
|
83
|
+
|
84
|
+
sleep 5
|
85
|
+
|
86
|
+
d = create_driver(csv_format_config_aws_timestamp)
|
87
|
+
d.run(expect_emits: 2, timeout: 5)
|
88
|
+
|
89
|
+
emits = d.events
|
90
|
+
assert_equal(2, emits.size)
|
91
|
+
assert_equal(['test', (time_ms / 1000).floor, {"message"=>"Cloudwatch non json logs1"}], emits[0])
|
92
|
+
assert_equal(['test', (time_ms / 1000).floor, {"message"=>"Cloudwatch non json logs2"}], emits[1])
|
93
|
+
end
|
94
|
+
|
95
|
+
def test_emit_with_log_timestamp
|
96
|
+
create_log_stream
|
97
|
+
|
98
|
+
time_ms = (Time.now.to_f * 1000).floor
|
99
|
+
log_time_ms = time_ms - 10000
|
100
|
+
put_log_events([
|
101
|
+
{timestamp: time_ms, message: Time.at(log_time_ms/1000.floor).to_s + ",Cloudwatch non json logs1"},
|
102
|
+
{timestamp: time_ms, message: Time.at(log_time_ms/1000.floor).to_s + ",Cloudwatch non json logs2"},
|
103
|
+
])
|
104
|
+
|
105
|
+
sleep 5
|
106
|
+
|
107
|
+
d = create_driver(csv_format_config)
|
108
|
+
d.run(expect_emits: 2, timeout: 5)
|
109
|
+
|
110
|
+
emits = d.events
|
111
|
+
assert_equal(2, emits.size)
|
112
|
+
assert_equal(['test', (log_time_ms / 1000).floor, {"message"=>"Cloudwatch non json logs1"}], emits[0])
|
113
|
+
assert_equal(['test', (log_time_ms / 1000).floor, {"message"=>"Cloudwatch non json logs2"}], emits[1])
|
114
|
+
end
|
115
|
+
|
116
|
+
def test_emit_width_format
|
117
|
+
create_log_stream
|
118
|
+
|
119
|
+
time_ms = (Time.now.to_f * 1000).floor
|
120
|
+
put_log_events([
|
121
|
+
{timestamp: time_ms, message: 'logs1'},
|
122
|
+
{timestamp: time_ms, message: 'logs2'},
|
123
|
+
])
|
124
|
+
|
125
|
+
sleep 5
|
126
|
+
|
127
|
+
d = create_driver(<<-EOC)
|
128
|
+
tag test
|
129
|
+
@type cloudwatch_logs
|
130
|
+
log_group_name #{log_group_name}
|
131
|
+
log_stream_name #{log_stream_name}
|
132
|
+
state_file /tmp/state
|
133
|
+
format /^(?<cloudwatch>[^ ]*)?/
|
134
|
+
#{aws_key_id}
|
135
|
+
#{aws_sec_key}
|
136
|
+
#{region}
|
137
|
+
#{endpoint}
|
138
|
+
EOC
|
139
|
+
|
140
|
+
d.run(expect_emits: 2, timeout: 5)
|
141
|
+
|
142
|
+
emits = d.events
|
143
|
+
assert_equal(2, emits.size)
|
144
|
+
assert_equal('test', emits[0][0])
|
145
|
+
assert_in_delta((time_ms / 1000).floor, emits[0][1], 10)
|
146
|
+
assert_equal({'cloudwatch' => 'logs1'}, emits[0][2])
|
147
|
+
assert_equal('test', emits[1][0])
|
148
|
+
assert_in_delta((time_ms / 1000).floor, emits[1][1], 10)
|
149
|
+
assert_equal({'cloudwatch' => 'logs2'}, emits[1][2])
|
150
|
+
end
|
151
|
+
|
152
|
+
def test_emit_with_prefix
|
153
|
+
new_log_stream("testprefix")
|
154
|
+
create_log_stream
|
155
|
+
|
156
|
+
time_ms = (Time.now.to_f * 1000).floor
|
157
|
+
put_log_events([
|
158
|
+
{timestamp: time_ms + 1000, message: '{"cloudwatch":"logs1"}'},
|
159
|
+
{timestamp: time_ms + 2000, message: '{"cloudwatch":"logs2"}'},
|
160
|
+
])
|
161
|
+
|
162
|
+
new_log_stream("testprefix")
|
163
|
+
create_log_stream
|
164
|
+
put_log_events([
|
165
|
+
{timestamp: time_ms + 3000, message: '{"cloudwatch":"logs3"}'},
|
166
|
+
{timestamp: time_ms + 4000, message: '{"cloudwatch":"logs4"}'},
|
167
|
+
])
|
168
|
+
|
169
|
+
sleep 5
|
170
|
+
|
171
|
+
d = create_driver(<<-EOC)
|
172
|
+
tag test
|
173
|
+
@type cloudwatch_logs
|
174
|
+
log_group_name #{log_group_name}
|
175
|
+
log_stream_name testprefix
|
176
|
+
use_log_stream_name_prefix true
|
177
|
+
state_file /tmp/state
|
178
|
+
#{aws_key_id}
|
179
|
+
#{aws_sec_key}
|
180
|
+
#{region}
|
181
|
+
#{endpoint}
|
182
|
+
EOC
|
183
|
+
d.run(expect_emits: 4, timeout: 5)
|
184
|
+
|
185
|
+
emits = d.events
|
186
|
+
assert_equal(4, emits.size)
|
187
|
+
assert_true(emits.include? ['test', ((time_ms + 1000) / 1000).floor, {'cloudwatch' => 'logs1'}])
|
188
|
+
assert_true(emits.include? ['test', ((time_ms + 2000) / 1000).floor, {'cloudwatch' => 'logs2'}])
|
189
|
+
assert_true(emits.include? ['test', ((time_ms + 3000) / 1000).floor, {'cloudwatch' => 'logs3'}])
|
190
|
+
assert_true(emits.include? ['test', ((time_ms + 4000) / 1000).floor, {'cloudwatch' => 'logs4'}])
|
191
|
+
end
|
192
|
+
|
193
|
+
def test_emit_with_todays_log_stream
|
194
|
+
new_log_stream("testprefix")
|
195
|
+
create_log_stream
|
196
|
+
|
197
|
+
today = DateTime.now.strftime("%Y/%m/%d")
|
198
|
+
yesterday = (Date.today - 1).strftime("%Y/%m/%d")
|
199
|
+
tomorrow = (Date.today + 1).strftime("%Y/%m/%d")
|
200
|
+
|
201
|
+
|
202
|
+
time_ms = (Time.now.to_f * 1000).floor
|
203
|
+
put_log_events([
|
204
|
+
{timestamp: time_ms + 1000, message: '{"cloudwatch":"logs1"}'},
|
205
|
+
{timestamp: time_ms + 2000, message: '{"cloudwatch":"logs2"}'},
|
206
|
+
])
|
207
|
+
|
208
|
+
new_log_stream(today)
|
209
|
+
create_log_stream
|
210
|
+
put_log_events([
|
211
|
+
{timestamp: time_ms + 3000, message: '{"cloudwatch":"logs3"}'},
|
212
|
+
{timestamp: time_ms + 4000, message: '{"cloudwatch":"logs4"}'},
|
213
|
+
])
|
214
|
+
|
215
|
+
new_log_stream(yesterday)
|
216
|
+
create_log_stream
|
217
|
+
put_log_events([
|
218
|
+
{timestamp: time_ms + 5000, message: '{"cloudwatch":"logs5"}'},
|
219
|
+
{timestamp: time_ms + 6000, message: '{"cloudwatch":"logs6"}'},
|
220
|
+
])
|
221
|
+
|
222
|
+
new_log_stream(tomorrow)
|
223
|
+
create_log_stream
|
224
|
+
put_log_events([
|
225
|
+
{timestamp: time_ms + 7000, message: '{"cloudwatch":"logs7"}'},
|
226
|
+
{timestamp: time_ms + 8000, message: '{"cloudwatch":"logs8"}'},
|
227
|
+
])
|
228
|
+
|
229
|
+
new_log_stream(today)
|
230
|
+
create_log_stream
|
231
|
+
put_log_events([
|
232
|
+
{timestamp: time_ms + 9000, message: '{"cloudwatch":"logs9"}'},
|
233
|
+
{timestamp: time_ms + 10000, message: '{"cloudwatch":"logs10"}'},
|
234
|
+
])
|
235
|
+
|
236
|
+
new_log_stream(yesterday)
|
237
|
+
create_log_stream
|
238
|
+
put_log_events([
|
239
|
+
{timestamp: time_ms + 11000, message: '{"cloudwatch":"logs11"}'},
|
240
|
+
{timestamp: time_ms + 12000, message: '{"cloudwatch":"logs12"}'},
|
241
|
+
])
|
242
|
+
|
243
|
+
sleep 15
|
244
|
+
|
245
|
+
d = create_driver(<<-EOC)
|
246
|
+
tag test
|
247
|
+
@type cloudwatch_logs
|
248
|
+
log_group_name #{log_group_name}
|
249
|
+
use_todays_log_stream true
|
250
|
+
state_file /tmp/state
|
251
|
+
#{aws_key_id}
|
252
|
+
#{aws_sec_key}
|
253
|
+
#{region}
|
254
|
+
#{endpoint}
|
255
|
+
EOC
|
256
|
+
d.run(expect_emits: 8, timeout: 15)
|
257
|
+
|
258
|
+
emits = d.events
|
259
|
+
assert_equal(8, emits.size)
|
260
|
+
assert_false(emits.include? ['test', ((time_ms + 1000) / 1000).floor, {'cloudwatch' => 'logs1'}])
|
261
|
+
assert_false(emits.include? ['test', ((time_ms + 2000) / 1000).floor, {'cloudwatch' => 'logs2'}])
|
262
|
+
assert_true(emits.include? ['test', ((time_ms + 3000) / 1000).floor, {'cloudwatch' => 'logs3'}])
|
263
|
+
assert_true(emits.include? ['test', ((time_ms + 4000) / 1000).floor, {'cloudwatch' => 'logs4'}])
|
264
|
+
assert_true(emits.include? ['test', ((time_ms + 5000) / 1000).floor, {'cloudwatch' => 'logs5'}])
|
265
|
+
assert_true(emits.include? ['test', ((time_ms + 6000) / 1000).floor, {'cloudwatch' => 'logs6'}])
|
266
|
+
assert_false(emits.include? ['test', ((time_ms + 7000) / 1000).floor, {'cloudwatch' => 'logs7'}])
|
267
|
+
assert_false(emits.include? ['test', ((time_ms + 8000) / 1000).floor, {'cloudwatch' => 'logs8'}])
|
268
|
+
assert_true(emits.include? ['test', ((time_ms + 9000) / 1000).floor, {'cloudwatch' => 'logs9'}])
|
269
|
+
assert_true(emits.include? ['test', ((time_ms + 10000) / 1000).floor, {'cloudwatch' => 'logs10'}])
|
270
|
+
assert_true(emits.include? ['test', ((time_ms + 11000) / 1000).floor, {'cloudwatch' => 'logs11'}])
|
271
|
+
assert_true(emits.include? ['test', ((time_ms + 12000) / 1000).floor, {'cloudwatch' => 'logs12'}])
|
272
|
+
end
|
273
|
+
end
|
274
|
+
|
275
|
+
sub_test_case "stub responses" do
|
276
|
+
setup do
|
277
|
+
@client = Aws::CloudWatchLogs::Client.new(stub_responses: true)
|
278
|
+
mock(Aws::CloudWatchLogs::Client).new(anything) do
|
279
|
+
@client
|
280
|
+
end
|
281
|
+
end
|
282
|
+
|
283
|
+
test "emit" do
|
284
|
+
time_ms = (Time.now.to_f * 1000).floor
|
285
|
+
log_stream = Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "stream_name")
|
286
|
+
@client.stub_responses(:describe_log_streams, { log_streams: [log_stream], next_token: nil })
|
287
|
+
cloudwatch_logs_events = [
|
288
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: { cloudwatch: "logs1" }.to_json, ingestion_time: time_ms),
|
289
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: { cloudwatch: "logs2" }.to_json, ingestion_time: time_ms)
|
290
|
+
]
|
291
|
+
@client.stub_responses(:get_log_events, { events: cloudwatch_logs_events, next_forward_token: nil })
|
292
|
+
|
293
|
+
d = create_driver
|
294
|
+
d.run(expect_emits: 2, timeout: 5)
|
295
|
+
|
296
|
+
events = d.events
|
297
|
+
assert_equal(2, events.size)
|
298
|
+
assert_equal(["test", (time_ms / 1000), { "cloudwatch" => "logs1" }], events[0])
|
299
|
+
assert_equal(["test", (time_ms / 1000), { "cloudwatch" => "logs2" }], events[1])
|
300
|
+
end
|
301
|
+
|
302
|
+
test "emit with aws_timestamp" do
|
303
|
+
time_ms = (Time.now.to_f * 1000).floor
|
304
|
+
log_time_ms = time_ms - 10000
|
305
|
+
log_time_str = Time.at(log_time_ms / 1000.floor).to_s
|
306
|
+
log_stream = Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "stream_name")
|
307
|
+
@client.stub_responses(:describe_log_streams, { log_streams: [log_stream], next_token: nil })
|
308
|
+
cloudwatch_logs_events = [
|
309
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: "#{log_time_str},Cloudwatch non json logs1"),
|
310
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: "#{log_time_str},Cloudwatch non json logs2")
|
311
|
+
]
|
312
|
+
@client.stub_responses(:get_log_events, { events: cloudwatch_logs_events, next_forward_token: nil })
|
313
|
+
|
314
|
+
d = create_driver(csv_format_config_aws_timestamp)
|
315
|
+
d.run(expect_emits: 2, timeout: 5)
|
316
|
+
|
317
|
+
events = d.events
|
318
|
+
assert_equal(2, events.size)
|
319
|
+
assert_equal(["test", (time_ms / 1000).floor, { "message" => "Cloudwatch non json logs1" }], events[0])
|
320
|
+
assert_equal(["test", (time_ms / 1000).floor, { "message" => "Cloudwatch non json logs2" }], events[1])
|
321
|
+
end
|
322
|
+
|
323
|
+
test "emit with log_timestamp" do
|
324
|
+
time_ms = (Time.now.to_f * 1000).floor
|
325
|
+
log_time_ms = time_ms - 10000
|
326
|
+
log_time_str = Time.at(log_time_ms / 1000.floor).to_s
|
327
|
+
log_stream = Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "stream_name")
|
328
|
+
@client.stub_responses(:describe_log_streams, { log_streams: [log_stream], next_token: nil })
|
329
|
+
cloudwatch_logs_events = [
|
330
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: "#{log_time_str},Cloudwatch non json logs1"),
|
331
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: "#{log_time_str},Cloudwatch non json logs2")
|
332
|
+
]
|
333
|
+
@client.stub_responses(:get_log_events, { events: cloudwatch_logs_events, next_forward_token: nil })
|
334
|
+
|
335
|
+
d = create_driver(csv_format_config)
|
336
|
+
d.run(expect_emits: 2, timeout: 5)
|
337
|
+
|
338
|
+
emits = d.events
|
339
|
+
assert_equal(2, emits.size)
|
340
|
+
assert_equal(["test", (log_time_ms / 1000).floor, { "message" => "Cloudwatch non json logs1" }], emits[0])
|
341
|
+
assert_equal(["test", (log_time_ms / 1000).floor, { "message" => "Cloudwatch non json logs2" }], emits[1])
|
342
|
+
end
|
343
|
+
|
344
|
+
test "emit with format" do
|
345
|
+
config = <<-CONFIG
|
346
|
+
tag test
|
347
|
+
@type cloudwatch_logs
|
348
|
+
log_group_name #{log_group_name}
|
349
|
+
log_stream_name #{log_stream_name}
|
350
|
+
state_file /tmp/state
|
351
|
+
format /^(?<cloudwatch>[^ ]*)?/
|
352
|
+
#{aws_key_id}
|
353
|
+
#{aws_sec_key}
|
354
|
+
#{region}
|
355
|
+
#{endpoint}
|
356
|
+
CONFIG
|
357
|
+
time_ms = (Time.now.to_f * 1000).floor
|
358
|
+
|
359
|
+
log_stream = Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "stream_name")
|
360
|
+
@client.stub_responses(:describe_log_streams, { log_streams: [log_stream], next_token: nil })
|
361
|
+
cloudwatch_logs_events = [
|
362
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: "logs1", ingestion_time: time_ms),
|
363
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms, message: "logs2", ingestion_time: time_ms)
|
364
|
+
]
|
365
|
+
@client.stub_responses(:get_log_events, { events: cloudwatch_logs_events, next_forward_token: nil })
|
366
|
+
|
367
|
+
d = create_driver(config)
|
368
|
+
d.run(expect_emits: 2, timeout: 5)
|
369
|
+
|
370
|
+
events = d.events
|
371
|
+
assert_equal(2, events.size)
|
372
|
+
assert_equal("test", events[0][0])
|
373
|
+
assert_in_delta(time_ms / 1000.0, events[0][1], 1.0)
|
374
|
+
assert_equal({ "cloudwatch" => "logs1" }, events[0][2])
|
375
|
+
assert_equal("test", events[1][0])
|
376
|
+
assert_in_delta(time_ms / 1000.0, events[1][1], 1.0)
|
377
|
+
assert_equal({ "cloudwatch" => "logs2" }, events[1][2])
|
378
|
+
end
|
379
|
+
|
380
|
+
test "emit with prefix" do
|
381
|
+
config = <<-CONFIG
|
382
|
+
tag test
|
383
|
+
@type cloudwatch_logs
|
384
|
+
log_group_name #{log_group_name}
|
385
|
+
log_stream_name testprefix
|
386
|
+
use_log_stream_name_prefix true
|
387
|
+
state_file /tmp/state
|
388
|
+
#{aws_key_id}
|
389
|
+
#{aws_sec_key}
|
390
|
+
#{region}
|
391
|
+
#{endpoint}
|
392
|
+
CONFIG
|
393
|
+
time_ms = (Time.now.to_f * 1000).floor
|
394
|
+
log_stream1 = Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "stream_name")
|
395
|
+
log_stream2 = Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "stream_name")
|
396
|
+
@client.stub_responses(:describe_log_streams, { log_streams: [log_stream1, log_stream2], next_token: nil })
|
397
|
+
cloudwatch_logs_events1 = [
|
398
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms + 1000, message: { cloudwatch: "logs1" }.to_json, ingestion_time: time_ms),
|
399
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms + 2000, message: { cloudwatch: "logs2" }.to_json, ingestion_time: time_ms)
|
400
|
+
]
|
401
|
+
cloudwatch_logs_events2 = [
|
402
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms + 3000, message: { cloudwatch: "logs3" }.to_json, ingestion_time: time_ms),
|
403
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms + 4000, message: { cloudwatch: "logs4" }.to_json, ingestion_time: time_ms)
|
404
|
+
]
|
405
|
+
@client.stub_responses(:get_log_events, [
|
406
|
+
{ events: cloudwatch_logs_events1, next_forward_token: nil },
|
407
|
+
{ events: cloudwatch_logs_events2, next_forward_token: nil },
|
408
|
+
])
|
409
|
+
|
410
|
+
d = create_driver(config)
|
411
|
+
d.run(expect_emits: 4, timeout: 5)
|
412
|
+
|
413
|
+
events = d.events
|
414
|
+
assert_equal(4, events.size)
|
415
|
+
assert_equal(["test", (time_ms + 1000) / 1000, { "cloudwatch" => "logs1" }], events[0])
|
416
|
+
assert_equal(["test", (time_ms + 2000) / 1000, { "cloudwatch" => "logs2" }], events[1])
|
417
|
+
assert_equal(["test", (time_ms + 3000) / 1000, { "cloudwatch" => "logs3" }], events[2])
|
418
|
+
assert_equal(["test", (time_ms + 4000) / 1000, { "cloudwatch" => "logs4" }], events[3])
|
419
|
+
end
|
420
|
+
|
421
|
+
test "emit with today's log stream" do
|
422
|
+
config = <<-CONFIG
|
423
|
+
tag test
|
424
|
+
@type cloudwatch_logs
|
425
|
+
log_group_name #{log_group_name}
|
426
|
+
use_todays_log_stream true
|
427
|
+
state_file /tmp/state
|
428
|
+
fetch_interval 0.1
|
429
|
+
#{aws_key_id}
|
430
|
+
#{aws_sec_key}
|
431
|
+
#{region}
|
432
|
+
#{endpoint}
|
433
|
+
CONFIG
|
434
|
+
|
435
|
+
today = Date.today.strftime("%Y/%m/%d")
|
436
|
+
yesterday = (Date.today - 1).strftime("%Y/%m/%d")
|
437
|
+
time_ms = (Time.now.to_f * 1000).floor
|
438
|
+
|
439
|
+
log_stream = ->(name) { Aws::CloudWatchLogs::Types::LogStream.new(log_stream_name: "#{name}_#{SecureRandom.uuid}") }
|
440
|
+
@client.stub_responses(:describe_log_streams, ->(context) {
|
441
|
+
if context.params[:log_stream_name_prefix].start_with?(today)
|
442
|
+
{ log_streams: [log_stream.call(today)], next_token: nil }
|
443
|
+
elsif context.params[:log_stream_name_prefix].start_with?(yesterday)
|
444
|
+
{ log_streams: [log_stream.call(yesterday)], next_token: nil }
|
445
|
+
else
|
446
|
+
{ log_streams: [], next_token: nil }
|
447
|
+
end
|
448
|
+
})
|
449
|
+
count = 0
|
450
|
+
@client.stub_responses(:get_log_events, ->(context) {
|
451
|
+
n = count * 2 + 1
|
452
|
+
cloudwatch_logs_events = [
|
453
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms + n * 1000, message: { cloudwatch: "logs#{n}" }.to_json, ingestion_time: time_ms),
|
454
|
+
Aws::CloudWatchLogs::Types::OutputLogEvent.new(timestamp: time_ms + (n + 1) * 1000, message: { cloudwatch: "logs#{n + 1}" }.to_json, ingestion_time: time_ms)
|
455
|
+
]
|
456
|
+
count += 1
|
457
|
+
if context.params[:log_stream_name].start_with?(today)
|
458
|
+
{ events: cloudwatch_logs_events, next_forward_token: nil }
|
459
|
+
elsif context.params[:log_stream_name].start_with?(yesterday)
|
460
|
+
{ events: cloudwatch_logs_events, next_forward_token: nil }
|
461
|
+
else
|
462
|
+
flunk("Failed log_stream_name: #{context.params[:log_stream_name]}")
|
463
|
+
end
|
464
|
+
})
|
465
|
+
|
466
|
+
d = create_driver(config)
|
467
|
+
d.run(expect_emits: 8, timeout: 15)
|
468
|
+
|
469
|
+
events = d.events
|
470
|
+
assert_equal(8, events.size)
|
471
|
+
assert_equal(["test", ((time_ms + 1000) / 1000), { "cloudwatch" => "logs1" }], events[0])
|
472
|
+
assert_equal(["test", ((time_ms + 2000) / 1000), { "cloudwatch" => "logs2" }], events[1])
|
473
|
+
assert_equal(["test", ((time_ms + 3000) / 1000), { "cloudwatch" => "logs3" }], events[2])
|
474
|
+
assert_equal(["test", ((time_ms + 4000) / 1000), { "cloudwatch" => "logs4" }], events[3])
|
475
|
+
assert_equal(["test", ((time_ms + 5000) / 1000), { "cloudwatch" => "logs5" }], events[4])
|
476
|
+
assert_equal(["test", ((time_ms + 6000) / 1000), { "cloudwatch" => "logs6" }], events[5])
|
477
|
+
assert_equal(["test", ((time_ms + 7000) / 1000), { "cloudwatch" => "logs7" }], events[6])
|
478
|
+
assert_equal(["test", ((time_ms + 8000) / 1000), { "cloudwatch" => "logs8" }], events[7])
|
479
|
+
end
|
480
|
+
end
|
481
|
+
|
482
|
+
private
|
483
|
+
|
484
|
+
def default_config
|
485
|
+
<<-EOC
|
486
|
+
tag test
|
487
|
+
@type cloudwatch_logs
|
488
|
+
log_group_name #{log_group_name}
|
489
|
+
log_stream_name #{log_stream_name}
|
490
|
+
state_file /tmp/state
|
491
|
+
fetch_interval 1
|
492
|
+
#{aws_key_id}
|
493
|
+
#{aws_sec_key}
|
494
|
+
#{region}
|
495
|
+
#{endpoint}
|
496
|
+
EOC
|
497
|
+
end
|
498
|
+
|
499
|
+
def csv_format_config
|
500
|
+
<<-EOC
|
501
|
+
tag test
|
502
|
+
@type cloudwatch_logs
|
503
|
+
log_group_name #{log_group_name}
|
504
|
+
log_stream_name #{log_stream_name}
|
505
|
+
state_file /tmp/state
|
506
|
+
fetch_interval 1
|
507
|
+
#{aws_key_id}
|
508
|
+
#{aws_sec_key}
|
509
|
+
#{region}
|
510
|
+
#{endpoint}
|
511
|
+
format csv
|
512
|
+
keys time,message
|
513
|
+
time_key time
|
514
|
+
EOC
|
515
|
+
end
|
516
|
+
|
517
|
+
def csv_format_config_aws_timestamp
|
518
|
+
csv_format_config.concat("use_aws_timestamp true")
|
519
|
+
end
|
520
|
+
|
521
|
+
def create_driver(conf = default_config)
|
522
|
+
Fluent::Test::Driver::Input.new(Fluent::Plugin::CloudwatchLogsInput).configure(conf)
|
523
|
+
end
|
524
|
+
end
|