/
HttpEventCollector_Log4j2Test.java
485 lines (402 loc) · 20.4 KB
/
HttpEventCollector_Log4j2Test.java
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
* Copyright 2013-2014 Splunk, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"): you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.splunk.logging.HttpEventCollectorErrorHandler;
import com.splunk.logging.HttpEventCollectorEventInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.junit.Assert;
import org.junit.Test;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
public final class HttpEventCollector_Log4j2Test {
private String httpEventCollectorName = "Log4j2Test";
final List<List<HttpEventCollectorEventInfo>> errors = new ArrayList<>();
final List<Exception> logEx = new ArrayList<>();
/**
* sending a message via httplogging using log4j2 to splunk
*/
@Test
public void canSendEventUsingLog4j2() throws Exception {
TestUtil.enableHttpEventCollector();
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "splunkLogger4j2";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
org.apache.logging.log4j.core.LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
//use httplogger
List<String> msgs = new ArrayList<>();
Date date = new Date();
String jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for log4j2}", date);
Logger logger = context.getLogger(loggerName);
logger.info(jsonMsg);
msgs.add(jsonMsg);
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test error for log4j2}", date);
logger.error(jsonMsg);
msgs.add(jsonMsg);
TestUtil.verifyEventsSentToSplunk(msgs);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
System.out.println("====================== Test pass=========================");
}
/**
* sending a message via httplogging using log4j2 to splunk
*/
@Test
public void canSendExceptionUsingLog4j2() throws Exception {
TestUtil.enableHttpEventCollector();
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "splunkLogger4j2";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
org.apache.logging.log4j.core.LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
//use httplogger
List<String> msgs = new ArrayList<>();
Date date = new Date();
Logger logger = context.getLogger(loggerName);
String jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test error for log4j2}", date);
logger.error(jsonMsg, new RuntimeException("This is RuntimeException"));
msgs.add(jsonMsg);
TestUtil.verifyEventsSentToSplunk(msgs);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
System.out.println("====================== Test pass=========================");
}
/**
* Sending a message via httplogging using log4j2 to Splunk.
*
* @throws Exception
*/
@Test
public void canSendCommentedEventStringUsingLog4j2() throws Exception {
TestUtil.enableHttpEventCollector();
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "splunkLogger4j2WithCommentedEventMessages";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
org.apache.logging.log4j.core.LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
//use httplogger
List<String> messages = new ArrayList<>();
Date date = new Date();
Logger logger = context.getLogger(loggerName);
String commentedEventMessage = "// This is a single line commented string. Dated: " + date.toString();
logger.info(commentedEventMessage);
messages.add(commentedEventMessage);
commentedEventMessage = "/* This is a document type commented string. */ Dated: " + date.toString();
logger.error(commentedEventMessage);
messages.add(commentedEventMessage);
commentedEventMessage = "## This is a normal commented string. Dated: " + date.toString();
logger.warn(commentedEventMessage);
messages.add(commentedEventMessage);
TestUtil.verifyEventsSentToSplunk(messages);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
System.out.println("====================== Test pass=========================");
}
/**
* sending a message via httplogging using log4j2 to splunk and set index, source and sourcetype
*/
@Test
public void canSendEventUsingLog4j2WithOptions() throws Exception {
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "splunkLogger4j2WithOptions";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
userInputs.put("user_index", "main");
userInputs.put("user_host", "host.example.com");
userInputs.put("user_source", "splunktest");
userInputs.put("user_sourcetype", "battlecat");
org.apache.logging.log4j.core.LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
//use httplogger
List<String> msgs = new ArrayList<>();
Date date = new Date();
String jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for log4j2}", date);
Logger logger = context.getLogger(loggerName);
logger.info(jsonMsg);
msgs.add(jsonMsg);
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test error for log4j2}", date);
logger.error(jsonMsg);
msgs.add(jsonMsg);
TestUtil.verifyEventsSentToSplunk(msgs);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
System.out.println("====================== Test pass=========================");
}
/**
* sending a message via httplogging using java.logging with batched_size_count
*/
@Test
public void sendBatchedEventsByCount() throws Exception {
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
//clean out the events cache by setting send events immediately
String loggerName = "splunkLoggerCountCleanCache";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
String jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for java logging}", new Date());
Logger logger = context.getLogger(loggerName);
logger.info(jsonMsg);
loggerName = "splunkBatchLoggerCount";
userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
userInputs.put("user_batch_interval","0");
userInputs.put("user_batch_size_count", "5");
userInputs.put("user_host", "host.example.com");
userInputs.put("user_source", "splunktest_BatchCount");
userInputs.put("user_sourcetype", "battlecat_BatchCount");
context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
logger = context.getLogger(loggerName);
List<String> msgs = new ArrayList<>();
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for java logging1}", new Date());
logger.info(jsonMsg);
msgs.add(jsonMsg);
System.out.println("event 1");
TestUtil.verifyNoEventSentToSplunk(msgs);
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for java logging2}", new Date());
logger.info(jsonMsg);
msgs.add(jsonMsg);
System.out.println("event 2");
TestUtil.verifyNoEventSentToSplunk(msgs);
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for java logging3}", new Date());
logger.info(jsonMsg);
msgs.add(jsonMsg);
System.out.println("event 3");
TestUtil.verifyNoEventSentToSplunk(msgs);
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for java logging4}", new Date());
logger.info(jsonMsg);
msgs.add(jsonMsg);
System.out.println("event 4");
TestUtil.verifyNoEventSentToSplunk(msgs);
Thread.sleep(6000);
TestUtil.verifyNoEventSentToSplunk(msgs);
jsonMsg = String.format("{EventDate:%s, EventMsg:'this is a test event for java logging5}", new Date());
logger.info(jsonMsg);
msgs.add(jsonMsg);
TestUtil.verifyEventsSentToSplunk(msgs);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
}
/**
* sending a message via httplogging using java.logging with batched_size_bytes
*/
@Test
public void sendBatchedEventsByBatchsize() throws Exception {
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "splunkLoggerBatchSize";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
userInputs.put("user_batch_size_bytes", "500");
userInputs.put("user_batch_interval", "20000");
userInputs.put("user_host", "host.example.com");
userInputs.put("user_source", "splunktest_BatchSize");
userInputs.put("user_sourcetype", "battlecat_BatchSize");
LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
Logger logger = context.getLogger(loggerName);
List<String> msgs = new ArrayList<>();
int size = 0;
String jsonMsg = String.format("{EventDate:%s, EventMsg:'test event for log4j size 1}", new Date());
logger.info(jsonMsg);
size += jsonMsg.length();
msgs.add(jsonMsg);
jsonMsg = String.format("{EventDate:%s, EventMsg:'test event for log4j size 2}", new Date());
size += jsonMsg.length();
logger.info(jsonMsg);
msgs.add(jsonMsg);
Thread.sleep(6000);
TestUtil.verifyNoEventSentToSplunk(msgs);
jsonMsg = String.format("{EventDate:%s, EventMsg:'test event for log4j size 3, adding more msg to exceed the maxsize}", new Date());
while (size + jsonMsg.length() < 550) {
jsonMsg = String.format("%saaaaa", jsonMsg);
}
logger.info(jsonMsg);
msgs.add(jsonMsg);
TestUtil.verifyEventsSentToSplunk(msgs);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
}
/**
* error handling
*/
@Test
public void errorHandlingInvalidToken() throws Exception {
errors.clear();
logEx.clear();
//define error callback
HttpEventCollectorErrorHandler.onError((data, ex) -> {
synchronized (errors) {
errors.add(data);
logEx.add(ex);
}
});
//create a token used for httpEventCollector logging, then make it becomes invalid
httpEventCollectorName = "wrongtoken";
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "errorHandlingInvalidTokenLog4j";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
Logger logger = context.getLogger(loggerName);
//disable the token so that it becomes invalid
TestUtil.disableHttpEventCollector(httpEventCollectorName);
Thread.sleep(5000);
String jsonMsg = String.format("{EventDate:%s, EventMsg:'test event disabled token }", new Date());
logger.info(jsonMsg);
Thread.sleep(5000);
//delete the token so that it becomes invalid
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
Thread.sleep(5000);
jsonMsg = String.format("{EventDate:%s, EventMsg:'test event deleted token}", new Date());
logger.info(jsonMsg);
//wait for async process to return the error
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 60 * 1000)/*wait for up to 60s*/ {
if (logEx.size() >= 2)
break;
Thread.sleep(1000);
}
for (List<HttpEventCollectorEventInfo> infos : errors) {
for (HttpEventCollectorEventInfo info : infos) {
System.out.println(info.getMessage());
}
}
System.out.println("======print logEx");
System.out.println(logEx);
System.out.println("======finish print logEx");
// in this case expect a valid http reply with a json error message
HttpEventCollectorErrorHandler.ServerErrorException serverErrorException1 = (HttpEventCollectorErrorHandler.ServerErrorException) logEx.get(0);
Assert.assertEquals("Token disabled", serverErrorException1.getErrorText());
Assert.assertEquals(1, serverErrorException1.getErrorCode());
HttpEventCollectorErrorHandler.ServerErrorException serverErrorException2 = (HttpEventCollectorErrorHandler.ServerErrorException) logEx.get(1);
Assert.assertEquals("Invalid token", serverErrorException2.getErrorText());
Assert.assertEquals(4, serverErrorException2.getErrorCode());
Assert.assertEquals(2, errors.size());
}
/**
* error handling
*/
@Test
public void errorHandlingDisabledHttpEventCollectorEndpoint() throws Exception {
errors.clear();
logEx.clear();
//define error callback
HttpEventCollectorErrorHandler.onError((data, ex) -> {
synchronized (errors) {
errors.add(data);
logEx.add(ex);
}
});
//create a token used for httpEventCollector logging, then make it becomes invalid
httpEventCollectorName = "wrongtoken";
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "errorHandlingDisabledHttpEventCollectorEndpoint";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
Logger logger = context.getLogger(loggerName);
//disable httpEventCollector endpoint
TestUtil.disableHttpEventCollector();
Thread.sleep(1000);
String jsonMsg = String.format("{EventDate:%s, EventMsg:'test event httpEventCollector disabled}", new Date());
logger.info(jsonMsg);
//wait for async process to return the error
long startTime = System.currentTimeMillis();
while (System.currentTimeMillis() - startTime < 60 * 1000)/*wait for up to 60s*/ {
if (logEx.size() >= 1)
break;
Thread.sleep(1000);
}
Assert.assertTrue(errors.size() >= 1);
System.out.println(logEx);
if (!StringUtils.containsAny(logEx.toString(), "Failed to connect to", "Remote host terminated the handshake", "Connection reset"))
Assert.fail(String.format("Unexpected error message '%s'", logEx));
}
/**
* verify events are index in correct order of the events were sent
*/
@Test
public void eventsIsIndexedInOrderOfSent() throws Exception {
TestUtil.enableHttpEventCollector();
String indexName="httpevents_in_order_l42";
TestUtil.createIndex(indexName);
String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
String loggerName = "splunkLogger4j2OrderOfSent";
HashMap<String, String> userInputs = new HashMap<>();
userInputs.put("user_logger_name", loggerName);
userInputs.put("user_httpEventCollector_token", token);
userInputs.put("user_index", indexName);
userInputs.put("user_send_mode", "sequential");
org.apache.logging.log4j.core.LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
//send multiple events and verify they are indexed in the order of sending
List<String> msgs = new ArrayList<>();
int totalEventsCount = 1000;
Logger logger = context.getLogger(loggerName);
String prefix="log4j2 multiple events";
for (int i = 0; i < totalEventsCount; i++) {
String jsonMsg = String.format("%s %s", prefix,i);
logger.info(jsonMsg);
msgs.add(jsonMsg);
}
TestUtil.verifyEventsSentInOrder(prefix,totalEventsCount,indexName);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
System.out.println("====================== Test pass=========================");
}
/**
* Test sending a JSON and text message with "_json" source type via http logging appender using log4j 2.x logger
*/
@Test
public void canSendJsonEventUsingUtilLoggerWithJsonSourceType() throws Exception {
canSendJsonEventUsingUtilLoggerWithSourceType("_json");
}
/**
* Test sending a JSON and text message with "battlecat_test" source type via http logging appender using log4j 2.x logger
*/
@Test
public void canSendJsonEventUsingUtilLoggerWithDefaultSourceType() throws Exception {
canSendJsonEventUsingUtilLoggerWithSourceType("battlecat_test");
}
private void canSendJsonEventUsingUtilLoggerWithSourceType(final String sourceType) throws Exception {
final String token = TestUtil.createHttpEventCollectorToken(httpEventCollectorName);
final String loggerName = "splunkLog4j2";
// Build User input map
final HashMap<String, String> userInputs = TestUtil.buildUserInputMap(loggerName, token, sourceType, "json");
final LoggerContext context = TestUtil.resetLog4j2Configuration("log4j2_template.xml", "log4j2.xml", userInputs);
final Logger logger = context.getLogger(loggerName);
final List<String> msgs = new ArrayList<>();
final long timeMillsec = new Date().getTime();
final JsonObject jsonObject = new JsonObject();
jsonObject.add("transactionId", new JsonPrimitive("11"));
jsonObject.add("userId", new JsonPrimitive("21"));
jsonObject.add("eventTimestamp", new JsonPrimitive(timeMillsec));
// Test with a json event message
jsonObject.add("severity", new JsonPrimitive("info"));
final String infoJson = jsonObject.toString();
logger.info(infoJson);
msgs.add(infoJson);
// Test with a text event message
final String infoText = String.format("{EventTimestamp:%s, EventMsg:'this is a text info for log4j2 logger}", timeMillsec);
logger.info(infoText);
msgs.add(infoText);
TestUtil.verifyEventsSentToSplunk(msgs);
TestUtil.deleteHttpEventCollectorToken(httpEventCollectorName);
}
}