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>
(cherry picked from commit 5f6d6a5093a4db799f9c1a6bb82eed1eea13ec0c)
Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Florian Noeding
2017-09-19 11:00:53 +02:00
committed by Sebastiaan van Stijn
parent 363a3e75cd
commit 1ad05ae326
2 changed files with 13 additions and 0 deletions
@@ -15,6 +15,7 @@ import (
"net/url"
"os"
"strconv"
"strings"
"sync"
"time"
@@ -363,6 +364,11 @@ func (l *splunkLoggerJSON) Log(msg *logger.Message) error {
}
func (l *splunkLoggerRaw) Log(msg *logger.Message) error {
// empty or whitespace-only messages are not accepted by HEC
if strings.TrimSpace(string(msg.Line)) == "" {
return nil
}
message := l.createSplunkMessage(msg)
message.Event = string(append(l.prefix, msg.Line...))
@@ -716,12 +716,19 @@ func TestRawFormatWithoutTag(t *testing.T) {
if err := loggerDriver.Log(&logger.Message{Line: []byte("notjson"), Source: "stdout", Timestamp: message2Time}); err != nil {
t.Fatal(err)
}
message3Time := time.Now()
if err := loggerDriver.Log(&logger.Message{Line: []byte(" "), Source: "stdout", Timestamp: message3Time}); err != nil {
t.Fatal(err)
}
err = loggerDriver.Close()
if err != nil {
t.Fatal(err)
}
// message3 would have an empty or whitespace only string in the "event" field
// both of which are not acceptable to HEC
// thus here we must expect 2 messages, not 3
if len(hec.messages) != 2 {
t.Fatal("Expected two messages")
}