From 6ee2348a71dbcb1489bd1345e91b3bec5660923b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 4 Dec 2024 11:53:38 +0100 Subject: [PATCH 1/7] log: Add event name to Record --- log/record.go | 40 ++++++++++++++++++++++++++++------------ log/record_test.go | 8 ++++++++ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/log/record.go b/log/record.go index 7cf5446a041..73a5d4d028d 100644 --- a/log/record.go +++ b/log/record.go @@ -14,11 +14,15 @@ import ( // cover 95% of all use-cases (https://go.dev/blog/slog#performance). const attributesInlineCount = 5 -// Record represents a log record. +// Record represents a log record and event. +// A record with non-empty event name is an OpenTelemetry Event. +// A record with empty event name is an OpenTelemetry Log Record. type Record struct { // Ensure forward compatibility by explicitly making this not comparable. noCmp [0]func() //nolint: unused // This is indeed used. + eventName string + timestamp time.Time observedTimestamp time.Time severity Severity @@ -44,32 +48,44 @@ type Record struct { back []KeyValue } -// Timestamp returns the time when the log record occurred. +// 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 record occurred. func (r *Record) Timestamp() time.Time { return r.timestamp } -// SetTimestamp sets the time when the log record occurred. +// SetTimestamp sets the time when the record occurred. func (r *Record) SetTimestamp(t time.Time) { r.timestamp = t } -// ObservedTimestamp returns the time when the log record was observed. +// ObservedTimestamp returns the time when the record was observed. func (r *Record) ObservedTimestamp() time.Time { return r.observedTimestamp } -// SetObservedTimestamp sets the time when the log record was observed. +// SetObservedTimestamp sets the time when the record was observed. func (r *Record) SetObservedTimestamp(t time.Time) { r.observedTimestamp = t } -// Severity returns the [Severity] of the log record. +// Severity returns the [Severity] of the record. func (r *Record) Severity() Severity { return r.severity } -// SetSeverity sets the [Severity] level of the log record. +// SetSeverity sets the [Severity] level of the record. func (r *Record) SetSeverity(level Severity) { r.severity = level } @@ -86,17 +102,17 @@ func (r *Record) SetSeverityText(text string) { r.severityText = text } -// Body returns the body of the log record. +// Body returns the body of the record. func (r *Record) Body() Value { return r.body } -// SetBody sets the body of the log record. +// SetBody sets the body of the record. func (r *Record) SetBody(v Value) { r.body = v } -// WalkAttributes walks all attributes the log record holds by calling f for +// WalkAttributes walks all attributes the record holds by calling f for // each on each [KeyValue] in the [Record]. Iteration stops if f returns false. func (r *Record) WalkAttributes(f func(KeyValue) bool) { for i := 0; i < r.nFront; i++ { @@ -111,7 +127,7 @@ func (r *Record) WalkAttributes(f func(KeyValue) bool) { } } -// AddAttributes adds attributes to the log record. +// AddAttributes adds attributes to the record. func (r *Record) AddAttributes(attrs ...KeyValue) { var i int for i = 0; i < len(attrs) && r.nFront < len(r.front); i++ { @@ -124,7 +140,7 @@ func (r *Record) AddAttributes(attrs ...KeyValue) { r.back = append(r.back, attrs[i:]...) } -// AttributesLen returns the number of attributes in the log record. +// AttributesLen returns the number of attributes in the record. func (r *Record) AttributesLen() int { return r.nFront + len(r.back) } diff --git a/log/record_test.go b/log/record_test.go index 5a7487740d2..b3f145fe0f2 100644 --- a/log/record_test.go +++ b/log/record_test.go @@ -15,6 +15,14 @@ import ( var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC) +func TestRecordEventName(t *testing.T) { + const text = "testing text" + + var r log.Record + r.SetEventName(text) + assert.Equal(t, text, r.EventName()) +} + func TestRecordTimestamp(t *testing.T) { var r log.Record r.SetTimestamp(y2k) 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 2/7] 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 From f9bc38050e14a5ff69ac2fb0d80dd0704296566e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 4 Dec 2024 15:14:52 +0100 Subject: [PATCH 3/7] Add EventName EnabledParameters --- log/logger.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/log/logger.go b/log/logger.go index 0773a49b608..4c3e18a0dd8 100644 --- a/log/logger.go +++ b/log/logger.go @@ -138,5 +138,6 @@ func WithSchemaURL(schemaURL string) LoggerOption { // EnabledParameters represents payload for [Logger]'s Enabled method. type EnabledParameters struct { - Severity Severity + EventName string + Severity Severity } From 738b5cfa2a63e5cec62bdf5ae8fb399c9169f394 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 4 Dec 2024 15:37:22 +0100 Subject: [PATCH 4/7] Remove notice comments --- log/logger.go | 6 ------ 1 file changed, 6 deletions(-) diff --git a/log/logger.go b/log/logger.go index 4c3e18a0dd8..a8dc580c51c 100644 --- a/log/logger.go +++ b/log/logger.go @@ -28,9 +28,6 @@ type Logger interface { // // Implementations of this method need to be safe for a user to call // concurrently. - // - // Notice: Emit is intended to be used by log bridges. - // Is should not be used for writing instrumentation. Emit(ctx context.Context, record Record) // Enabled returns whether the Logger emits for the given context and @@ -53,9 +50,6 @@ type Logger interface { // // Implementations of this method need to be safe for a user to call // concurrently. - // - // Notice: Enabled is intended to be used by log bridges. - // Is should not be used for writing instrumentation. Enabled(ctx context.Context, param EnabledParameters) bool } From 86b6326e4150cc00a22132d6bf62f78326d34178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Wed, 4 Dec 2024 15:48:29 +0100 Subject: [PATCH 5/7] Add comments --- log/record.go | 2 ++ sdk/log/record.go | 2 ++ 2 files changed, 4 insertions(+) diff --git a/log/record.go b/log/record.go index 73a5d4d028d..77466024310 100644 --- a/log/record.go +++ b/log/record.go @@ -56,6 +56,8 @@ func (r *Record) EventName() string { } // SetEventName sets 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) SetEventName(s string) { r.eventName = s } diff --git a/sdk/log/record.go b/sdk/log/record.go index bd45dd063de..cea34eee772 100644 --- a/sdk/log/record.go +++ b/sdk/log/record.go @@ -114,6 +114,8 @@ func (r *Record) EventName() string { } // SetEventName sets 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) SetEventName(s string) { r.eventName = s } From d1eb448949160cf3d905eec89e1f47a78996345f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 5 Dec 2024 09:32:52 +0100 Subject: [PATCH 6/7] Refine comments --- log/record.go | 11 ++++------- sdk/log/record.go | 9 ++++----- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/log/record.go b/log/record.go index 77466024310..20e7426eb32 100644 --- a/log/record.go +++ b/log/record.go @@ -14,9 +14,8 @@ import ( // cover 95% of all use-cases (https://go.dev/blog/slog#performance). const attributesInlineCount = 5 -// Record represents a log record and event. -// A record with non-empty event name is an OpenTelemetry Event. -// A record with empty event name is an OpenTelemetry Log Record. +// Record represents a log record. +// A log record with non-empty event name is interpreted as an event record. type Record struct { // Ensure forward compatibility by explicitly making this not comparable. noCmp [0]func() //nolint: unused // This is indeed used. @@ -49,15 +48,13 @@ type Record struct { } // 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. +// 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 record with non-empty event name is an OpenTelemetry Event. -// A record with empty event name is an OpenTelemetry Log Record. +// A log record with non-empty event name is interpreted as an event record. func (r *Record) SetEventName(s string) { r.eventName = s } diff --git a/sdk/log/record.go b/sdk/log/record.go index cea34eee772..1487e89c06f 100644 --- a/sdk/log/record.go +++ b/sdk/log/record.go @@ -41,7 +41,8 @@ func putIndex(index map[string]int) { indexPool.Put(index) } -// Record is a log record or event emitted by the Logger. +// 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] @@ -107,15 +108,13 @@ func (r *Record) setDropped(n int) { } // 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. +// 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 record with non-empty event name is an OpenTelemetry Event. -// A record with empty event name is an OpenTelemetry Log Record. +// A log record with non-empty event name is interpreted as an event record. func (r *Record) SetEventName(s string) { r.eventName = s } From a5c4c59c54ea183eb23f21c1027948d8ef98d71f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 5 Dec 2024 09:38:44 +0100 Subject: [PATCH 7/7] Bring back comments --- log/record.go | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/log/record.go b/log/record.go index 20e7426eb32..96e0c5ce645 100644 --- a/log/record.go +++ b/log/record.go @@ -59,32 +59,32 @@ func (r *Record) SetEventName(s string) { r.eventName = s } -// Timestamp returns the time when the record occurred. +// Timestamp returns the time when the log record occurred. func (r *Record) Timestamp() time.Time { return r.timestamp } -// SetTimestamp sets the time when the record occurred. +// SetTimestamp sets the time when the log record occurred. func (r *Record) SetTimestamp(t time.Time) { r.timestamp = t } -// ObservedTimestamp returns the time when the record was observed. +// ObservedTimestamp returns the time when the log record was observed. func (r *Record) ObservedTimestamp() time.Time { return r.observedTimestamp } -// SetObservedTimestamp sets the time when the record was observed. +// SetObservedTimestamp sets the time when the log record was observed. func (r *Record) SetObservedTimestamp(t time.Time) { r.observedTimestamp = t } -// Severity returns the [Severity] of the record. +// Severity returns the [Severity] of the log record. func (r *Record) Severity() Severity { return r.severity } -// SetSeverity sets the [Severity] level of the record. +// SetSeverity sets the [Severity] level of the log record. func (r *Record) SetSeverity(level Severity) { r.severity = level } @@ -101,17 +101,17 @@ func (r *Record) SetSeverityText(text string) { r.severityText = text } -// Body returns the body of the record. +// Body returns the body of the log record. func (r *Record) Body() Value { return r.body } -// SetBody sets the body of the record. +// SetBody sets the body of the log record. func (r *Record) SetBody(v Value) { r.body = v } -// WalkAttributes walks all attributes the record holds by calling f for +// WalkAttributes walks all attributes the log record holds by calling f for // each on each [KeyValue] in the [Record]. Iteration stops if f returns false. func (r *Record) WalkAttributes(f func(KeyValue) bool) { for i := 0; i < r.nFront; i++ { @@ -126,7 +126,7 @@ func (r *Record) WalkAttributes(f func(KeyValue) bool) { } } -// AddAttributes adds attributes to the record. +// AddAttributes adds attributes to the log record. func (r *Record) AddAttributes(attrs ...KeyValue) { var i int for i = 0; i < len(attrs) && r.nFront < len(r.front); i++ { @@ -139,7 +139,7 @@ func (r *Record) AddAttributes(attrs ...KeyValue) { r.back = append(r.back, attrs[i:]...) } -// AttributesLen returns the number of attributes in the record. +// AttributesLen returns the number of attributes in the log record. func (r *Record) AttributesLen() int { return r.nFront + len(r.back) }