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 a258b33
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 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
33 changes: 30 additions & 3 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 @@ -275,6 +277,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 +297,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 +317,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 +340,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 +370,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 +391,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 +412,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 +433,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 +457,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 +487,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 +522,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 +539,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 +565,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 a258b33

Please sign in to comment.