Browse code

Fixed `raw` mode splunk logger

Splunk HEC does not accept log events with an empty string or a
whitespace-only string.

Signed-off-by: Florian Noeding <florian@noeding.com>

Florian Noeding authored on 2017/08/15 01:27:51
Showing 2 changed files
... ...
@@ -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...))
... ...
@@ -716,12 +716,19 @@ func TestRawFormatWithoutTag(t *testing.T) {
716 716
 	if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
717 717
 		t.Fatal(err)
718 718
 	}
719
+	message3Time := time.Now()
720
+	if err := loggerDriver.Log(&logger.Message{Line: []byte(" "), Source: "stdout", Timestamp: message3Time}); err != nil {
721
+		t.Fatal(err)
722
+	}
719 723
 
720 724
 	err = loggerDriver.Close()
721 725
 	if err != nil {
722 726
 		t.Fatal(err)
723 727
 	}
724 728
 
729
+	// message3 would have an empty or whitespace only string in the "event" field
730
+	// both of which are not acceptable to HEC
731
+	// thus here we must expect 2 messages, not 3
725 732
 	if len(hec.messages) != 2 {
726 733
 		t.Fatal("Expected two messages")
727 734
 	}