Skip to content

Commit 62e4225

Browse files
authored
sdk/log: Add EventName (#6193)
Fixes #6183 Fixes #6184 Towards #6181 Prior-art: #6018
1 parent 185547c commit 62e4225

8 files changed

+36
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1414
- Add `EventName` and `SetEventName` to `Record` in `go.opentelemetry.io/otel/log`. (#6187)
1515
- Add `EventName` to `RecordFactory` in `go.opentelemetry.io/otel/log/logtest`. (#6187)
1616
- `AssertRecordEqual` in `go.opentelemetry.io/otel/log/logtest` checks `Record.EventName`. (#6187)
17+
- Add `EventName` and `SetEventName` to `Record` in `go.opentelemetry.io/otel/sdk/log`. (#6193)
18+
- Add `EventName` to `RecordFactory` in `go.opentelemetry.io/otel/sdk/log/logtest`. (#6193)
1719

1820
<!-- Released section -->
1921
<!-- Don't change this section unless doing release -->

log/record.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ type Record struct {
4646
back []KeyValue
4747
}
4848

49-
// Event returns the event name.
49+
// EventName returns the event name.
5050
// A log record with non-empty event name is interpreted as an event record.
5151
func (r *Record) EventName() string {
5252
return r.eventName

sdk/log/logger.go

+1
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ func (l *logger) newRecord(ctx context.Context, r log.Record) Record {
7373
sc := trace.SpanContextFromContext(ctx)
7474

7575
newRecord := Record{
76+
eventName: r.EventName(),
7677
timestamp: r.Timestamp(),
7778
observedTimestamp: r.ObservedTimestamp(),
7879
severity: r.Severity(),

sdk/log/logger_test.go

+5
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestLoggerEmit(t *testing.T) {
3333
p2WithError.Err = errors.New("error")
3434

3535
r := log.Record{}
36+
r.SetEventName("testing.name")
3637
r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
3738
r.SetBody(log.StringValue("testing body value"))
3839
r.SetSeverity(log.SeverityInfo)
@@ -78,6 +79,7 @@ func TestLoggerEmit(t *testing.T) {
7879
record: r,
7980
expectedRecords: []Record{
8081
{
82+
eventName: r.EventName(),
8183
timestamp: r.Timestamp(),
8284
body: r.Body(),
8385
severity: r.Severity(),
@@ -118,6 +120,7 @@ func TestLoggerEmit(t *testing.T) {
118120
record: r,
119121
expectedRecords: []Record{
120122
{
123+
eventName: r.EventName(),
121124
timestamp: r.Timestamp(),
122125
body: r.Body(),
123126
severity: r.Severity(),
@@ -151,6 +154,7 @@ func TestLoggerEmit(t *testing.T) {
151154
record: r,
152155
expectedRecords: []Record{
153156
{
157+
eventName: r.EventName(),
154158
timestamp: r.Timestamp(),
155159
body: r.Body(),
156160
severity: r.Severity(),
@@ -181,6 +185,7 @@ func TestLoggerEmit(t *testing.T) {
181185
record: rWithNoObservedTimestamp,
182186
expectedRecords: []Record{
183187
{
188+
eventName: rWithNoObservedTimestamp.EventName(),
184189
timestamp: rWithNoObservedTimestamp.Timestamp(),
185190
body: rWithNoObservedTimestamp.Body(),
186191
severity: rWithNoObservedTimestamp.Severity(),

sdk/log/logtest/factory.go

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
//
2323
// Do not use RecordFactory to create records in production code.
2424
type RecordFactory struct {
25+
EventName string
2526
Timestamp time.Time
2627
ObservedTimestamp time.Time
2728
Severity log.Severity
@@ -49,6 +50,7 @@ func (f RecordFactory) NewRecord() sdklog.Record {
4950
set(r, "attributeCountLimit", -1)
5051
set(r, "attributeValueLengthLimit", -1)
5152

53+
r.SetEventName(f.EventName)
5254
r.SetTimestamp(f.Timestamp)
5355
r.SetObservedTimestamp(f.ObservedTimestamp)
5456
r.SetSeverity(f.Severity)

sdk/log/logtest/factory_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestRecordFactoryEmpty(t *testing.T) {
2525
func TestRecordFactory(t *testing.T) {
2626
now := time.Now()
2727
observed := now.Add(time.Second)
28+
eventName := "testing.name"
2829
severity := log.SeverityDebug
2930
severityText := "DBG"
3031
body := log.StringValue("Message")
@@ -43,6 +44,7 @@ func TestRecordFactory(t *testing.T) {
4344
r := resource.NewSchemaless(attribute.Bool("works", true))
4445

4546
got := RecordFactory{
47+
EventName: eventName,
4648
Timestamp: now,
4749
ObservedTimestamp: observed,
4850
Severity: severity,
@@ -57,6 +59,7 @@ func TestRecordFactory(t *testing.T) {
5759
Resource: r,
5860
}.NewRecord()
5961

62+
assert.Equal(t, eventName, got.EventName())
6063
assert.Equal(t, now, got.Timestamp())
6164
assert.Equal(t, observed, got.ObservedTimestamp())
6265
assert.Equal(t, severity, got.Severity())

sdk/log/record.go

+14
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func putIndex(index map[string]int) {
4242
}
4343

4444
// Record is a log record emitted by the Logger.
45+
// A log record with non-empty event name is interpreted as an event record.
4546
//
4647
// Do not create instances of Record on your own in production code.
4748
// You can use [go.opentelemetry.io/otel/sdk/log/logtest.RecordFactory]
@@ -50,6 +51,7 @@ type Record struct {
5051
// Do not embed the log.Record. Attributes need to be overwrite-able and
5152
// deep-copying needs to be possible.
5253

54+
eventName string
5355
timestamp time.Time
5456
observedTimestamp time.Time
5557
severity log.Severity
@@ -104,6 +106,18 @@ func (r *Record) setDropped(n int) {
104106
r.dropped = n
105107
}
106108

109+
// EventName returns the event name.
110+
// A log record with non-empty event name is interpreted as an event record.
111+
func (r *Record) EventName() string {
112+
return r.eventName
113+
}
114+
115+
// SetEventName sets the event name.
116+
// A log record with non-empty event name is interpreted as an event record.
117+
func (r *Record) SetEventName(s string) {
118+
r.eventName = s
119+
}
120+
107121
// Timestamp returns the time when the log record occurred.
108122
func (r *Record) Timestamp() time.Time {
109123
return r.timestamp

sdk/log/record_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ import (
1919
"go.opentelemetry.io/otel/trace"
2020
)
2121

22+
func TestRecordEventName(t *testing.T) {
23+
const text = "testing text"
24+
25+
r := new(Record)
26+
r.SetEventName(text)
27+
assert.Equal(t, text, r.EventName())
28+
}
29+
2230
func TestRecordTimestamp(t *testing.T) {
2331
now := time.Now()
2432
r := new(Record)

0 commit comments

Comments
 (0)