Skip to content

Commit bf30889

Browse files
committed
[chore] [pkg/ottl] Add function ProfileID()
1 parent 52a072f commit bf30889

File tree

4 files changed

+115
-0
lines changed

4 files changed

+115
-0
lines changed

pkg/ottl/ottlfuncs/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ Available Converters:
500500
- [ParseKeyValue](#parsekeyvalue)
501501
- [ParseSimplifiedXML](#parsesimplifiedxml)
502502
- [ParseXML](#parsexml)
503+
- [ProfileID](#profileid)
503504
- [RemoveXML](#removexml)
504505
- [Second](#second)
505506
- [Seconds](#seconds)
@@ -1714,6 +1715,18 @@ Examples:
17141715

17151716
- `ParseXML("<HostInfo hostname=\"example.com\" zone=\"east-1\" cloudprovider=\"aws\" />")`
17161717

1718+
### ProfileID
1719+
1720+
`ProfileID(bytes)`
1721+
1722+
The `ProfileID` Converter returns a `pprofile.ProfileID` struct from the given byte slice.
1723+
1724+
`bytes` is a byte slice of exactly 16 bytes.
1725+
1726+
Examples:
1727+
1728+
- `ProfileID(0x00000000000000000000000000000000)`
1729+
17171730
### RemoveXML
17181731

17191732
`RemoveXML(target, xpath)`

pkg/ottl/ottlfuncs/func_profile_id.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs"
5+
6+
import (
7+
"context"
8+
"errors"
9+
"fmt"
10+
11+
"go.opentelemetry.io/collector/pdata/pprofile"
12+
13+
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
14+
)
15+
16+
type ProfileIDArguments[K any] struct {
17+
Bytes []byte
18+
}
19+
20+
func NewProfileIDFactory[K any]() ottl.Factory[K] {
21+
return ottl.NewFactory("ProfileID", &ProfileIDArguments[K]{}, createProfileIDFunction[K])
22+
}
23+
24+
func createProfileIDFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
25+
args, ok := oArgs.(*ProfileIDArguments[K])
26+
27+
if !ok {
28+
return nil, errors.New("ProfileIDFactory args must be of type *ProfileIDArguments[K]")
29+
}
30+
31+
return profileID[K](args.Bytes)
32+
}
33+
34+
func profileID[K any](bytes []byte) (ottl.ExprFunc[K], error) {
35+
id := pprofile.ProfileID{}
36+
if len(bytes) != len(id) {
37+
return nil, fmt.Errorf("profile ids must be %d bytes", len(id))
38+
}
39+
copy(id[:], bytes)
40+
return func(context.Context, K) (any, error) {
41+
return id, nil
42+
}, nil
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package ottlfuncs
5+
6+
import (
7+
"testing"
8+
9+
"github.com/stretchr/testify/assert"
10+
"github.com/stretchr/testify/require"
11+
"go.opentelemetry.io/collector/pdata/pprofile"
12+
)
13+
14+
func Test_profileID(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
bytes []byte
18+
want pprofile.ProfileID
19+
}{
20+
{
21+
name: "create profile id",
22+
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16},
23+
want: pprofile.ProfileID([16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}),
24+
},
25+
}
26+
for _, tt := range tests {
27+
t.Run(tt.name, func(t *testing.T) {
28+
exprFunc, err := profileID[any](tt.bytes)
29+
assert.NoError(t, err)
30+
result, err := exprFunc(nil, nil)
31+
assert.NoError(t, err)
32+
assert.Equal(t, tt.want, result)
33+
})
34+
}
35+
}
36+
37+
func Test_profileID_validation(t *testing.T) {
38+
tests := []struct {
39+
name string
40+
bytes []byte
41+
}{
42+
{
43+
name: "byte slice less than 16",
44+
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
45+
},
46+
{
47+
name: "byte slice longer than 16",
48+
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
49+
},
50+
}
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
_, err := profileID[any](tt.bytes)
54+
require.Error(t, err)
55+
assert.ErrorContains(t, err, "profile ids must be 16 bytes")
56+
})
57+
}
58+
}

pkg/ottl/ottlfuncs/functions.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,6 @@ func converters[K any]() []ottl.Factory[K] {
114114
NewYearFactory[K](),
115115
NewHexFactory[K](),
116116
NewSliceToMapFactory[K](),
117+
NewProfileIDFactory[K](),
117118
}
118119
}

0 commit comments

Comments
 (0)