diff --git a/CHANGELOG.md b/CHANGELOG.md index 38c5a9003..7094c5248 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Add `EventName` and `SetEventName` to `Record` in `go.opentelemetry.io/otel/log`. (#6187) - Add `EventName` to `RecordFactory` in `go.opentelemetry.io/otel/log/logtest`. (#6187) - `AssertRecordEqual` in `go.opentelemetry.io/otel/log/logtest` checks `Record.EventName`. (#6187) +- Add `EventName` and `SetEventName` to `Record` in `go.opentelemetry.io/otel/sdk/log`. (#6193) +- Add `EventName` to `RecordFactory` in `go.opentelemetry.io/otel/sdk/log/logtest`. (#6193) diff --git a/log/record.go b/log/record.go index 3f58ce7b1..4d2f32d0f 100644 --- a/log/record.go +++ b/log/record.go @@ -46,7 +46,7 @@ type Record struct { back []KeyValue } -// Event returns the event name. +// EventName returns the event name. // A log record with non-empty event name is interpreted as an event record. func (r *Record) EventName() string { return r.eventName diff --git a/sdk/log/logger.go b/sdk/log/logger.go index d6ca2ea41..3809aecb9 100644 --- a/sdk/log/logger.go +++ b/sdk/log/logger.go @@ -73,6 +73,7 @@ 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 b8da0750b..086b68452 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/logtest/factory.go b/sdk/log/logtest/factory.go index 14adefdac..e62695e87 100644 --- a/sdk/log/logtest/factory.go +++ b/sdk/log/logtest/factory.go @@ -22,6 +22,7 @@ import ( // // Do not use RecordFactory to create records in production code. type RecordFactory struct { + EventName string Timestamp time.Time ObservedTimestamp time.Time Severity log.Severity @@ -49,6 +50,7 @@ func (f RecordFactory) NewRecord() sdklog.Record { set(r, "attributeCountLimit", -1) set(r, "attributeValueLengthLimit", -1) + r.SetEventName(f.EventName) r.SetTimestamp(f.Timestamp) r.SetObservedTimestamp(f.ObservedTimestamp) r.SetSeverity(f.Severity) diff --git a/sdk/log/logtest/factory_test.go b/sdk/log/logtest/factory_test.go index 63e48de8b..595927085 100644 --- a/sdk/log/logtest/factory_test.go +++ b/sdk/log/logtest/factory_test.go @@ -25,6 +25,7 @@ func TestRecordFactoryEmpty(t *testing.T) { func TestRecordFactory(t *testing.T) { now := time.Now() observed := now.Add(time.Second) + eventName := "testing.name" severity := log.SeverityDebug severityText := "DBG" body := log.StringValue("Message") @@ -43,6 +44,7 @@ func TestRecordFactory(t *testing.T) { r := resource.NewSchemaless(attribute.Bool("works", true)) got := RecordFactory{ + EventName: eventName, Timestamp: now, ObservedTimestamp: observed, Severity: severity, @@ -57,6 +59,7 @@ func TestRecordFactory(t *testing.T) { Resource: r, }.NewRecord() + assert.Equal(t, eventName, got.EventName()) assert.Equal(t, now, got.Timestamp()) assert.Equal(t, observed, got.ObservedTimestamp()) assert.Equal(t, severity, got.Severity()) diff --git a/sdk/log/record.go b/sdk/log/record.go index f04e5b28f..a13fcac7b 100644 --- a/sdk/log/record.go +++ b/sdk/log/record.go @@ -42,6 +42,7 @@ func putIndex(index map[string]int) { } // Record is a log record emitted by the Logger. +// A log record with non-empty event name is interpreted as an event record. // // 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 +51,7 @@ 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 } +// EventName returns the event name. +// A log record with non-empty event name is interpreted as an event record. +func (r *Record) EventName() string { + return r.eventName +} + +// SetEventName sets the event name. +// A log record with non-empty event name is interpreted as an event record. +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 diff --git a/sdk/log/record_test.go b/sdk/log/record_test.go index 1d1d579d0..945a1b971 100644 --- a/sdk/log/record_test.go +++ b/sdk/log/record_test.go @@ -19,6 +19,14 @@ import ( "go.opentelemetry.io/otel/trace" ) +func TestRecordEventName(t *testing.T) { + const text = "testing text" + + r := new(Record) + r.SetEventName(text) + assert.Equal(t, text, r.EventName()) +} + func TestRecordTimestamp(t *testing.T) { now := time.Now() r := new(Record)