forked from elastic/logstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.rb
297 lines (256 loc) · 9.8 KB
/
event.rb
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
# encoding: utf-8
require "logstash/event"
require "insist"
describe LogStash::Event do
subject do
LogStash::Event.new(
"@timestamp" => Time.iso8601("2013-01-01T00:00:00.000Z"),
"type" => "sprintf",
"message" => "hello world",
"tags" => [ "tag1" ],
"source" => "/home/foo",
"a" => "b",
"c" => {
"d" => "f",
"e" => {"f" => "g"}
},
"f" => { "g" => { "h" => "i" } },
"j" => {
"k1" => "v",
"k2" => [ "w", "x" ],
"k3" => {"4" => "m"},
5 => 6,
"5" => 7
}
)
end
context "[]=" do
it "should raise an exception if you attempt to set @timestamp to a value type other than a Time object" do
insist { subject["@timestamp"] = "crash!" }.raises(TypeError)
end
it "should assign simple fields" do
insist { subject["foo"] }.nil?
insist { subject["foo"] = "bar"} == "bar"
insist { subject["foo"] } == "bar"
end
it "should overwrite simple fields" do
insist { subject["foo"] }.nil?
insist { subject["foo"] = "bar"} == "bar"
insist { subject["foo"] } == "bar"
insist { subject["foo"] = "baz"} == "baz"
insist { subject["foo"] } == "baz"
end
it "should assign deep fields" do
insist { subject["[foo][bar]"] }.nil?
insist { subject["[foo][bar]"] = "baz"} == "baz"
insist { subject["[foo][bar]"] } == "baz"
end
it "should overwrite deep fields" do
insist { subject["[foo][bar]"] }.nil?
insist { subject["[foo][bar]"] = "baz"} == "baz"
insist { subject["[foo][bar]"] } == "baz"
insist { subject["[foo][bar]"] = "zab"} == "zab"
insist { subject["[foo][bar]"] } == "zab"
end
end
context "#sprintf" do
it "should report a unix timestamp for %{+%s}" do
insist { subject.sprintf("%{+%s}") } == "1356998400"
end
it "should report a time with %{+format} syntax", :if => RUBY_ENGINE == "jruby" do
insist { subject.sprintf("%{+YYYY}") } == "2013"
insist { subject.sprintf("%{+MM}") } == "01"
insist { subject.sprintf("%{+HH}") } == "00"
end
it "should report fields with %{field} syntax" do
insist { subject.sprintf("%{type}") } == "sprintf"
insist { subject.sprintf("%{message}") } == subject["message"]
end
it "should print deep fields" do
insist { subject.sprintf("%{[j][k1]}") } == "v"
insist { subject.sprintf("%{[j][k2][0]}") } == "w"
end
it "should be able to take a non-string for the format" do
insist { subject.sprintf(2) } == "2"
end
end
context "#[]" do
it "should fetch data" do
insist { subject["type"] } == "sprintf"
end
it "should fetch fields" do
insist { subject["a"] } == "b"
insist { subject['c']['d'] } == "f"
end
it "should fetch deep fields" do
insist { subject["[j][k1]"] } == "v"
insist { subject["[c][d]"] } == "f"
insist { subject['[f][g][h]'] } == "i"
insist { subject['[j][k3][4]'] } == "m"
insist { subject['[j][5]'] } == 7
end
it "should be fast?", :performance => true do
count = 1000000
2.times do
start = Time.now
count.times { subject["[j][k1]"] }
duration = Time.now - start
puts "event #[] rate: #{"%02.0f/sec" % (count / duration)}, elapsed: #{duration}s"
end
end
end
context "#overwrite" do
it "should swap data with new content" do
new_event = LogStash::Event.new(
"type" => "new",
"message" => "foo bar",
)
subject.overwrite(new_event)
insist { subject["message"] } == "foo bar"
insist { subject["type"] } == "new"
["tags", "source", "a", "c", "f", "j"].each do |field|
insist { subject[field] } == nil
end
end
end
context "#append" do
it "should append strings to an array" do
subject.append(LogStash::Event.new("message" => "another thing"))
insist { subject["message"] } == [ "hello world", "another thing" ]
end
it "should concatenate tags" do
subject.append(LogStash::Event.new("tags" => [ "tag2" ]))
insist { subject["tags"] } == [ "tag1", "tag2" ]
end
context "when event field is nil" do
it "should add single value as string" do
subject.append(LogStash::Event.new({"field1" => "append1"}))
insist { subject[ "field1" ] } == "append1"
end
it "should add multi values as array" do
subject.append(LogStash::Event.new({"field1" => [ "append1","append2" ]}))
insist { subject[ "field1" ] } == [ "append1","append2" ]
end
end
context "when event field is a string" do
before { subject[ "field1" ] = "original1" }
it "should append string to values, if different from current" do
subject.append(LogStash::Event.new({"field1" => "append1"}))
insist { subject[ "field1" ] } == [ "original1", "append1" ]
end
it "should not change value, if appended value is equal current" do
subject.append(LogStash::Event.new({"field1" => "original1"}))
insist { subject[ "field1" ] } == "original1"
end
it "should concatenate values in an array" do
subject.append(LogStash::Event.new({"field1" => [ "append1" ]}))
insist { subject[ "field1" ] } == [ "original1", "append1" ]
end
it "should join array, removing duplicates" do
subject.append(LogStash::Event.new({"field1" => [ "append1","original1" ]}))
insist { subject[ "field1" ] } == [ "original1", "append1" ]
end
end
context "when event field is an array" do
before { subject[ "field1" ] = [ "original1", "original2" ] }
it "should append string values to array, if not present in array" do
subject.append(LogStash::Event.new({"field1" => "append1"}))
insist { subject[ "field1" ] } == [ "original1", "original2", "append1" ]
end
it "should not append string values, if the array already contains it" do
subject.append(LogStash::Event.new({"field1" => "original1"}))
insist { subject[ "field1" ] } == [ "original1", "original2" ]
end
it "should join array, removing duplicates" do
subject.append(LogStash::Event.new({"field1" => [ "append1","original1" ]}))
insist { subject[ "field1" ] } == [ "original1", "original2", "append1" ]
end
end
end
it "timestamp parsing speed", :performance => true do
warmup = 10000
count = 1000000
data = { "@timestamp" => "2013-12-21T07:25:06.605Z" }
event = LogStash::Event.new(data)
insist { event["@timestamp"] }.is_a?(Time)
duration = 0
[warmup, count].each do |i|
start = Time.now
i.times do
data = { "@timestamp" => "2013-12-21T07:25:06.605Z" }
LogStash::Event.new(data.clone)
end
duration = Time.now - start
end
puts "event @timestamp parse rate: #{"%02.0f/sec" % (count / duration)}, elapsed: #{duration}s"
end
context "acceptable @timestamp formats" do
subject { LogStash::Event.new }
formats = [
"YYYY-MM-dd'T'HH:mm:ss.SSSZ",
"YYYY-MM-dd'T'HH:mm:ss.SSSSSSZ",
"YYYY-MM-dd'T'HH:mm:ss.SSS",
"YYYY-MM-dd'T'HH:mm:ss",
"YYYY-MM-dd'T'HH:mm:ssZ",
]
formats.each do |format|
it "includes #{format}" do
time = subject.sprintf("%{+#{format}}")
begin
LogStash::Event.new("@timestamp" => time)
rescue => e
raise StandardError, "Time '#{time}' was rejected. #{e.class}: #{e.to_s}"
end
end
end
context "from LOGSTASH-1738" do
it "does not error" do
LogStash::Event.new("@timestamp" => "2013-12-29T23:12:52.371240+02:00")
end
end
context "from LOGSTASH-1732" do
it "does not error" do
LogStash::Event.new("@timestamp" => "2013-12-27T11:07:25+00:00")
end
end
end
context "timestamp initialization" do
let(:logger) { double("logger") }
it "should coerce timestamp" do
t = Time.iso8601("2014-06-12T00:12:17.114Z")
expect(LogStash::Timestamp).to receive(:coerce).exactly(3).times.and_call_original
insist{LogStash::Event.new("@timestamp" => t).timestamp.to_i} == t.to_i
insist{LogStash::Event.new("@timestamp" => LogStash::Timestamp.new(t)).timestamp.to_i} == t.to_i
insist{LogStash::Event.new("@timestamp" => "2014-06-12T00:12:17.114Z").timestamp.to_i} == t.to_i
end
it "should assign current time when no timestamp" do
ts = LogStash::Timestamp.now
expect(LogStash::Timestamp).to receive(:now).and_return(ts)
insist{LogStash::Event.new({}).timestamp.to_i} == ts.to_i
end
it "should tag and warn for invalid value" do
ts = LogStash::Timestamp.now
expect(LogStash::Timestamp).to receive(:now).twice.and_return(ts)
expect(Cabin::Channel).to receive(:get).twice.and_return(logger)
expect(logger).to receive(:warn).twice
event = LogStash::Event.new("@timestamp" => :foo)
insist{event.timestamp.to_i} == ts.to_i
insist{event["tags"]} == [LogStash::Event::TIMESTAMP_FAILURE_TAG]
insist{event[LogStash::Event::TIMESTAMP_FAILURE_FIELD]} == :foo
event = LogStash::Event.new("@timestamp" => 666)
insist{event.timestamp.to_i} == ts.to_i
insist{event["tags"]} == [LogStash::Event::TIMESTAMP_FAILURE_TAG]
insist{event[LogStash::Event::TIMESTAMP_FAILURE_FIELD]} == 666
end
it "should tag and warn for invalid string format" do
ts = LogStash::Timestamp.now
expect(LogStash::Timestamp).to receive(:now).and_return(ts)
expect(Cabin::Channel).to receive(:get).and_return(logger)
expect(logger).to receive(:warn)
event = LogStash::Event.new("@timestamp" => "foo")
insist{event.timestamp.to_i} == ts.to_i
insist{event["tags"]} == [LogStash::Event::TIMESTAMP_FAILURE_TAG]
insist{event[LogStash::Event::TIMESTAMP_FAILURE_FIELD]} == "foo"
end
end
end