Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- `RPCGRPCResponseMetadata`
- 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)
- Add `Clone` method to `Record` in `go.opentelemetry.io/otel/log` that returns a copy of the record with no shared state. (#7001)

### Changed

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
}
61 changes: 61 additions & 0 deletions log/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,64 @@ 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)

// Clone and modify the clone
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)

// Assertions on original record (r0)
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()))

var r0Attrs []log.KeyValue
r0.WalkAttributes(func(kv log.KeyValue) bool {
r0Attrs = append(r0Attrs, kv)
return true
})
assert.Contains(t, r0Attrs, attr0)
assert.NotContains(t, r0Attrs, attr1)

// Assertions on cloned record (r1)
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()))

var r1Attrs []log.KeyValue
r1.WalkAttributes(func(kv log.KeyValue) bool {
r1Attrs = append(r1Attrs, kv)
return true
})
assert.Contains(t, r1Attrs, attr0)
assert.Contains(t, r1Attrs, attr1)
}
Loading