Skip to content

Commit

Permalink
sdk/log: Add EventName (open-telemetry#6193)
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Jan 22, 2025
1 parent 185547c commit 62e4225
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

<!-- Released section -->
<!-- Don't change this section unless doing release -->
Expand Down
2 changes: 1 addition & 1 deletion log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions sdk/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
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
2 changes: 2 additions & 0 deletions sdk/log/logtest/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions sdk/log/logtest/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -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())
Expand Down
14 changes: 14 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,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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions sdk/log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 62e4225

Please sign in to comment.