Skip to content

Commit

Permalink
sdk/log: Add event name to Record
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared committed Dec 4, 2024
1 parent 6ee2348 commit a6abaef
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
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
16 changes: 15 additions & 1 deletion sdk/log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6abaef

Please sign in to comment.