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

54 lines
1.5 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 Location
func (ms Location) Equal(val Location) bool {
return ms.MappingIndex() == val.MappingIndex() &&
ms.Address() == val.Address() &&
ms.AttributeIndices().Equal(val.AttributeIndices()) &&
ms.Lines().Equal(val.Lines())
}
// switchDictionary updates the Location, switching its indices from one
// dictionary to another.
func (ms Location) switchDictionary(src, dst ProfilesDictionary) error {
if ms.MappingIndex() > 0 {
if src.MappingTable().Len() <= int(ms.MappingIndex()) {
return fmt.Errorf("invalid mapping index %d", ms.MappingIndex())
}
mapping := src.MappingTable().At(int(ms.MappingIndex()))
idx, err := SetMapping(dst.MappingTable(), mapping)
if err != nil {
return fmt.Errorf("couldn't set mapping: %w", err)
}
ms.SetMappingIndex(idx)
}
for i, v := range ms.AttributeIndices().All() {
if src.AttributeTable().Len() <= int(v) {
return fmt.Errorf("invalid attribute index %d", v)
}
attr := src.AttributeTable().At(int(v))
idx, err := SetAttribute(dst.AttributeTable(), attr)
if err != nil {
return fmt.Errorf("couldn't set attribute %d: %w", i, err)
}
ms.AttributeIndices().SetAt(i, idx)
}
for i, v := range ms.Lines().All() {
err := v.switchDictionary(src, dst)
if err != nil {
return fmt.Errorf("couldn't switch dictionary for line %d: %w", i, err)
}
}
return nil
}