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
23 changes: 23 additions & 0 deletions .chloggen/link-equal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata/pprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Introduce `Equal` method on the `Link` type

# One or more tracking issues or pull requests related to the change
issues: [13223]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
25 changes: 25 additions & 0 deletions .chloggen/set-link.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata/pprofile

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add new helper method `SetLink` to set a new link on a sample.

# One or more tracking issues or pull requests related to the change
issues: [13223]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
10 changes: 10 additions & 0 deletions pdata/pprofile/link.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"

// Equal checks equality with another Link
func (l Link) Equal(val Link) bool {
return l.TraceID() == val.TraceID() &&
l.SpanID() == val.SpanID()
}
79 changes: 79 additions & 0 deletions pdata/pprofile/link_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile

import (
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/collector/pdata/pcommon"
)

func TestLinkEqual(t *testing.T) {
for _, tt := range []struct {
name string
orig Link
dest Link
want bool
}{
{
name: "empty links",
orig: NewLink(),
dest: NewLink(),
want: true,
},
{
name: "non-empty identical links",
orig: buildLink(
pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}),
pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
),
dest: buildLink(
pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}),
pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
),
want: true,
},
{
name: "with different trace IDs",
orig: buildLink(
pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}),
pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
),
dest: buildLink(
pcommon.TraceID([16]byte{8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8}),
pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
),
want: false,
},
{
name: "with different span IDs",
orig: buildLink(
pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}),
pcommon.SpanID([8]byte{1, 2, 3, 4, 5, 6, 7, 8}),
),
dest: buildLink(
pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}),
pcommon.SpanID([8]byte{8, 7, 6, 5, 4, 3, 2, 1}),
),
want: false,
},
} {
t.Run(tt.name, func(t *testing.T) {
if tt.want {
assert.True(t, tt.orig.Equal(tt.dest))
} else {
assert.False(t, tt.orig.Equal(tt.dest))
}
})
}
}

func buildLink(traceID pcommon.TraceID, spanID pcommon.SpanID) Link {
l := NewLink()
l.SetTraceID(traceID)
l.SetSpanID(spanID)
return l
}
47 changes: 47 additions & 0 deletions pdata/pprofile/links.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"

import (
"errors"
"fmt"
"math"
)

var errTooManyLinkTableEntries = errors.New("too many entries in LinkTable")

// SetLink updates a LinkTable and a Sample's LinkIndex to
// add or update a link.
func SetLink(table LinkSlice, record Sample, li Link) error {
idx := int(record.LinkIndex())
if idx > 0 {
if idx >= table.Len() {
return fmt.Errorf("index value %d out of range for LinkIndex", idx)
}
mapAt := table.At(idx)
if mapAt.Equal(li) {
// Link already exists, nothing to do.
return nil
}
}

for j, l := range table.All() {
if l.Equal(li) {
if j > math.MaxInt32 {
return errTooManyLinkTableEntries
}
// Add the index of the existing link to the indices.
record.SetLinkIndex(int32(j)) //nolint:gosec // G115 overflow checked
return nil
}
}

if table.Len() >= math.MaxInt32 {
return errTooManyLinkTableEntries
}

li.CopyTo(table.AppendEmpty())
record.SetLinkIndex(int32(table.Len() - 1)) //nolint:gosec // G115 overflow checked
return nil
}
124 changes: 124 additions & 0 deletions pdata/pprofile/links_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package pprofile

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.opentelemetry.io/collector/pdata/pcommon"
)

func TestSetLink(t *testing.T) {
table := NewLinkSlice()
l := NewLink()
l.SetTraceID(pcommon.TraceID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 1}))
l2 := NewLink()
l.SetTraceID(pcommon.TraceID([16]byte{2, 2, 3, 4, 5, 6, 7, 8, 8, 7, 6, 5, 4, 3, 2, 2}))
smpl := NewSample()

// Put a first link
require.NoError(t, SetLink(table, smpl, l))
assert.Equal(t, 1, table.Len())
assert.Equal(t, int32(0), smpl.LinkIndex())

// Put the same link
// This should be a no-op.
require.NoError(t, SetLink(table, smpl, l))
assert.Equal(t, 1, table.Len())
assert.Equal(t, int32(0), smpl.LinkIndex())

// Set a new link
// This sets the index and adds to the table.
require.NoError(t, SetLink(table, smpl, l2))
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(table.Len()-1), smpl.LinkIndex()) //nolint:gosec // G115

// Set an existing link
require.NoError(t, SetLink(table, smpl, l))
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(0), smpl.LinkIndex())
// Set another existing link
require.NoError(t, SetLink(table, smpl, l2))
assert.Equal(t, 2, table.Len())
assert.Equal(t, int32(table.Len()-1), smpl.LinkIndex()) //nolint:gosec // G115
}

func TestSetLinkCurrentTooHigh(t *testing.T) {
table := NewLinkSlice()
smpl := NewSample()
smpl.SetLinkIndex(42)

err := SetLink(table, smpl, NewLink())
require.Error(t, err)
assert.Equal(t, 0, table.Len())
assert.Equal(t, int32(42), smpl.LinkIndex())
}

func BenchmarkSetLink(b *testing.B) {
for _, bb := range []struct {
name string
link Link

runBefore func(*testing.B, LinkSlice, Sample)
}{
{
name: "with a new link",
link: NewLink(),
},
{
name: "with an existing link",
link: func() Link {
l := NewLink()
l.SetTraceID(pcommon.NewTraceIDEmpty())
return l
}(),

runBefore: func(_ *testing.B, table LinkSlice, _ Sample) {
l := table.AppendEmpty()
l.SetTraceID(pcommon.NewTraceIDEmpty())
},
},
{
name: "with a duplicate link",
link: NewLink(),

runBefore: func(_ *testing.B, table LinkSlice, obj Sample) {
require.NoError(b, SetLink(table, obj, NewLink()))
},
},
{
name: "with a hundred links to loop through",
link: func() Link {
l := NewLink()
l.SetTraceID(pcommon.NewTraceIDEmpty())
return l
}(),

runBefore: func(_ *testing.B, table LinkSlice, _ Sample) {
for range 100 {
table.AppendEmpty()
}
},
},
} {
b.Run(bb.name, func(b *testing.B) {
table := NewLinkSlice()
obj := NewSample()

if bb.runBefore != nil {
bb.runBefore(b, table, obj)
}

b.ResetTimer()
b.ReportAllocs()

for range b.N {
_ = SetLink(table, obj, bb.link)
}
})
}
}
Loading