Skip to content

log: Add Record.Clone#6991

Closed
arjun7579 wants to merge 2 commits intoopen-telemetry:mainfrom
arjun7579:add-clone-to-record
Closed

log: Add Record.Clone#6991
arjun7579 wants to merge 2 commits intoopen-telemetry:mainfrom
arjun7579:add-clone-to-record

Conversation

@arjun7579
Copy link
Copy Markdown
Contributor

@arjun7579 arjun7579 commented Jul 10, 2025

Fixes #6986

Add the Clone() method to the log.Record type.
It returns a copy of the record with no shared state, allowing both the original and the clone to be modified independently.

The back slice is cloned using slices.Clone to avoid shared references.

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Jul 10, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@codecov
Copy link
Copy Markdown

codecov Bot commented Jul 10, 2025

Codecov Report

Attention: Patch coverage is 0% with 4 lines in your changes missing coverage. Please review.

Project coverage is 82.7%. Comparing base (687c3f5) to head (8d9ecec).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
log/record.go 0.0% 4 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main   #6991     +/-   ##
=======================================
- Coverage   82.7%   82.7%   -0.1%     
=======================================
  Files        262     262             
  Lines      24325   24329      +4     
=======================================
- Hits       20140   20136      -4     
- Misses      3810    3818      +8     
  Partials     375     375             
Files with missing lines Coverage Δ
log/record.go 91.6% <0.0%> (-8.4%) ⬇️

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Member

@pellared pellared left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From https://github.com/open-telemetry/opentelemetry-go/blob/main/CONTRIBUTING.md:

update CHANGELOG.md

Each functionality should be covered by tests.

This should help you:

  • func TestRecordClone(t *testing.T) {
    now0 := time.Now()
    sev0 := log.SeverityInfo
    text0 := "text"
    val0 := log.BoolValue(true)
    attr0 := log.Bool("0", true)
    traceID0 := trace.TraceID([16]byte{1})
    spanID0 := trace.SpanID([8]byte{1})
    flag0 := trace.FlagsSampled
    r0 := new(Record)
    r0.SetTimestamp(now0)
    r0.SetObservedTimestamp(now0)
    r0.SetSeverity(sev0)
    r0.SetSeverityText(text0)
    r0.SetBody(val0)
    r0.SetAttributes(attr0)
    r0.SetTraceID(traceID0)
    r0.SetSpanID(spanID0)
    r0.SetTraceFlags(flag0)
    now1 := now0.Add(time.Second)
    sev1 := log.SeverityDebug
    text1 := "string"
    val1 := log.IntValue(1)
    attr1 := log.Int64("1", 2)
    traceID1 := trace.TraceID([16]byte{2})
    spanID1 := trace.SpanID([8]byte{2})
    flag1 := trace.TraceFlags(2)
    r1 := r0.Clone()
    r1.SetTimestamp(now1)
    r1.SetObservedTimestamp(now1)
    r1.SetSeverity(sev1)
    r1.SetSeverityText(text1)
    r1.SetBody(val1)
    r1.SetAttributes(attr1)
    r1.SetTraceID(traceID1)
    r1.SetSpanID(spanID1)
    r1.SetTraceFlags(flag1)
    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()))
    assert.Equal(t, traceID0, r0.TraceID())
    assert.Equal(t, spanID0, r0.SpanID())
    assert.Equal(t, flag0, r0.TraceFlags())
    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()))
    assert.Equal(t, traceID1, r1.TraceID())
    assert.Equal(t, spanID1, r1.SpanID())
    assert.Equal(t, flag1, r1.TraceFlags())
    r1.WalkAttributes(func(kv log.KeyValue) bool {
    return assert.Truef(t, kv.Equal(attr1), "%v != %v", kv, attr1)
    })
    }

Also linter failed with:

Error: log/record.go:146:1: File is not properly formatted (gofumpt)

@pellared pellared changed the title feat(log): implement Record.Clone (#6986) log: Add Record.Clone Jul 10, 2025
@arjun7579 arjun7579 closed this Jul 13, 2025
@arjun7579 arjun7579 deleted the add-clone-to-record branch July 13, 2025 15:19
@pellared pellared mentioned this pull request Jul 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

log: Add Record.Clone

2 participants