Skip to content

Commit

Permalink
feat(sumologicexporter): add clear_logs_timestamp option
Browse files Browse the repository at this point in the history
setting it to true indicates that backend will be responsoble for timestamp parsing

Signed-off-by: Dominik Rosiek <drosiek@sumologic.com>
  • Loading branch information
sumo-drosiek committed Aug 18, 2021
1 parent 27eeadd commit 6424852
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pkg/exporter/sumologicexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ exporters:
# maximum connection timeout is 55s, default = 5s
timeout: <timeout>

# defines if timestamp for logs should be set to 0,
# it indicates that backend will extract timestamp from logs,
# this option affects OTLP format only
# default = true
clear_logs_timestamp: {true, false}

# For below described source and graphite template related configuration,
# please refer to "Source templates" documentation chapter from this document.

Expand Down
8 changes: 8 additions & 0 deletions pkg/exporter/sumologicexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ type Config struct {
SourceHost string `mapstructure:"source_host"`
// Name of the client
Client string `mapstructure:"client"`

// ClearTimestamp defines if timestamp for logs should be set to 0.
// It indicates that backend will extract timestamp from logs.
// This option affects OTLP format only.
// By default this is true.
ClearLogsTimestamp bool `mapstructure:"clear_logs_timestamp"`
}

// CreateDefaultHTTPClientSettings returns default http client settings
Expand Down Expand Up @@ -165,4 +171,6 @@ const (
DefaultTranslateAttributes bool = true
// DefaultTranslateTelegrafMetrics defines default TranslateTelegrafMetrics
DefaultTranslateTelegrafMetrics bool = true
// DefaultClearTimestamp defines default ClearLogsTimestamp value
DefaultClearLogsTimestamp bool = true
)
42 changes: 42 additions & 0 deletions pkg/exporter/sumologicexporter/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,48 @@ func TestPushFailedBatch(t *testing.T) {
assert.EqualError(t, err, "error during sending data: 500 Internal Server Error")
}

func TestPushOTLPLogsWithClearTimestamp(t *testing.T) {
exampleLogs := exampleLog()
exampleLogs[0].SetTimestamp(12345)
logs := LogRecordsToLogs(exampleLogs)

config := createTestConfig()
config.ClearLogsTimestamp = true
config.LogFormat = OTLPLogFormat

expectedRequests := []func(w http.ResponseWriter, req *http.Request){
func(w http.ResponseWriter, req *http.Request) {
body := extractBody(t, req)
assert.Equal(t, "\n\x1b\n\x00\x12\x17\n\x00\x12\x13*\r\n\vExample logJ\x00R\x00", body)
},
}
test := prepareExporterTest(t, config, expectedRequests)

err := test.exp.pushLogsData(context.Background(), logs)
assert.NoError(t, err)
}

func TestPushOTLPLogsWithoutClearTimestamp(t *testing.T) {
exampleLogs := exampleLog()
exampleLogs[0].SetTimestamp(12345)
logs := LogRecordsToLogs(exampleLogs)

config := createTestConfig()
config.ClearLogsTimestamp = false
config.LogFormat = OTLPLogFormat

expectedRequests := []func(w http.ResponseWriter, req *http.Request){
func(w http.ResponseWriter, req *http.Request) {
body := extractBody(t, req)
assert.Equal(t, "\n$\n\x00\x12 \n\x00\x12\x1c\t90\x00\x00\x00\x00\x00\x00*\r\n\vExample logJ\x00R\x00", body)
},
}
test := prepareExporterTest(t, config, expectedRequests)

err := test.exp.pushLogsData(context.Background(), logs)
assert.NoError(t, err)
}

func TestPushTextLogsWithAttributeTranslation(t *testing.T) {
logs := LogRecordsToLogs(exampleLog())
logs.ResourceLogs().At(0).Resource().Attributes().InsertString("host.name", "harry-potter")
Expand Down
8 changes: 7 additions & 1 deletion pkg/exporter/sumologicexporter/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,13 @@ func (s *sender) sendOTLPLogs(ctx context.Context, flds fields) ([]pdata.LogReco
logs := ill.Logs()
logs.EnsureCapacity(len(s.logBuffer))
for _, record := range s.logBuffer {
record.CopyTo(logs.AppendEmpty())
log := logs.AppendEmpty()
record.CopyTo(log)

// Clear timestamp if required
if s.config.ClearLogsTimestamp {
log.SetTimestamp(0)
}
}

s.addResourceAttributes(rl.Resource().Attributes(), flds)
Expand Down

0 comments on commit 6424852

Please sign in to comment.