Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stdoutlog: Do not print timestamps when WithoutTimestamps is set #5241

Merged
merged 5 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions exporters/stdout/stdoutlog/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestExporter(t *testing.T) {

return exporter
}(),
want: getJSON(now),
want: getJSON(&now),
},
}

Expand Down Expand Up @@ -113,7 +113,7 @@ func TestExporterExport(t *testing.T) {
options: []Option{},
ctx: context.Background(),
records: records,
wantResult: getJSONs(now),
wantResult: getJSONs(&now),
},
{
name: "NoRecords",
Expand All @@ -127,21 +127,21 @@ func TestExporterExport(t *testing.T) {
options: []Option{WithPrettyPrint()},
ctx: context.Background(),
records: records,
wantResult: getPrettyJSONs(now),
wantResult: getPrettyJSONs(&now),
},
{
name: "WithoutTimestamps",
options: []Option{WithoutTimestamps()},
ctx: context.Background(),
records: records,
wantResult: getJSONs(time.Time{}),
wantResult: getJSONs(nil),
},
{
name: "WithoutTimestamps and WithPrettyPrint",
options: []Option{WithoutTimestamps(), WithPrettyPrint()},
ctx: context.Background(),
records: records,
wantResult: getPrettyJSONs(time.Time{}),
wantResult: getPrettyJSONs(nil),
},
{
name: "WithCanceledContext",
Expand Down Expand Up @@ -171,17 +171,17 @@ func TestExporterExport(t *testing.T) {
}
}

func getJSON(now time.Time) string {
func getJSON(now *time.Time) string {
serializedNow, _ := json.Marshal(now)

return "{\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + ",\"Severity\":9,\"SeverityText\":\"INFO\",\"Body\":{},\"Attributes\":[{\"Key\":\"key\",\"Value\":{}},{\"Key\":\"key2\",\"Value\":{}},{\"Key\":\"key3\",\"Value\":{}},{\"Key\":\"key4\",\"Value\":{}},{\"Key\":\"key5\",\"Value\":{}},{\"Key\":\"bool\",\"Value\":{}}],\"TraceID\":\"0102030405060708090a0b0c0d0e0f10\",\"SpanID\":\"0102030405060708\",\"TraceFlags\":\"01\",\"Resource\":{},\"Scope\":{\"Name\":\"\",\"Version\":\"\",\"SchemaURL\":\"\"},\"AttributeValueLengthLimit\":0,\"AttributeCountLimit\":0}\n"
XSAM marked this conversation as resolved.
Show resolved Hide resolved
}

func getJSONs(now time.Time) string {
func getJSONs(now *time.Time) string {
return getJSON(now) + getJSON(now)
}

func getPrettyJSON(now time.Time) string {
func getPrettyJSON(now *time.Time) string {
serializedNow, _ := json.Marshal(now)

return `{
Expand Down Expand Up @@ -231,7 +231,7 @@ func getPrettyJSON(now time.Time) string {
`
}

func getPrettyJSONs(now time.Time) string {
func getPrettyJSONs(now *time.Time) string {
return getPrettyJSON(now) + getPrettyJSON(now)
}

Expand Down
11 changes: 7 additions & 4 deletions exporters/stdout/stdoutlog/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (

// recordJSON is a JSON-serializable representation of a Record.
type recordJSON struct {
Timestamp time.Time
ObservedTimestamp time.Time
Timestamp *time.Time
ObservedTimestamp *time.Time
XSAM marked this conversation as resolved.
Show resolved Hide resolved
Severity log.Severity
SeverityText string
Body log.Value
Expand Down Expand Up @@ -52,8 +52,11 @@ func (e *Exporter) newRecordJSON(r sdklog.Record) recordJSON {
})

if e.timestamps {
newRecord.Timestamp = r.Timestamp()
newRecord.ObservedTimestamp = r.ObservedTimestamp()
timestamp := r.Timestamp()
newRecord.Timestamp = &timestamp

observedTimestamp := r.ObservedTimestamp()
newRecord.ObservedTimestamp = &observedTimestamp
}

return newRecord
Expand Down
Loading