Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `ErrorType` attribute helper function to the `go.opentelmetry.io/otel/semconv/v1.34.0` package. (#6962)
- Add `WithAllowKeyDuplication` in `go.opentelemetry.io/otel/sdk/log` which can be used to disable deduplication for log records. (#6968)

- `log.Record.Clone()` method that returns a copy of the record with no shared state. (#6986)
Comment thread
pellared marked this conversation as resolved.
Outdated
Comment thread
arjun7579 marked this conversation as resolved.
Outdated

### Changed

- Change `AssertEqual` in `go.opentelemetry.io/otel/log/logtest` to accept `TestingT` in order to support benchmarks and fuzz tests. (#6908)
Expand Down
8 changes: 8 additions & 0 deletions log/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,11 @@ func (r *Record) AddAttributes(attrs ...KeyValue) {
func (r *Record) AttributesLen() int {
return r.nFront + len(r.back)
}

// Clone returns a copy of the record with no shared state.
// The original record and the clone can both be modified without interfering with each other.
func (r *Record) Clone() Record {
res := *r
res.back = slices.Clone(r.back)
return res
}
48 changes: 48 additions & 0 deletions log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,51 @@ func TestRecordAllocationLimits(t *testing.T) {
// Convince the linter these values are used.
_, _, _, _, _, _ = tStamp, sev, text, body, n, attr
}

func TestRecordClone(t *testing.T) {
now0 := time.Now()
sev0 := log.SeverityInfo
text0 := "text"
val0 := log.BoolValue(true)
attr0 := log.Bool("0", true)

r0 := log.Record{}
r0.SetTimestamp(now0)
r0.SetObservedTimestamp(now0)
r0.SetSeverity(sev0)
r0.SetSeverityText(text0)
r0.SetBody(val0)
r0.AddAttributes(attr0)

now1 := now0.Add(time.Second)
sev1 := log.SeverityDebug
text1 := "string"
val1 := log.IntValue(1)
attr1 := log.Int64("1", 2)

r1 := r0.Clone()
r1.SetTimestamp(now1)
r1.SetObservedTimestamp(now1)
r1.SetSeverity(sev1)
r1.SetSeverityText(text1)
r1.SetBody(val1)
r1.AddAttributes(attr1)

assert.Equal(t, now0, r0.Timestamp())
assert.Equal(t, now0, r0.ObservedTimestamp())
assert.Equal(t, sev0, r0.Severity())
assert.Equal(t, text0, r0.SeverityText())
assert.True(t, val0.Equal(r0.Body()))
r0.WalkAttributes(func(kv log.KeyValue) bool {
return assert.Truef(t, kv.Equal(attr0), "%v != %v", kv, attr0)
})

assert.Equal(t, now1, r1.Timestamp())
assert.Equal(t, now1, r1.ObservedTimestamp())
assert.Equal(t, sev1, r1.Severity())
assert.Equal(t, text1, r1.SeverityText())
assert.True(t, val1.Equal(r1.Body()))
r1.WalkAttributes(func(kv log.KeyValue) bool {
return assert.Truef(t, kv.Equal(attr1), "%v != %v", kv, attr1)
})
Comment thread
pellared marked this conversation as resolved.
}
Loading