Skip to content

Commit 9be378b

Browse files
authored
[chore][pkg/ottl] Disallow setting empty profile IDs (#39659)
#### Description Disallow setting empty profile IDs via the OTTL accessor. See discussion at #39587 (comment)
1 parent 7781bdb commit 9be378b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

pkg/ottl/contexts/internal/ctxprofile/profile.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package ctxprofile // import "github.com/open-telemetry/opentelemetry-collector-
66
import (
77
"context"
88
"encoding/hex"
9+
"errors"
910
"time"
1011

1112
"go.opentelemetry.io/collector/pdata/pcommon"
@@ -333,8 +334,11 @@ func accessProfileID[K ProfileContext]() ottl.StandardGetSetter[K] {
333334
return tCtx.GetProfile().ProfileID(), nil
334335
},
335336
Setter: func(_ context.Context, tCtx K, val any) error {
336-
if i, ok := val.(pprofile.ProfileID); ok {
337-
tCtx.GetProfile().SetProfileID(i)
337+
if id, ok := val.(pprofile.ProfileID); ok {
338+
if id.IsEmpty() {
339+
return errors.New("profile ids must not be empty")
340+
}
341+
tCtx.GetProfile().SetProfileID(id)
338342
}
339343
return nil
340344
},
@@ -353,6 +357,9 @@ func accessStringProfileID[K ProfileContext]() ottl.StandardGetSetter[K] {
353357
if err != nil {
354358
return err
355359
}
360+
if id.IsEmpty() {
361+
return errors.New("profile ids must not be empty")
362+
}
356363
tCtx.GetProfile().SetProfileID(id)
357364
}
358365
return nil

pkg/ottl/contexts/internal/ctxprofile/profile_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package ctxprofile // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxprofile"
55
import (
66
"context"
7+
"encoding/hex"
78
"strings"
89
"testing"
910
"time"
@@ -104,10 +105,23 @@ func TestPathGetSetter(t *testing.T) {
104105
path: "profile_id",
105106
val: createProfileID(),
106107
},
108+
{
109+
path: "profile_id",
110+
val: pprofile.NewProfileIDEmpty(),
111+
setFails: true,
112+
},
107113
{
108114
path: "profile_id string",
109115
val: createProfileID().String(),
110116
},
117+
{
118+
path: "profile_id string",
119+
val: func() string {
120+
id := pprofile.NewProfileIDEmpty()
121+
return hex.EncodeToString(id[:])
122+
}(),
123+
setFails: true,
124+
},
111125
{
112126
path: "attribute_indices",
113127
val: []int64{567},

0 commit comments

Comments
 (0)