From a6abaefcd31f12723a62486189e4af6dc8985e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 4 Dec 2024 11:59:49 +0100 Subject: [PATCH] sdk/log: Add event name to Record --- sdk/log/logger.go | 2 ++ sdk/log/logger_test.go | 5 +++++ sdk/log/record.go | 16 +++++++++++++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/sdk/log/logger.go b/sdk/log/logger.go index d6ca2ea41aa..981351caf21 100644 --- a/sdk/log/logger.go +++ b/sdk/log/logger.go @@ -73,6 +73,8 @@ func (l *logger) newRecord(ctx context.Context, r log.Record) Record { sc := trace.SpanContextFromContext(ctx) newRecord := Record{ + eventName: r.EventName(), + timestamp: r.Timestamp(), observedTimestamp: r.ObservedTimestamp(), severity: r.Severity(), diff --git a/sdk/log/logger_test.go b/sdk/log/logger_test.go index b8da0750bad..086b684529f 100644 --- a/sdk/log/logger_test.go +++ b/sdk/log/logger_test.go @@ -33,6 +33,7 @@ func TestLoggerEmit(t *testing.T) { p2WithError.Err = errors.New("error") r := log.Record{} + r.SetEventName("testing.name") r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)) r.SetBody(log.StringValue("testing body value")) r.SetSeverity(log.SeverityInfo) @@ -78,6 +79,7 @@ func TestLoggerEmit(t *testing.T) { record: r, expectedRecords: []Record{ { + eventName: r.EventName(), timestamp: r.Timestamp(), body: r.Body(), severity: r.Severity(), @@ -118,6 +120,7 @@ func TestLoggerEmit(t *testing.T) { record: r, expectedRecords: []Record{ { + eventName: r.EventName(), timestamp: r.Timestamp(), body: r.Body(), severity: r.Severity(), @@ -151,6 +154,7 @@ func TestLoggerEmit(t *testing.T) { record: r, expectedRecords: []Record{ { + eventName: r.EventName(), timestamp: r.Timestamp(), body: r.Body(), severity: r.Severity(), @@ -181,6 +185,7 @@ func TestLoggerEmit(t *testing.T) { record: rWithNoObservedTimestamp, expectedRecords: []Record{ { + eventName: rWithNoObservedTimestamp.EventName(), timestamp: rWithNoObservedTimestamp.Timestamp(), body: rWithNoObservedTimestamp.Body(), severity: rWithNoObservedTimestamp.Severity(), diff --git a/sdk/log/record.go b/sdk/log/record.go index 155e4cad2b6..bd45dd063de 100644 --- a/sdk/log/record.go +++ b/sdk/log/record.go @@ -41,7 +41,7 @@ func putIndex(index map[string]int) { indexPool.Put(index) } -// Record is a log record emitted by the Logger. +// Record is a log record or event emitted by the Logger. // // Do not create instances of Record on your own in production code. // You can use [go.opentelemetry.io/otel/sdk/log/logtest.RecordFactory] @@ -50,6 +50,8 @@ type Record struct { // Do not embed the log.Record. Attributes need to be overwrite-able and // deep-copying needs to be possible. + eventName string + timestamp time.Time observedTimestamp time.Time severity log.Severity @@ -104,6 +106,18 @@ func (r *Record) setDropped(n int) { r.dropped = n } +// Event returns the event name. +// A record with non-empty event name is an OpenTelemetry Event. +// A record with empty event name is an OpenTelemetry Log Record. +func (r *Record) EventName() string { + return r.eventName +} + +// SetEventName sets the event name. +func (r *Record) SetEventName(s string) { + r.eventName = s +} + // Timestamp returns the time when the log record occurred. func (r *Record) Timestamp() time.Time { return r.timestamp