fluent-plugin-scalyr 0.8.10 → 0.8.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +2 -0
- data/README.md +20 -1
- data/Rakefile +9 -6
- data/VERSION +1 -1
- data/fluent-plugin-scalyr.gemspec +11 -7
- data/lib/fluent/plugin/out_scalyr.rb +185 -215
- data/lib/fluent/plugin/{scalyr-exceptions.rb → scalyr_exceptions.rb} +2 -2
- data/test/helper.rb +12 -6
- data/test/test_config.rb +19 -30
- data/test/test_events.rb +156 -154
- data/test/test_handle_response.rb +34 -35
- data/test/test_ssl_verify.rb +101 -10
- metadata +47 -33
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#
|
2
4
|
# Scalyr Output Plugin for Fluentd
|
3
5
|
#
|
@@ -15,8 +17,6 @@
|
|
15
17
|
# See the License for the specific language governing permissions and
|
16
18
|
# limitations under the License.
|
17
19
|
|
18
|
-
|
19
|
-
|
20
20
|
module Scalyr
|
21
21
|
class ClientError < StandardError; end
|
22
22
|
class Client4xxError < StandardError; end
|
data/test/helper.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#
|
2
4
|
# Scalyr Output Plugin for Fluentd
|
3
5
|
#
|
@@ -15,9 +17,13 @@
|
|
15
17
|
# See the License for the specific language governing permissions and
|
16
18
|
# limitations under the License.
|
17
19
|
|
20
|
+
require "fluent/test"
|
21
|
+
require "fluent/test/helpers"
|
22
|
+
require "fluent/test/log"
|
23
|
+
require "fluent/test/driver/output"
|
24
|
+
require "fluent/plugin/out_scalyr"
|
18
25
|
|
19
|
-
|
20
|
-
require 'fluent/plugin/out_scalyr'
|
26
|
+
include Fluent::Test::Helpers # rubocop:disable Style/MixinUsage
|
21
27
|
|
22
28
|
module Scalyr
|
23
29
|
class ScalyrOutTest < Test::Unit::TestCase
|
@@ -25,13 +31,13 @@ module Scalyr
|
|
25
31
|
Fluent::Test.setup
|
26
32
|
end
|
27
33
|
|
28
|
-
CONFIG = %
|
34
|
+
CONFIG = %(
|
29
35
|
api_write_token test_token
|
30
36
|
ssl_ca_bundle_path /etc/ssl/certs/ca-certificates.crt
|
31
|
-
|
37
|
+
)
|
32
38
|
|
33
|
-
def create_driver(
|
34
|
-
Fluent::Test::
|
39
|
+
def create_driver(conf=CONFIG)
|
40
|
+
Fluent::Test::Driver::Output.new(Scalyr::ScalyrOut).configure(conf)
|
35
41
|
end
|
36
42
|
end
|
37
43
|
end
|
data/test/test_config.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#
|
2
4
|
# Scalyr Output Plugin for Fluentd
|
3
5
|
#
|
@@ -15,58 +17,45 @@
|
|
15
17
|
# See the License for the specific language governing permissions and
|
16
18
|
# limitations under the License.
|
17
19
|
|
20
|
+
require "helper"
|
21
|
+
require "socket"
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
class ConfigTest < Scalyr::ScalyrOutTest
|
23
|
-
|
24
|
-
def test_default_params
|
25
|
-
d = create_driver
|
26
|
-
hostname = Socket.gethostname
|
27
|
-
assert_not_nil( d.instance.server_attributes, "Default server_attributes should not be nil" )
|
28
|
-
assert_equal( hostname, d.instance.server_attributes['serverHost'], "Default serverHost is not hostname" )
|
29
|
-
assert( d.instance.ssl_verify_peer, "Default ssl_verify_peer should be true" )
|
30
|
-
|
31
|
-
#check default buffer limits because they are set outside of the config_set_default
|
32
|
-
assert_equal( 100*1024, d.instance.buffer.buffer_chunk_limit, "Buffer chunk limit should be 100k" )
|
33
|
-
assert_equal( 1024, d.instance.buffer.buffer_queue_limit, "Buffer queue limit should be 1024" )
|
34
|
-
end
|
35
|
-
|
23
|
+
# rubocop:disable Layout/LineLength
|
24
|
+
class ConfigTest < Scalyr::ScalyrOutTest
|
36
25
|
def test_custom_serverhost_not_overwritten
|
37
26
|
hostname = "customHost"
|
38
27
|
d = create_driver CONFIG + "server_attributes { \"serverHost\":\"#{hostname}\" }\nuse_hostname_for_serverhost true"
|
39
|
-
assert_equal(
|
28
|
+
assert_equal(hostname, d.instance.server_attributes["serverHost"], "Custom serverHost should not be overwritten")
|
40
29
|
end
|
41
30
|
|
42
31
|
def test_configure_use_hostname_for_serverhost
|
43
|
-
d = create_driver CONFIG +
|
44
|
-
assert_nil(
|
32
|
+
d = create_driver CONFIG + "use_hostname_for_serverhost false"
|
33
|
+
assert_nil(d.instance.server_attributes, "Default server_attributes should be nil")
|
45
34
|
end
|
46
35
|
|
47
36
|
def test_configure_ssl_verify_peer
|
48
|
-
d = create_driver CONFIG +
|
49
|
-
assert(
|
37
|
+
d = create_driver CONFIG + "ssl_verify_peer false"
|
38
|
+
assert(!d.instance.ssl_verify_peer, "Config failed to set ssl_verify_peer")
|
50
39
|
end
|
51
40
|
|
52
41
|
def test_scalyr_server_adding_trailing_slash
|
53
|
-
d = create_driver CONFIG +
|
54
|
-
assert_equal(
|
42
|
+
d = create_driver CONFIG + "scalyr_server http://www.example.com"
|
43
|
+
assert_equal("http://www.example.com/", d.instance.scalyr_server, "Missing trailing slash for scalyr_server")
|
55
44
|
end
|
56
45
|
|
57
46
|
def test_configure_ssl_ca_bundle_path
|
58
|
-
d = create_driver CONFIG +
|
59
|
-
assert_equal(
|
47
|
+
d = create_driver CONFIG + "ssl_ca_bundle_path /test/ca-bundle.crt"
|
48
|
+
assert_equal("/test/ca-bundle.crt", d.instance.ssl_ca_bundle_path, "Config failed to set ssl_ca_bundle_path")
|
60
49
|
end
|
61
50
|
|
62
51
|
def test_configure_ssl_verify_depth
|
63
|
-
d = create_driver CONFIG +
|
64
|
-
assert_equal(
|
52
|
+
d = create_driver CONFIG + "ssl_verify_depth 10"
|
53
|
+
assert_equal(10, d.instance.ssl_verify_depth, "Config failed to set ssl_verify_depth")
|
65
54
|
end
|
66
55
|
|
67
56
|
def test_configure_server_attributes
|
68
57
|
d = create_driver CONFIG + 'server_attributes { "test":"value" }'
|
69
|
-
assert_equal(
|
58
|
+
assert_equal("value", d.instance.server_attributes["test"], "Config failed to set server_attributes")
|
70
59
|
end
|
71
60
|
end
|
72
|
-
|
61
|
+
# rubocop:enable Layout/LineLength
|
data/test/test_events.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
#
|
2
4
|
# Scalyr Output Plugin for Fluentd
|
3
5
|
#
|
@@ -15,234 +17,234 @@
|
|
15
17
|
# See the License for the specific language governing permissions and
|
16
18
|
# limitations under the License.
|
17
19
|
|
18
|
-
|
19
|
-
require
|
20
|
-
require
|
20
|
+
require "helper"
|
21
|
+
require "flexmock/test_unit"
|
22
|
+
require "fluent/event"
|
21
23
|
|
22
24
|
class EventsTest < Scalyr::ScalyrOutTest
|
23
|
-
|
24
25
|
def test_format
|
25
26
|
d = create_driver
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
d.run
|
27
|
+
|
28
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
29
|
+
|
30
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
31
|
+
mock = flexmock(d.instance)
|
32
|
+
mock.should_receive(:post_request).with_any_args.and_return(response)
|
33
|
+
|
34
|
+
d.run(default_tag: "test") do
|
35
|
+
d.feed(time, {"a" => 1})
|
36
|
+
end
|
37
|
+
|
38
|
+
expected = [
|
39
|
+
["test", time.sec, time.nsec, {"a" => 1}].to_msgpack
|
40
|
+
]
|
41
|
+
|
42
|
+
assert_equal(expected, d.formatted)
|
34
43
|
end
|
35
44
|
|
36
45
|
def test_build_add_events_body_basic_values
|
37
46
|
d = create_driver
|
38
|
-
response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
|
39
|
-
mock = flexmock( d.instance )
|
40
47
|
|
41
|
-
time =
|
42
|
-
attrs = {
|
43
|
-
|
48
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
49
|
+
attrs = {"a" => 1}
|
50
|
+
attrs["logfile"] = "/fluentd/test"
|
44
51
|
|
45
|
-
|
52
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
53
|
+
mock = flexmock(d.instance)
|
46
54
|
|
47
|
-
|
55
|
+
mock_called = false
|
56
|
+
|
57
|
+
mock.should_receive(:post_request).with(
|
48
58
|
URI,
|
49
|
-
on {
|
50
|
-
body = JSON.parse(
|
51
|
-
assert(
|
52
|
-
assert(
|
53
|
-
assert(
|
54
|
-
assert(
|
55
|
-
assert(
|
56
|
-
|
57
|
-
assert_equal(
|
58
|
-
|
59
|
-
assert_equal(
|
60
|
-
true
|
59
|
+
on {|request_body|
|
60
|
+
body = JSON.parse(request_body)
|
61
|
+
assert(body.key?("token"), "Missing token field")
|
62
|
+
assert(body.key?("client_timestamp"), "Missing client_timestamp field")
|
63
|
+
assert(body.key?("sessionInfo"), "sessionInfo field set, but no sessionInfo")
|
64
|
+
assert(body.key?("events"), "missing events field")
|
65
|
+
assert(body.key?("threads"), "missing threads field")
|
66
|
+
assert_equal(1, body["events"].length, "Only expecting 1 event")
|
67
|
+
assert_equal(time.sec * 1_000_000_000, body["events"][0]["ts"].to_i,
|
68
|
+
"Event timestamp differs")
|
69
|
+
assert_equal(attrs, body["events"][0]["attrs"], "Value of attrs differs from log")
|
70
|
+
mock_called = true
|
61
71
|
}
|
62
|
-
|
72
|
+
).once.and_return(response)
|
63
73
|
|
64
|
-
d.run
|
74
|
+
d.run(default_tag: "test") do
|
75
|
+
d.feed(time, attrs)
|
76
|
+
end
|
77
|
+
|
78
|
+
assert_equal(mock_called, true, "mock method was never called!")
|
65
79
|
end
|
66
80
|
|
67
81
|
def test_build_add_events_body_dont_override_logfile_field
|
68
82
|
d = create_driver
|
69
|
-
response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
|
70
|
-
mock = flexmock( d.instance )
|
71
83
|
|
72
|
-
time =
|
73
|
-
attrs = {
|
74
|
-
attrs["logfile"] = "/some/log/file"
|
75
|
-
|
84
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
85
|
+
attrs = {"a" => 1}
|
86
|
+
attrs["logfile"] = "/some/log/file"
|
87
|
+
|
88
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
89
|
+
mock = flexmock(d.instance)
|
90
|
+
|
91
|
+
mock_called = false
|
76
92
|
|
77
|
-
mock.should_receive(
|
93
|
+
mock.should_receive(:post_request).with(
|
78
94
|
URI,
|
79
|
-
on {
|
80
|
-
body = JSON.parse(
|
81
|
-
assert_equal(
|
95
|
+
on {|request_body|
|
96
|
+
body = JSON.parse(request_body)
|
97
|
+
assert_equal(attrs, body["events"][0]["attrs"], "Value of attrs differs from log")
|
98
|
+
mock_called = true
|
82
99
|
true
|
83
100
|
}
|
84
|
-
|
101
|
+
).once.and_return(response)
|
85
102
|
|
86
|
-
d.run
|
103
|
+
d.run(default_tag: "test") do
|
104
|
+
d.feed(time, attrs)
|
105
|
+
end
|
106
|
+
|
107
|
+
assert_equal(mock_called, true, "mock method was never called!")
|
87
108
|
end
|
88
109
|
|
89
110
|
def test_build_add_events_body_with_server_attributes
|
90
111
|
d = create_driver CONFIG + 'server_attributes { "test":"value" }'
|
91
112
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
time = Time.parse("2015-04-01 10:00:00 UTC").to_i
|
96
|
-
attrs = { "a" => 1 }
|
97
|
-
d.emit( attrs, time )
|
98
|
-
|
99
|
-
mock.should_receive( :post_request ).with(
|
100
|
-
URI,
|
101
|
-
on { |request_body|
|
102
|
-
body = JSON.parse( request_body )
|
103
|
-
assert( body.key?( "sessionInfo"), "sessionInfo field set, but no sessionInfo" )
|
104
|
-
assert_equal( "value", body["sessionInfo"]["test"] )
|
105
|
-
true
|
106
|
-
}
|
107
|
-
).and_return( response )
|
108
|
-
|
109
|
-
d.run
|
110
|
-
end
|
111
|
-
|
112
|
-
def test_build_add_events_body_incrementing_timestamps
|
113
|
-
d = create_driver
|
114
|
-
response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
|
115
|
-
mock = flexmock( d.instance )
|
113
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
114
|
+
attrs = {"a" => 1}
|
116
115
|
|
117
|
-
|
118
|
-
d.
|
119
|
-
d.emit( { "a" => 2 }, time )
|
116
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
117
|
+
mock = flexmock(d.instance)
|
120
118
|
|
121
|
-
|
122
|
-
d.emit( { "a" => 3 }, time )
|
119
|
+
mock_called = false
|
123
120
|
|
124
|
-
mock.should_receive(
|
121
|
+
mock.should_receive(:post_request).with(
|
125
122
|
URI,
|
126
|
-
on {
|
127
|
-
body = JSON.parse(
|
128
|
-
|
129
|
-
assert_equal(
|
130
|
-
|
131
|
-
assert events[1]['ts'].to_i > events[0]['ts'].to_i, "Event timestamps must increase"
|
132
|
-
|
133
|
-
#test earlier timestamps are increased
|
134
|
-
assert events[2]['ts'].to_i > events[1]['ts'].to_i, "Event timestamps must increase"
|
135
|
-
|
123
|
+
on {|request_body|
|
124
|
+
body = JSON.parse(request_body)
|
125
|
+
assert(body.key?("sessionInfo"), "sessionInfo field set, but no sessionInfo")
|
126
|
+
assert_equal("value", body["sessionInfo"]["test"])
|
127
|
+
mock_called = true
|
136
128
|
true
|
137
129
|
}
|
138
|
-
|
130
|
+
).once.and_return(response)
|
131
|
+
|
132
|
+
d.run(default_tag: "test") do
|
133
|
+
d.feed(time, attrs)
|
134
|
+
end
|
139
135
|
|
140
|
-
|
136
|
+
assert_equal(mock_called, true, "mock method was never called!")
|
141
137
|
end
|
142
138
|
|
143
|
-
def
|
139
|
+
def test_build_add_events_body_incrementing_timestamps
|
144
140
|
d = create_driver
|
145
|
-
response = flexmock( Net::HTTPResponse, :code => '200', :body =>'{ "status":"success" }' )
|
146
|
-
mock = flexmock( d.instance )
|
147
|
-
|
148
|
-
entries = []
|
149
141
|
|
150
|
-
|
151
|
-
|
142
|
+
time1 = event_time("2015-04-01 10:00:00 UTC")
|
143
|
+
time2 = event_time("2015-04-01 09:59:00 UTC")
|
152
144
|
|
153
|
-
|
154
|
-
|
145
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
146
|
+
mock = flexmock(d.instance)
|
155
147
|
|
156
|
-
|
157
|
-
chunk << buffer
|
148
|
+
mock_called = false
|
158
149
|
|
159
|
-
|
160
|
-
chunk << buffer
|
161
|
-
|
162
|
-
mock.should_receive( :post_request ).with(
|
150
|
+
mock.should_receive(:post_request).with(
|
163
151
|
URI,
|
164
|
-
on {
|
165
|
-
body = JSON.parse(
|
166
|
-
events = body[
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
assert_equal( events[1]['thread'], threads[1]['id'].to_s, "thread id should match event thread id" )
|
152
|
+
on {|request_body|
|
153
|
+
body = JSON.parse(request_body)
|
154
|
+
events = body["events"]
|
155
|
+
assert_equal(3, events.length, "Expecting 3 events")
|
156
|
+
# Since 0.8.10 timestamps dont need to increase anymore
|
157
|
+
assert events[1]["ts"].to_i == events[0]["ts"].to_i, "Event timestamps must be the same"
|
158
|
+
assert events[2]["ts"].to_i < events[0]["ts"].to_i, "Event timestamps must be less"
|
159
|
+
mock_called = true
|
173
160
|
true
|
174
161
|
}
|
175
|
-
|
162
|
+
).once.and_return(response)
|
163
|
+
|
164
|
+
d.run(default_tag: "test") do
|
165
|
+
d.feed(time1, {"a" => 1})
|
166
|
+
d.feed(time1, {"a" => 2})
|
167
|
+
d.feed(time2, {"a" => 3})
|
168
|
+
end
|
176
169
|
|
177
|
-
|
178
|
-
d.instance.write( chunk )
|
179
|
-
d.instance.shutdown
|
170
|
+
assert_equal(mock_called, true, "mock method was never called!")
|
180
171
|
end
|
181
172
|
|
182
173
|
def test_default_message_field
|
183
174
|
d = create_driver CONFIG
|
184
175
|
|
185
|
-
|
186
|
-
|
176
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
177
|
+
attrs = {"log" => "this is a test", "logfile" => "/fluentd/test"}
|
178
|
+
|
179
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
180
|
+
mock = flexmock(d.instance)
|
187
181
|
|
188
|
-
|
189
|
-
attrs = { "log" => "this is a test", "logfile" => "/fluentd/test" }
|
190
|
-
d.emit( attrs, time )
|
182
|
+
mock_called = false
|
191
183
|
|
192
|
-
mock.should_receive(
|
184
|
+
mock.should_receive(:post_request).with(
|
193
185
|
URI,
|
194
|
-
on {
|
195
|
-
body = JSON.parse(
|
196
|
-
|
197
|
-
|
186
|
+
on {|request_body|
|
187
|
+
body = JSON.parse(request_body)
|
188
|
+
assert_equal(attrs, body["events"][0]["attrs"], "Value of attrs differs from log")
|
189
|
+
mock_called = true
|
198
190
|
true
|
199
191
|
}
|
200
|
-
|
192
|
+
).once.and_return(response)
|
201
193
|
|
202
|
-
d.run
|
194
|
+
d.run(default_tag: "test") do
|
195
|
+
d.feed(time, attrs)
|
196
|
+
end
|
197
|
+
|
198
|
+
assert_equal(mock_called, true, "mock method was never called!")
|
203
199
|
end
|
204
200
|
|
205
201
|
def test_different_message_field
|
206
|
-
d = create_driver CONFIG +
|
202
|
+
d = create_driver CONFIG + "message_field log"
|
203
|
+
|
204
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
205
|
+
attrs = {"log" => "this is a test"}
|
207
206
|
|
208
|
-
response = flexmock(
|
209
|
-
mock = flexmock(
|
207
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
208
|
+
mock = flexmock(d.instance)
|
210
209
|
|
211
|
-
|
212
|
-
attrs = { "log" => "this is a test" }
|
213
|
-
d.emit( attrs, time )
|
210
|
+
mock_called = false
|
214
211
|
|
215
|
-
mock.should_receive(
|
212
|
+
mock.should_receive(:post_request).with(
|
216
213
|
URI,
|
217
|
-
on {
|
218
|
-
body = JSON.parse(
|
219
|
-
events = body[
|
220
|
-
assert(
|
221
|
-
assert_equal(
|
222
|
-
assert(
|
214
|
+
on {|request_body|
|
215
|
+
body = JSON.parse(request_body)
|
216
|
+
events = body["events"]
|
217
|
+
assert(events[0]["attrs"].key?("message"), "'message' field not found in event")
|
218
|
+
assert_equal("this is a test", events[0]["attrs"]["message"], "'message' field incorrect")
|
219
|
+
assert(!events[0]["attrs"].key?("log"), "'log' field should no longer exist in event")
|
220
|
+
mock_called = true
|
223
221
|
true
|
224
222
|
}
|
225
|
-
|
223
|
+
).once.and_return(response)
|
226
224
|
|
227
|
-
d.run
|
225
|
+
d.run(default_tag: "test") do
|
226
|
+
d.feed(time, attrs)
|
227
|
+
end
|
228
|
+
|
229
|
+
assert_equal(mock_called, true, "mock method was never called!")
|
228
230
|
end
|
229
231
|
|
230
232
|
def test_different_message_field_message_already_exists
|
231
|
-
d = create_driver CONFIG +
|
233
|
+
d = create_driver CONFIG + "message_field log"
|
232
234
|
|
233
|
-
|
234
|
-
|
235
|
-
mock.should_receive( :post_request ).and_return( response )
|
235
|
+
time = event_time("2015-04-01 10:00:00 UTC")
|
236
|
+
attrs = {"log" => "this is a test", "message" => "uh oh"}
|
236
237
|
|
237
|
-
|
238
|
-
|
239
|
-
d.emit( attrs, time )
|
238
|
+
response = flexmock(Net::HTTPResponse, code: "200", body: '{ "status":"success" }')
|
239
|
+
mock = flexmock(d.instance)
|
240
240
|
|
241
|
-
|
242
|
-
logger.should_receive( :warn ).with( /overwriting log record field 'message'/i ).at_least().once()
|
241
|
+
mock.should_receive(:post_request).once.and_return(response)
|
243
242
|
|
244
|
-
|
245
|
-
|
243
|
+
logger = flexmock($log)
|
244
|
+
logger.should_receive(:warn).with(/overwriting log record field 'message'/i).at_least.once
|
246
245
|
|
246
|
+
d.run(default_tag: "test") do
|
247
|
+
d.feed(time, attrs)
|
248
|
+
end
|
249
|
+
end
|
247
250
|
end
|
248
|
-
|