diff --git a/log/logger.go b/log/logger.go index 0773a49b608..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 } @@ -138,5 +132,6 @@ func WithSchemaURL(schemaURL string) LoggerOption { // EnabledParameters represents payload for [Logger]'s Enabled method. type EnabledParameters struct { - Severity Severity + EventName string + Severity Severity } diff --git a/log/record.go b/log/record.go index 7cf5446a041..96e0c5ce645 100644 --- a/log/record.go +++ b/log/record.go @@ -15,10 +15,13 @@ import ( const attributesInlineCount = 5 // 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. + eventName string + timestamp time.Time observedTimestamp time.Time severity Severity @@ -44,6 +47,18 @@ type Record struct { back []KeyValue } +// Event 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/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) 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 f04e5b28f95..eace1d888e7 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,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 +107,18 @@ func (r *Record) setDropped(n int) { r.dropped = n } +// Event 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