Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Prototype] log: Events support with minimal non-breaking changes #6018

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
9 changes: 2 additions & 7 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions sdk/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
5 changes: 5 additions & 0 deletions sdk/log/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down
15 changes: 15 additions & 0 deletions sdk/log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading