You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-07-13 01:00:22 +02:00
log: Add EventName (#6187)
Fixes https://github.com/open-telemetry/opentelemetry-go/issues/6182 Towards https://github.com/open-telemetry/opentelemetry-go/issues/6184 Towards https://github.com/open-telemetry/opentelemetry-go/issues/6181 Prior-art: https://github.com/open-telemetry/opentelemetry-go/pull/6018
This commit is contained in:
@ -11,6 +11,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
### Added
|
### Added
|
||||||
|
|
||||||
- Add `ValueFromAttribute` and `KeyValueFromAttribute` in `go.opentelemetry.io/otel/log`. (#6180)
|
- Add `ValueFromAttribute` and `KeyValueFromAttribute` in `go.opentelemetry.io/otel/log`. (#6180)
|
||||||
|
- 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)
|
||||||
|
|
||||||
<!-- Released section -->
|
<!-- Released section -->
|
||||||
<!-- Don't change this section unless doing release -->
|
<!-- Don't change this section unless doing release -->
|
||||||
|
@ -15,6 +15,10 @@ import (
|
|||||||
func AssertRecordEqual(t testing.TB, want, got log.Record) bool {
|
func AssertRecordEqual(t testing.TB, want, got log.Record) bool {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
|
|
||||||
|
if want.EventName() != got.EventName() {
|
||||||
|
t.Errorf("EventName value is not equal:\nwant: %v\ngot: %v", want.EventName(), got.EventName())
|
||||||
|
return false
|
||||||
|
}
|
||||||
if !want.Timestamp().Equal(got.Timestamp()) {
|
if !want.Timestamp().Equal(got.Timestamp()) {
|
||||||
t.Errorf("Timestamp value is not equal:\nwant: %v\ngot: %v", want.Timestamp(), got.Timestamp())
|
t.Errorf("Timestamp value is not equal:\nwant: %v\ngot: %v", want.Timestamp(), got.Timestamp())
|
||||||
return false
|
return false
|
||||||
|
@ -13,10 +13,11 @@ import (
|
|||||||
func TestAssertRecord(t *testing.T) {
|
func TestAssertRecord(t *testing.T) {
|
||||||
r1 := log.Record{}
|
r1 := log.Record{}
|
||||||
r2 := log.Record{}
|
r2 := log.Record{}
|
||||||
now := time.Now()
|
|
||||||
|
|
||||||
AssertRecordEqual(t, r1, r2)
|
AssertRecordEqual(t, r1, r2)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
r1.SetEventName("my_event")
|
||||||
|
r2.SetEventName("my_event")
|
||||||
r1.SetTimestamp(now)
|
r1.SetTimestamp(now)
|
||||||
r2.SetTimestamp(now)
|
r2.SetTimestamp(now)
|
||||||
r1.SetObservedTimestamp(now)
|
r1.SetObservedTimestamp(now)
|
||||||
|
@ -14,6 +14,7 @@ import (
|
|||||||
//
|
//
|
||||||
// Do not use RecordFactory to create records in production code.
|
// Do not use RecordFactory to create records in production code.
|
||||||
type RecordFactory struct {
|
type RecordFactory struct {
|
||||||
|
EventName string
|
||||||
Timestamp time.Time
|
Timestamp time.Time
|
||||||
ObservedTimestamp time.Time
|
ObservedTimestamp time.Time
|
||||||
Severity log.Severity
|
Severity log.Severity
|
||||||
@ -25,6 +26,7 @@ type RecordFactory struct {
|
|||||||
// NewRecord returns a log record.
|
// NewRecord returns a log record.
|
||||||
func (b RecordFactory) NewRecord() log.Record {
|
func (b RecordFactory) NewRecord() log.Record {
|
||||||
var record log.Record
|
var record log.Record
|
||||||
|
record.SetEventName(b.EventName)
|
||||||
record.SetTimestamp(b.Timestamp)
|
record.SetTimestamp(b.Timestamp)
|
||||||
record.SetObservedTimestamp(b.ObservedTimestamp)
|
record.SetObservedTimestamp(b.ObservedTimestamp)
|
||||||
record.SetSeverity(b.Severity)
|
record.SetSeverity(b.Severity)
|
||||||
|
@ -15,6 +15,7 @@ import (
|
|||||||
func TestRecordFactory(t *testing.T) {
|
func TestRecordFactory(t *testing.T) {
|
||||||
now := time.Now()
|
now := time.Now()
|
||||||
observed := now.Add(time.Second)
|
observed := now.Add(time.Second)
|
||||||
|
evnt := "my_event"
|
||||||
severity := log.SeverityDebug
|
severity := log.SeverityDebug
|
||||||
severityText := "DBG"
|
severityText := "DBG"
|
||||||
body := log.StringValue("Message")
|
body := log.StringValue("Message")
|
||||||
@ -25,6 +26,7 @@ func TestRecordFactory(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
got := RecordFactory{
|
got := RecordFactory{
|
||||||
|
EventName: evnt,
|
||||||
Timestamp: now,
|
Timestamp: now,
|
||||||
ObservedTimestamp: observed,
|
ObservedTimestamp: observed,
|
||||||
Severity: severity,
|
Severity: severity,
|
||||||
@ -33,6 +35,7 @@ func TestRecordFactory(t *testing.T) {
|
|||||||
Attributes: attrs,
|
Attributes: attrs,
|
||||||
}.NewRecord()
|
}.NewRecord()
|
||||||
|
|
||||||
|
assert.Equal(t, evnt, got.EventName())
|
||||||
assert.Equal(t, now, got.Timestamp())
|
assert.Equal(t, now, got.Timestamp())
|
||||||
assert.Equal(t, observed, got.ObservedTimestamp())
|
assert.Equal(t, observed, got.ObservedTimestamp())
|
||||||
assert.Equal(t, severity, got.Severity())
|
assert.Equal(t, severity, got.Severity())
|
||||||
|
@ -15,10 +15,12 @@ import (
|
|||||||
const attributesInlineCount = 5
|
const attributesInlineCount = 5
|
||||||
|
|
||||||
// Record represents a log record.
|
// Record represents a log record.
|
||||||
|
// A log record with non-empty event name is interpreted as an event record.
|
||||||
type Record struct {
|
type Record struct {
|
||||||
// Ensure forward compatibility by explicitly making this not comparable.
|
// Ensure forward compatibility by explicitly making this not comparable.
|
||||||
noCmp [0]func() //nolint: unused // This is indeed used.
|
noCmp [0]func() //nolint: unused // This is indeed used.
|
||||||
|
|
||||||
|
eventName string
|
||||||
timestamp time.Time
|
timestamp time.Time
|
||||||
observedTimestamp time.Time
|
observedTimestamp time.Time
|
||||||
severity Severity
|
severity Severity
|
||||||
@ -44,6 +46,18 @@ type Record struct {
|
|||||||
back []KeyValue
|
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.
|
// Timestamp returns the time when the log record occurred.
|
||||||
func (r *Record) Timestamp() time.Time {
|
func (r *Record) Timestamp() time.Time {
|
||||||
return r.timestamp
|
return r.timestamp
|
||||||
|
@ -15,6 +15,14 @@ import (
|
|||||||
|
|
||||||
var y2k = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
|
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) {
|
func TestRecordTimestamp(t *testing.T) {
|
||||||
var r log.Record
|
var r log.Record
|
||||||
r.SetTimestamp(y2k)
|
r.SetTimestamp(y2k)
|
||||||
|
Reference in New Issue
Block a user