Skip to content

Commit cd0db67

Browse files
rockdabootvincentfree
authored andcommitted
[pkg/ottl] Add function ProfileID() (open-telemetry#39587)
#### Description Add the OTTL function `ProfileID()`, which is currently missing.
1 parent 6721c59 commit cd0db67

File tree

6 files changed

+156
-0
lines changed

6 files changed

+156
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: pkg/ottl
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Add the OTTL function ProfileID()
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [39587]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

pkg/ottl/e2e/e2e_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,12 @@ func Test_e2e_converters(t *testing.T) {
997997
tCtx.GetLogRecord().SetSpanID(pcommon.NewSpanIDEmpty())
998998
},
999999
},
1000+
{
1001+
statement: `set(attributes["test"], "pass") where String(ProfileID(0x00000000000000000000000000000001)) == "[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1]"`,
1002+
want: func(tCtx ottllog.TransformContext) {
1003+
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
1004+
},
1005+
},
10001006
{
10011007
statement: `set(attributes["test"], Split(attributes["flags"], "|"))`,
10021008
want: func(tCtx ottllog.TransformContext) {

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(0x00112233445566778899aabbccddeeff)`
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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
err string
42+
}{
43+
{
44+
name: "nil profile id",
45+
bytes: nil,
46+
err: "profile ids must be 16 bytes",
47+
},
48+
{
49+
name: "byte slice less than 16",
50+
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
51+
err: "profile ids must be 16 bytes",
52+
},
53+
{
54+
name: "byte slice longer than 16",
55+
bytes: []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17},
56+
err: "profile ids must be 16 bytes",
57+
},
58+
}
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
_, err := profileID[any](tt.bytes)
62+
require.Error(t, err)
63+
assert.ErrorContains(t, err, tt.err)
64+
})
65+
}
66+
}

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)