Debian Codegraph @

pdata/pprofile/attributes.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
5
6 import (
7 "errors"
8 "math"
9
10 "go.opentelemetry.io/collector/pdata/pcommon"
11 )
12
13 type attributable interface {
14 AttributeIndices() pcommon.Int32Slice
15 }
16
17 // FromAttributeIndices builds a [pcommon.Map] containing the attributes of a
18 // record.
19 // The record can be any struct that implements an `AttributeIndices` method.
20 // Updates made to the return map will not be applied back to the record.
21 func FromAttributeIndices(table KeyValueAndUnitSlice, record attributable, dic ProfilesDictionary) pcommon.Map {
22 m := pcommon.NewMap()
23 m.EnsureCapacity(record.AttributeIndices().Len())
24
25 for i := 0; i < record.AttributeIndices().Len(); i++ {
26 kv := table.At(int(record.AttributeIndices().At(i)))
27 key := dic.StringTable().At(int(kv.KeyStrindex()))
28 kv.Value().CopyTo(m.PutEmpty(key))
29 }
30
31 return m
32 }
33
34 var errTooManyAttributeTableEntries = errors.New("too many entries in AttributeTable")
35
36 // SetAttribute updates an AttributeTable, adding or providing a value and
37 // returns its index.
38 func SetAttribute(table KeyValueAndUnitSlice, attr KeyValueAndUnit) (int32, error) {
39 for j, a := range table.All() {
40 if a.Equal(attr) {
41 if j > math.MaxInt32 {
42 return 0, errTooManyAttributeTableEntries
43 }
44 return int32(j), nil
45 }
46 }
47
48 if table.Len() >= math.MaxInt32 {
49 return 0, errTooManyAttributeTableEntries
50 }
51
52 attr.CopyTo(table.AppendEmpty())
53 return int32(table.Len() - 1), nil
54 }