Skip to content

Commit ec86b0e

Browse files
committed
[pkg/ottl] Add function ProfileID()
1 parent 61feff7 commit ec86b0e

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
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)