Skip to content

Commit

Permalink
stdoutlog: Do not print timestamps when WithoutTimestamps is set (#5241)
Browse files Browse the repository at this point in the history
  • Loading branch information
XSAM authored Apr 25, 2024
1 parent f33d408 commit 27e0344
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `RecordFactory` in `go.opentelemetry.io/otel/log/logtest` to facilitate testing the bridge implementations. (#5263)
- Add `RecordFactory` in `go.opentelemetry.io/otel/sdk/log/logtest` to facilitate testing the exporter and processor implementations. (#5258)

### Changed

- `go.opentelemetry.io/otel/exporters/stdout/stdoutlog` won't print timestamps when `WithoutTimestamps` option is set. (#5241)

## [1.26.0/0.48.0/0.2.0-alpha] 2024-04-24

### Added
Expand Down
36 changes: 21 additions & 15 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,22 +171,28 @@ func TestExporterExport(t *testing.T) {
}
}

func getJSON(now time.Time) string {
serializedNow, _ := json.Marshal(now)
func getJSON(now *time.Time) string {
var timestamps string
if now != nil {
serializedNow, _ := json.Marshal(now)
timestamps = "\"Timestamp\":" + string(serializedNow) + ",\"ObservedTimestamp\":" + string(serializedNow) + ","
}

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\":null,\"Scope\":{\"Name\":\"\",\"Version\":\"\",\"SchemaURL\":\"\"},\"AttributeValueLengthLimit\":0,\"AttributeCountLimit\":0}\n"
return "{" + timestamps + "\"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\":null,\"Scope\":{\"Name\":\"\",\"Version\":\"\",\"SchemaURL\":\"\"},\"AttributeValueLengthLimit\":0,\"AttributeCountLimit\":0}\n"
}

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

func getPrettyJSON(now time.Time) string {
serializedNow, _ := json.Marshal(now)
func getPrettyJSON(now *time.Time) string {
var timestamps string
if now != nil {
serializedNow, _ := json.Marshal(now)
timestamps = "\n\t\"Timestamp\": " + string(serializedNow) + ",\n\t\"ObservedTimestamp\": " + string(serializedNow) + ","
}

return `{
"Timestamp": ` + string(serializedNow) + `,
"ObservedTimestamp": ` + string(serializedNow) + `,
return `{` + timestamps + `
"Severity": 9,
"SeverityText": "INFO",
"Body": {},
Expand Down Expand Up @@ -231,7 +237,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 `json:",omitempty"`
ObservedTimestamp *time.Time `json:",omitempty"`
Severity log.Severity
SeverityText string
Body log.Value
Expand Down Expand Up @@ -53,8 +53,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

0 comments on commit 27e0344

Please sign in to comment.