Fixed `raw` mode splunk logger
| ... | ... |
@@ -15,6 +15,7 @@ import ( |
| 15 | 15 |
"net/url" |
| 16 | 16 |
"os" |
| 17 | 17 |
"strconv" |
| 18 |
+ "strings" |
|
| 18 | 19 |
"sync" |
| 19 | 20 |
"time" |
| 20 | 21 |
|
| ... | ... |
@@ -363,6 +364,11 @@ func (l *splunkLoggerJSON) Log(msg *logger.Message) error {
|
| 363 | 363 |
} |
| 364 | 364 |
|
| 365 | 365 |
func (l *splunkLoggerRaw) Log(msg *logger.Message) error {
|
| 366 |
+ // empty or whitespace-only messages are not accepted by HEC |
|
| 367 |
+ if strings.TrimSpace(string(msg.Line)) == "" {
|
|
| 368 |
+ return nil |
|
| 369 |
+ } |
|
| 370 |
+ |
|
| 366 | 371 |
message := l.createSplunkMessage(msg) |
| 367 | 372 |
|
| 368 | 373 |
message.Event = string(append(l.prefix, msg.Line...)) |
| ... | ... |
@@ -713,12 +713,19 @@ func TestRawFormatWithoutTag(t *testing.T) {
|
| 713 | 713 |
if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
|
| 714 | 714 |
t.Fatal(err) |
| 715 | 715 |
} |
| 716 |
+ message3Time := time.Now() |
|
| 717 |
+ if err := loggerDriver.Log(&logger.Message{Line: []byte(" "), Source: "stdout", Timestamp: message3Time}); err != nil {
|
|
| 718 |
+ t.Fatal(err) |
|
| 719 |
+ } |
|
| 716 | 720 |
|
| 717 | 721 |
err = loggerDriver.Close() |
| 718 | 722 |
if err != nil {
|
| 719 | 723 |
t.Fatal(err) |
| 720 | 724 |
} |
| 721 | 725 |
|
| 726 |
+ // message3 would have an empty or whitespace only string in the "event" field |
|
| 727 |
+ // both of which are not acceptable to HEC |
|
| 728 |
+ // thus here we must expect 2 messages, not 3 |
|
| 722 | 729 |
if len(hec.messages) != 2 {
|
| 723 | 730 |
t.Fatal("Expected two messages")
|
| 724 | 731 |
} |