fluent-plugin-datadog-log 0.1.0.rc18 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,278 +0,0 @@
1
- # Copyright 2017 Yusuke KUOKA All rights reserved.
2
- #
3
- # Licensed under the Apache License, Version 2.0 (the "License");
4
- # you may not use this file except in compliance with the License.
5
- # You may obtain a copy of the License at
6
- #
7
- # http://www.apache.org/licenses/LICENSE-2.0
8
- #
9
- # Unless required by applicable law or agreed to in writing, software
10
- # distributed under the License is distributed on an "AS IS" BASIS,
11
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- # See the License for the specific language governing permissions and
13
- # limitations under the License.
14
-
15
- require_relative 'base_test'
16
-
17
- # Unit tests for Datadog Log plugin
18
- class DatadogLogOutputTest < Test::Unit::TestCase
19
- include BaseTest
20
-
21
- def test_configure
22
- new_stub_context do
23
- setup_ec2_metadata_stubs
24
-
25
- d = create_driver(<<-EOC)
26
- type datadog_log
27
- api_key myapikey
28
- service myservice
29
- source mysource
30
- EOC
31
-
32
- assert_equal 'myapikey', d.instance.api_key
33
- assert_equal 'myservice', d.instance.service
34
- assert_equal 'mysource', d.instance.source
35
- end
36
- end
37
-
38
- def test_configure_with_env
39
- new_stub_context do
40
- setup_ec2_metadata_stubs
41
-
42
- ENV.stubs(:[])
43
- .with('DD_API_KEY')
44
- .returns('myapikey_from_env')
45
-
46
- ENV.stubs(:[])
47
- .with(Not equals 'DD_API_KEY')
48
- .returns('')
49
- .times(3)
50
-
51
- d = create_driver(<<-EOC)
52
- type datadog_log
53
- service myservice
54
- source mysource
55
- EOC
56
-
57
- assert_equal 'myapikey_from_env', d.instance.api_key
58
- assert_equal 'myservice', d.instance.service
59
- assert_equal 'mysource', d.instance.source
60
- end
61
- end
62
-
63
- def test_write
64
- new_stub_context do
65
- setup_ec2_metadata_stubs
66
-
67
- timestamp_str = '2006-01-02T15:04:05.000000+00:00'
68
- t = DateTime.rfc3339(timestamp_str).to_time
69
- time = Fluent::EventTime.from_time(t)
70
- d = create_driver(<<-EOC)
71
- type datadog_log
72
- api_key myapikey
73
- service myservice
74
- source mysource
75
- source_category mysourcecategory
76
- logset mylogset
77
- log_level debug
78
- EOC
79
- conn = StubConn.new
80
- fluentd_tag = 'mytag'
81
- Net::TCPClient.stubs(:new)
82
- .with(server: ':10516', ssl: true)
83
- .returns(conn)
84
- d.run(default_tag: fluentd_tag) do
85
- record = {
86
- 'log' => 'mymsg'
87
- }
88
- d.feed(time, record)
89
- end
90
-
91
- # fail d.logs.inspect
92
- assert_equal(1, d.logs.count { |l| l =~ /Sent payload to Datadog/ })
93
- assert_equal(1, conn.sent.size)
94
- # rubocop:disable LineLength
95
- payload = %(myapikey/mylogset <46>0 2006-01-02T15:04:05.000000+00:00 i-81c16767 myservice - - [dd ddsource="mysource"][dd ddsourcecategory="mysourcecategory"][dd ddtags="host=i-81c16767,zone=aws:us-west-2b,aws_account_id=123456789012"] mymsg\n)
96
- # rubocop:enable LineLength
97
- assert_equal(payload, conn.sent.first)
98
- end
99
- end
100
-
101
- def test_write_kube
102
- new_stub_context do
103
- setup_ec2_metadata_stubs
104
-
105
- timestamp_str = '2006-01-02T15:04:05.000000+00:00'
106
- t = DateTime.rfc3339(timestamp_str).to_time
107
- time = Fluent::EventTime.from_time(t)
108
- d = create_driver(<<-EOC)
109
- type datadog_log
110
- api_key myapikey
111
- service myservice
112
- source mysource
113
- source_category mysourcecategory
114
- logset mylogset
115
- log_level debug
116
- tags ["kube_cluster=MyCluster", "mykey=myval"]
117
- EOC
118
- conn = StubConn.new
119
- fluentd_tag = 'mytag'
120
- Net::TCPClient.stubs(:new)
121
- .with(server: ':10516', ssl: true)
122
- .returns(conn)
123
- d.run(default_tag: fluentd_tag) do
124
- record = {
125
- 'log' => 'mymsg',
126
- 'docker' => {
127
- 'container_id' => 'myfullcontainerid'
128
- },
129
- 'kubernetes' => {
130
- 'namespace' => 'myns',
131
- 'pod_name' => 'mypod',
132
- 'container_name' => 'mycontainer',
133
- 'labels' => {
134
- 'k8s-app' => 'myapp'
135
- },
136
- 'annotations' => {
137
- # rubocop:disable LineLength
138
- 'kubernetes.io/created-by' => '{"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"Deployment","namespace":"default","name":"myapp","uid":"d67e8857-c2dc-11e7-aed9-066d23381f8c","apiVersion":"extensions","resourceVersion":"289"}}'
139
- # rubocop:enable LineLength
140
- }
141
- }
142
- }
143
- d.feed(time, record)
144
- end
145
-
146
- # fail d.logs.inspect
147
- assert_equal(1, d.logs.count { |l| l =~ /Sent payload to Datadog/ })
148
- assert_equal(1, conn.sent.size)
149
- # rubocop:disable LineLength
150
- payload = %(myapikey/mylogset <46>0 2006-01-02T15:04:05.000000+00:00 i-81c16767 myapp - - [dd ddsource="mypod"][dd ddsourcecategory="mycontainer"][dd ddtags="pod_name=mypod,container_name=mycontainer,kube_k8s-app=myapp,kube_deployment=myapp,host=i-81c16767,zone=aws:us-west-2b,aws_account_id=123456789012,kube_cluster=MyCluster,mykey=myval"] mymsg\n)
151
- # rubocop:enable LineLength
152
- assert_equal(payload, conn.sent.first)
153
- end
154
- end
155
-
156
- def test_prometheus_metrics
157
- new_stub_context do
158
- setup_ec2_metadata_stubs
159
- timestamp_str = '2006-01-02T15:04:05.000000+00:00'
160
- t = DateTime.rfc3339(timestamp_str).to_time
161
- time = Fluent::EventTime.from_time(t)
162
- [
163
- # Single successful request.
164
- [false, 0, 1, 1, [1, 0, 1, 0, 0]],
165
- # Several successful requests.
166
- [false, 0, 2, 1, [2, 0, 2, 0, 0]]
167
- ].each do |_should_fail, _code, request_count, entry_count, metric_values|
168
- setup_prometheus
169
- (1..request_count).each do
170
- d = create_driver(<<-EOC)
171
- type datadog_log
172
- api_key myapikey
173
- service myservice
174
- source mysource
175
- source_category mysourcecategory
176
- logset mylogset
177
- log_level debug
178
- enable_monitoring true
179
- EOC
180
- conn = StubConn.new
181
- Net::TCPClient.stubs(:new)
182
- .with(server: ':10516', ssl: true)
183
- .returns(conn)
184
- d.run(default_tag: 'mytag') do
185
- (1..entry_count).each do |i|
186
- d.feed time, 'message' => log_entry(i.to_s)
187
- end
188
- end
189
- end
190
- successful_requests_count, failed_requests_count,
191
- ingested_entries_count, dropped_entries_count,
192
- retried_entries_count = metric_values
193
- assert_prometheus_metric_value(:datadog_successful_requests_count,
194
- successful_requests_count)
195
- assert_prometheus_metric_value(:datadog_failed_requests_count,
196
- failed_requests_count)
197
- assert_prometheus_metric_value(:datadog_ingested_entries_count,
198
- ingested_entries_count)
199
- assert_prometheus_metric_value(:datadog_dropped_entries_count,
200
- dropped_entries_count)
201
- assert_prometheus_metric_value(:datadog_retried_entries_count,
202
- retried_entries_count)
203
- end
204
- end
205
- end
206
-
207
- def test_struct_payload_non_utf8_log
208
- # d.emit('msg' => log_entry(0),
209
- # 'normal_key' => "test#{non_utf8_character}non utf8",
210
- # "non_utf8#{non_utf8_character}key" => 5000,
211
- # 'nested_struct' => { "non_utf8#{non_utf8_character}key" => \
212
- # "test#{non_utf8_character}non utf8" },
213
- # 'null_field' => nil)
214
- end
215
-
216
- class StubConn
217
- attr_reader :sent
218
-
219
- def initialize
220
- @sent = []
221
- end
222
-
223
- def write(payload)
224
- @sent << payload
225
- end
226
-
227
- def close
228
- end
229
- end
230
-
231
- private
232
-
233
- # Use the right single quotation mark as the sample non-utf8 character.
234
- def non_utf8_character
235
- [0x92].pack('C*')
236
- end
237
-
238
- # For an optional field with default values, Protobuf omits the field when it
239
- # is deserialized to json. So we need to add an extra check for gRPC which
240
- # uses Protobuf.
241
- #
242
- # An optional block can be passed in if we need to assert something other than
243
- # a plain equal. e.g. assert_in_delta.
244
- def assert_equal_with_default(field, expected_value, default_value, entry)
245
- if expected_value == default_value
246
- assert_nil field
247
- elsif block_given?
248
- yield
249
- else
250
- assert_equal expected_value, field, entry
251
- end
252
- end
253
-
254
- # Get the fields of the payload.
255
- def get_fields(payload)
256
- payload['fields']
257
- end
258
-
259
- # Get the value of a struct field.
260
- def get_struct(field)
261
- field['structValue']
262
- end
263
-
264
- # Get the value of a string field.
265
- def get_string(field)
266
- field['stringValue']
267
- end
268
-
269
- # Get the value of a number field.
270
- def get_number(field)
271
- field['numberValue']
272
- end
273
-
274
- # The null value.
275
- def null_value
276
- { 'nullValue' => 'NULL_VALUE' }
277
- end
278
- end