Skip to content

Commit

Permalink
fix(sumologicexporter): don't send null logs
Browse files Browse the repository at this point in the history
  • Loading branch information
pmalek committed Sep 17, 2021
1 parent 87e0aa4 commit 7303bbf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
11 changes: 10 additions & 1 deletion pkg/exporter/sumologicexporter/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,16 @@ func (s *sender) logToText(record pdata.LogRecord) string {
// logToJSON converts LogRecord to a json line, returns it and error eventually
func (s *sender) logToJSON(record pdata.LogRecord) (string, error) {
data := s.filter.filterOut(record.Attributes())
data.orig.Upsert(logKey, record.Body())

// Only append the body when it's not empty to prevent sending 'null' log.
body := record.Body()
bodyType := body.Type()
if bodyType == pdata.AttributeValueTypeString && len(body.StringVal()) > 0 ||
bodyType == pdata.AttributeValueTypeArray && body.ArrayVal().Len() > 0 ||
bodyType == pdata.AttributeValueTypeMap && body.MapVal().Len() > 0 ||
bodyType == pdata.AttributeValueTypeBytes && len(body.BytesVal()) > 0 {
data.orig.Upsert(logKey, body)
}

nextLine, err := json.Marshal(pdata.AttributeMapToMap(data.orig))
if err != nil {
Expand Down
36 changes: 32 additions & 4 deletions pkg/exporter/sumologicexporter/sender_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,9 @@ import (
)

type senderTest struct {
srv *httptest.Server
s *sender
reqCounter *int32
srv *httptest.Server
s *sender
}

// prepareSenderTest prepares sender test environment.
Expand Down Expand Up @@ -72,7 +73,8 @@ func prepareSenderTest(t *testing.T, cb []func(w http.ResponseWriter, req *http.
require.NoError(t, err)

return &senderTest{
srv: testServer,
reqCounter: &reqCounter,
srv: testServer,
s: newSender(
cfg,
&http.Client{
Expand Down Expand Up @@ -128,7 +130,8 @@ func prepareOTLPSenderTest(t *testing.T, cb []func(w http.ResponseWriter, req *h
require.NoError(t, err)

return &senderTest{
srv: testServer,
reqCounter: &reqCounter,
srv: testServer,
s: newSender(
cfg,
&http.Client{
Expand Down Expand Up @@ -275,6 +278,7 @@ func TestSendLogs(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key1": "value", "key2": "value2"}))
assert.NoError(t, err)
assert.EqualValues(t, 1, *test.reqCounter)
}

func TestSendLogsMultitype(t *testing.T) {
Expand All @@ -294,6 +298,8 @@ func TestSendLogsMultitype(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key1": "value", "key2": "value2"}))
assert.NoError(t, err)

assert.EqualValues(t, 1, *test.reqCounter)
}

func TestSendLogsSplit(t *testing.T) {
Expand All @@ -312,6 +318,8 @@ func TestSendLogsSplit(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), newFields(pdata.NewAttributeMap()))
assert.NoError(t, err)

assert.EqualValues(t, 2, *test.reqCounter)
}
func TestSendLogsSplitFailedOne(t *testing.T) {
test := prepareSenderTest(t, []func(w http.ResponseWriter, req *http.Request){
Expand All @@ -333,6 +341,8 @@ func TestSendLogsSplitFailedOne(t *testing.T) {
dropped, err := test.s.sendLogs(context.Background(), newFields(pdata.NewAttributeMap()))
assert.EqualError(t, err, "error during sending data: 500 Internal Server Error")
assert.Equal(t, test.s.logBuffer[0:1], dropped)

assert.EqualValues(t, 2, *test.reqCounter)
}

func TestSendLogsSplitFailedAll(t *testing.T) {
Expand Down Expand Up @@ -361,6 +371,8 @@ func TestSendLogsSplitFailedAll(t *testing.T) {
"[error during sending data: 500 Internal Server Error; error during sending data: 404 Not Found]",
)
assert.Equal(t, test.s.logBuffer[0:2], dropped)

assert.EqualValues(t, 2, *test.reqCounter)
}

func TestSendLogsJson(t *testing.T) {
Expand All @@ -380,6 +392,8 @@ func TestSendLogsJson(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key": "value"}))
assert.NoError(t, err)

assert.EqualValues(t, 1, *test.reqCounter)
}

func TestSendLogsJsonMultitype(t *testing.T) {
Expand All @@ -399,6 +413,8 @@ func TestSendLogsJsonMultitype(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key": "value"}))
assert.NoError(t, err)

assert.EqualValues(t, 1, *test.reqCounter)
}

func TestSendLogsJsonSplit(t *testing.T) {
Expand All @@ -418,6 +434,8 @@ func TestSendLogsJsonSplit(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), newFields(pdata.NewAttributeMap()))
assert.NoError(t, err)

assert.EqualValues(t, 2, *test.reqCounter)
}

func TestSendLogsJsonSplitFailedOne(t *testing.T) {
Expand All @@ -440,6 +458,8 @@ func TestSendLogsJsonSplitFailedOne(t *testing.T) {
dropped, err := test.s.sendLogs(context.Background(), newFields(pdata.NewAttributeMap()))
assert.EqualError(t, err, "error during sending data: 500 Internal Server Error")
assert.Equal(t, test.s.logBuffer[0:1], dropped)

assert.EqualValues(t, 2, *test.reqCounter)
}

func TestSendLogsJsonSplitFailedAll(t *testing.T) {
Expand Down Expand Up @@ -468,6 +488,8 @@ func TestSendLogsJsonSplitFailedAll(t *testing.T) {
"[error during sending data: 500 Internal Server Error; error during sending data: 404 Not Found]",
)
assert.Equal(t, test.s.logBuffer[0:2], dropped)

assert.EqualValues(t, 2, *test.reqCounter)
}

func TestSendLogsUnexpectedFormat(t *testing.T) {
Expand Down Expand Up @@ -501,6 +523,8 @@ func TestSendLogsOTLP(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key1": "value", "key2": "value2"}))
assert.NoError(t, err)

assert.EqualValues(t, 1, *test.reqCounter)
}

func TestOverrideSourceName(t *testing.T) {
Expand All @@ -516,6 +540,8 @@ func TestOverrideSourceName(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key1": "test_name"}))
assert.NoError(t, err)

assert.EqualValues(t, 1, *test.reqCounter)
})

t.Run("otlp", func(t *testing.T) {
Expand All @@ -540,6 +566,8 @@ func TestOverrideSourceName(t *testing.T) {

_, err := test.s.sendLogs(context.Background(), fieldsFromMap(map[string]string{"key1": "test_name"}))
assert.NoError(t, err)

assert.EqualValues(t, 1, *test.reqCounter)
})
}

Expand Down

0 comments on commit 7303bbf

Please sign in to comment.