1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-10-31 00:07:40 +02:00

sdk/log: ObservedTimestamp should be set (#5091)

* Set ObservedTimestamp

* Update sdk/log/logger.go

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* Reuse value

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
This commit is contained in:
Sam Xie
2024-03-22 07:39:01 -07:00
committed by GitHub
parent 335f4de960
commit b77a5c3968
2 changed files with 53 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ package log // import "go.opentelemetry.io/otel/sdk/log"
import (
"context"
"time"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/log"
@@ -13,6 +14,8 @@ import (
"go.opentelemetry.io/otel/trace"
)
var now = time.Now
// Compile-time check logger implements log.Logger.
var _ log.Logger = (*logger)(nil)
@@ -68,6 +71,12 @@ func (l *logger) newRecord(ctx context.Context, r log.Record) Record {
attributeValueLengthLimit: l.provider.attributeValueLengthLimit,
attributeCountLimit: l.provider.attributeCountLimit,
}
// This field SHOULD be set once the event is observed by OpenTelemetry.
if newRecord.observedTimestamp.IsZero() {
newRecord.observedTimestamp = now()
}
r.WalkAttributes(func(kv log.KeyValue) bool {
newRecord.AddAttributes(kv)
return true

View File

@@ -2,6 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
package log // import "go.opentelemetry.io/otel/sdk/log"
import (
"context"
"errors"
@@ -18,6 +19,16 @@ import (
)
func TestLoggerEmit(t *testing.T) {
nowDate := time.Date(2010, time.January, 1, 0, 0, 0, 0, time.UTC)
nowSwap := now
t.Cleanup(func() {
now = nowSwap
})
now = func() time.Time {
return nowDate
}
p0, p1, p2WithError := newProcessor("0"), newProcessor("1"), newProcessor("2")
p2WithError.Err = errors.New("error")
@@ -32,6 +43,9 @@ func TestLoggerEmit(t *testing.T) {
)
r.SetObservedTimestamp(time.Date(2001, time.January, 1, 0, 0, 0, 0, time.UTC))
rWithNoObservedTimestamp := r
rWithNoObservedTimestamp.SetObservedTimestamp(time.Time{})
contextWithSpanContext := trace.ContextWithSpanContext(context.Background(), trace.NewSpanContext(trace.SpanContextConfig{
TraceID: trace.TraceID{0o1},
SpanID: trace.SpanID{0o2},
@@ -154,6 +168,36 @@ func TestLoggerEmit(t *testing.T) {
},
},
},
{
name: "NoObservedTimestamp",
logger: newLogger(NewLoggerProvider(
WithProcessor(p0),
WithProcessor(p1),
WithAttributeValueLengthLimit(3),
WithAttributeCountLimit(2),
WithResource(resource.NewSchemaless(attribute.String("key", "value"))),
), instrumentation.Scope{Name: "scope"}),
ctx: context.Background(),
record: rWithNoObservedTimestamp,
expectedRecords: []Record{
{
timestamp: rWithNoObservedTimestamp.Timestamp(),
body: rWithNoObservedTimestamp.Body(),
severity: rWithNoObservedTimestamp.Severity(),
severityText: rWithNoObservedTimestamp.SeverityText(),
observedTimestamp: nowDate,
resource: resource.NewSchemaless(attribute.String("key", "value")),
attributeValueLengthLimit: 3,
attributeCountLimit: 2,
scope: &instrumentation.Scope{Name: "scope"},
front: [attributesInlineCount]log.KeyValue{
log.String("k1", "str"),
log.Float64("k2", 1.0),
},
nFront: 2,
},
},
},
}
for _, tc := range testCases {