1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-28 21:09:17 +02:00

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

This commit is contained in:
Sam Xie 2024-04-25 01:19:18 -07:00 committed by GitHub
parent f33d40886d
commit 27e0344491
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 19 deletions

View File

@ -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

View File

@ -48,7 +48,7 @@ func TestExporter(t *testing.T) {
return exporter
}(),
want: getJSON(now),
want: getJSON(&now),
},
}
@ -113,7 +113,7 @@ func TestExporterExport(t *testing.T) {
options: []Option{},
ctx: context.Background(),
records: records,
wantResult: getJSONs(now),
wantResult: getJSONs(&now),
},
{
name: "NoRecords",
@ -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",
@ -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": {},
@ -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)
}

View File

@ -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
@ -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