1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-01-26 03:52:03 +02:00
Robert Pająk 2025-01-22 17:46:47 +01:00 committed by GitHub
parent 185547c846
commit 62e4225a42
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 36 additions and 1 deletions

View File

@ -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` and `SetEventName` to `Record` in `go.opentelemetry.io/otel/log`. (#6187)
- Add `EventName` to `RecordFactory` in `go.opentelemetry.io/otel/log/logtest`. (#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) - `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 --> <!-- Released section -->
<!-- Don't change this section unless doing release --> <!-- Don't change this section unless doing release -->

View File

@ -46,7 +46,7 @@ type Record struct {
back []KeyValue 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. // A log record with non-empty event name is interpreted as an event record.
func (r *Record) EventName() string { func (r *Record) EventName() string {
return r.eventName return r.eventName

View File

@ -73,6 +73,7 @@ func (l *logger) newRecord(ctx context.Context, r log.Record) Record {
sc := trace.SpanContextFromContext(ctx) sc := trace.SpanContextFromContext(ctx)
newRecord := Record{ newRecord := Record{
eventName: r.EventName(),
timestamp: r.Timestamp(), timestamp: r.Timestamp(),
observedTimestamp: r.ObservedTimestamp(), observedTimestamp: r.ObservedTimestamp(),
severity: r.Severity(), severity: r.Severity(),

View File

@ -33,6 +33,7 @@ func TestLoggerEmit(t *testing.T) {
p2WithError.Err = errors.New("error") p2WithError.Err = errors.New("error")
r := log.Record{} r := log.Record{}
r.SetEventName("testing.name")
r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)) r.SetTimestamp(time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC))
r.SetBody(log.StringValue("testing body value")) r.SetBody(log.StringValue("testing body value"))
r.SetSeverity(log.SeverityInfo) r.SetSeverity(log.SeverityInfo)
@ -78,6 +79,7 @@ func TestLoggerEmit(t *testing.T) {
record: r, record: r,
expectedRecords: []Record{ expectedRecords: []Record{
{ {
eventName: r.EventName(),
timestamp: r.Timestamp(), timestamp: r.Timestamp(),
body: r.Body(), body: r.Body(),
severity: r.Severity(), severity: r.Severity(),
@ -118,6 +120,7 @@ func TestLoggerEmit(t *testing.T) {
record: r, record: r,
expectedRecords: []Record{ expectedRecords: []Record{
{ {
eventName: r.EventName(),
timestamp: r.Timestamp(), timestamp: r.Timestamp(),
body: r.Body(), body: r.Body(),
severity: r.Severity(), severity: r.Severity(),
@ -151,6 +154,7 @@ func TestLoggerEmit(t *testing.T) {
record: r, record: r,
expectedRecords: []Record{ expectedRecords: []Record{
{ {
eventName: r.EventName(),
timestamp: r.Timestamp(), timestamp: r.Timestamp(),
body: r.Body(), body: r.Body(),
severity: r.Severity(), severity: r.Severity(),
@ -181,6 +185,7 @@ func TestLoggerEmit(t *testing.T) {
record: rWithNoObservedTimestamp, record: rWithNoObservedTimestamp,
expectedRecords: []Record{ expectedRecords: []Record{
{ {
eventName: rWithNoObservedTimestamp.EventName(),
timestamp: rWithNoObservedTimestamp.Timestamp(), timestamp: rWithNoObservedTimestamp.Timestamp(),
body: rWithNoObservedTimestamp.Body(), body: rWithNoObservedTimestamp.Body(),
severity: rWithNoObservedTimestamp.Severity(), severity: rWithNoObservedTimestamp.Severity(),

View File

@ -22,6 +22,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
@ -49,6 +50,7 @@ func (f RecordFactory) NewRecord() sdklog.Record {
set(r, "attributeCountLimit", -1) set(r, "attributeCountLimit", -1)
set(r, "attributeValueLengthLimit", -1) set(r, "attributeValueLengthLimit", -1)
r.SetEventName(f.EventName)
r.SetTimestamp(f.Timestamp) r.SetTimestamp(f.Timestamp)
r.SetObservedTimestamp(f.ObservedTimestamp) r.SetObservedTimestamp(f.ObservedTimestamp)
r.SetSeverity(f.Severity) r.SetSeverity(f.Severity)

View File

@ -25,6 +25,7 @@ func TestRecordFactoryEmpty(t *testing.T) {
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)
eventName := "testing.name"
severity := log.SeverityDebug severity := log.SeverityDebug
severityText := "DBG" severityText := "DBG"
body := log.StringValue("Message") body := log.StringValue("Message")
@ -43,6 +44,7 @@ func TestRecordFactory(t *testing.T) {
r := resource.NewSchemaless(attribute.Bool("works", true)) r := resource.NewSchemaless(attribute.Bool("works", true))
got := RecordFactory{ got := RecordFactory{
EventName: eventName,
Timestamp: now, Timestamp: now,
ObservedTimestamp: observed, ObservedTimestamp: observed,
Severity: severity, Severity: severity,
@ -57,6 +59,7 @@ func TestRecordFactory(t *testing.T) {
Resource: r, Resource: r,
}.NewRecord() }.NewRecord()
assert.Equal(t, eventName, 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())

View File

@ -42,6 +42,7 @@ func putIndex(index map[string]int) {
} }
// Record is a log record emitted by the Logger. // 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. // Do not create instances of Record on your own in production code.
// You can use [go.opentelemetry.io/otel/sdk/log/logtest.RecordFactory] // You can use [go.opentelemetry.io/otel/sdk/log/logtest.RecordFactory]
@ -50,6 +51,7 @@ type Record struct {
// Do not embed the log.Record. Attributes need to be overwrite-able and // Do not embed the log.Record. Attributes need to be overwrite-able and
// deep-copying needs to be possible. // deep-copying needs to be possible.
eventName string
timestamp time.Time timestamp time.Time
observedTimestamp time.Time observedTimestamp time.Time
severity log.Severity severity log.Severity
@ -104,6 +106,18 @@ func (r *Record) setDropped(n int) {
r.dropped = n 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. // 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

View File

@ -19,6 +19,14 @@ import (
"go.opentelemetry.io/otel/trace" "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) { func TestRecordTimestamp(t *testing.T) {
now := time.Now() now := time.Now()
r := new(Record) r := new(Record)