Files
a47746c5a7 Switch the dictionaries of dictionary tables only once (#14709)
Right now, when merging profiles, we switch dictionary entries when they
are used.

For example, for each sample, we do:

* Run `switchDictionary` for that sample.
* Switch all the sample indices dictionaries.
* Run `switchDictionary` for the stack associated with that sample.
* Update the sample's stack ID with the one provided by the new
dictionary.

This has a pretty big issue, as when stacks are associated to multiple
samples (which is very common in production workloads, but was missed in
tests), we will try switching the stack's dictionary multiple times.

* Switch the stack's dictionary, works fine.
* Switch it again. Now, we try assigning indices from the source
dictionary when they have meaning in the destination one.

Instead, we now do the following:

* Run `switchDictionary` for the stacks.
* Loop through samples, and run `switchDictionary` for each of them.
* Retrieve the stack. Because we already switched its dictionary, we get
a stack already pointing at the new dictionary.
* Update the sample's stack ID with the one provided by the new
dictionary.

<sub>This investigation and PR were proudly done **without** the use of
any LLM.</sub>

---------

Signed-off-by: Florian Lehner <[email protected]>
Co-authored-by: Florian Lehner <[email protected]>
2026-03-10 09:50:52 +00:00

48 lines
1.1 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
import "fmt"
// Equal checks equality with another LineSlice
func (l LineSlice) Equal(val LineSlice) bool {
if l.Len() != val.Len() {
return false
}
for i := range l.Len() {
if !l.At(i).Equal(val.At(i)) {
return false
}
}
return true
}
// Equal checks equality with another Line
func (l Line) Equal(val Line) bool {
return l.Column() == val.Column() &&
l.FunctionIndex() == val.FunctionIndex() &&
l.Line() == val.Line()
}
// switchDictionary updates the Line, switching its indices from one
// dictionary to another.
func (l Line) switchDictionary(src, dst ProfilesDictionary) error {
if l.FunctionIndex() > 0 {
if src.FunctionTable().Len() <= int(l.FunctionIndex()) {
return fmt.Errorf("invalid function index %d", l.FunctionIndex())
}
fn := src.FunctionTable().At(int(l.FunctionIndex()))
idx, err := SetFunction(dst.FunctionTable(), fn)
if err != nil {
return fmt.Errorf("couldn't set function: %w", err)
}
l.SetFunctionIndex(idx)
}
return nil
}