Skip to content

Commit 1e2b677

Browse files
committed
fix all pkg/ottl tests
1 parent 7786479 commit 1e2b677

File tree

7 files changed

+49
-48
lines changed

7 files changed

+49
-48
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func PathGetSetter[K ProfileContext](path ottl.Path[K]) (ottl.GetSetter[K], erro
4747
return accessPeriod[K](), nil
4848
case "comment_string_indices":
4949
return accessCommentStringIndices[K](), nil
50-
case "default_sample_type_string_index":
51-
return accessDefaultSampleTypeStringIndex[K](), nil
50+
case "default_sample_type_index":
51+
return accessDefaultSampleTypeIndex[K](), nil
5252
case "profile_id":
5353
nextPath := path.Next()
5454
if nextPath != nil {
@@ -205,14 +205,14 @@ func accessCommentStringIndices[K ProfileContext]() ottl.StandardGetSetter[K] {
205205
}
206206
}
207207

208-
func accessDefaultSampleTypeStringIndex[K ProfileContext]() ottl.StandardGetSetter[K] {
208+
func accessDefaultSampleTypeIndex[K ProfileContext]() ottl.StandardGetSetter[K] {
209209
return ottl.StandardGetSetter[K]{
210210
Getter: func(_ context.Context, tCtx K) (any, error) {
211-
return int64(tCtx.GetProfile().DefaultSampleTypeStrindex()), nil
211+
return int64(tCtx.GetProfile().DefaultSampleTypeIndex()), nil
212212
},
213213
Setter: func(_ context.Context, tCtx K, val any) error {
214214
if i, ok := val.(int64); ok {
215-
tCtx.GetProfile().SetDefaultSampleTypeStrindex(int32(i))
215+
tCtx.GetProfile().SetDefaultSampleTypeIndex(int32(i))
216216
}
217217
return nil
218218
},

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func TestPathGetSetter(t *testing.T) {
6969
val: []int64{345},
7070
},
7171
{
72-
path: "default_sample_type_string_index",
72+
path: "default_sample_type_index",
7373
val: int64(456),
7474
},
7575
{

pkg/ottl/contexts/internal/logprofile/logging.go

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,89 +13,86 @@ import (
1313
"go.uber.org/zap/zapcore"
1414
)
1515

16-
type Profile pprofile.Profile
16+
type Profile struct {
17+
pprofile.Profile
18+
Dictionary pprofile.ProfilesDictionary
19+
}
1720

1821
func (p Profile) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
19-
pp := pprofile.Profile(p)
2022
var joinedErr error
2123

22-
vts, err := newValueTypes(p, pp.SampleType())
24+
vts, err := newValueTypes(p, p.SampleType())
2325
joinedErr = errors.Join(joinedErr, err)
2426
joinedErr = errors.Join(joinedErr, encoder.AddArray("sample_type", vts))
2527

26-
ss, err := newSamples(p, pp.Sample())
28+
ss, err := newSamples(p, p.Sample())
2729
joinedErr = errors.Join(joinedErr, err)
2830
joinedErr = errors.Join(joinedErr, encoder.AddArray("sample", ss))
2931

30-
encoder.AddInt64("time_nanos", int64(pp.Time()))
31-
encoder.AddInt64("duration_nanos", int64(pp.Duration()))
32+
encoder.AddInt64("time_nanos", int64(p.Time()))
33+
encoder.AddInt64("duration_nanos", int64(p.Duration()))
3234

33-
vt, err := newValueType(p, pp.PeriodType())
35+
vt, err := newValueType(p, p.PeriodType())
3436
joinedErr = errors.Join(joinedErr, err)
3537
joinedErr = errors.Join(joinedErr, encoder.AddObject("period_type", vt))
3638

37-
encoder.AddInt64("period", pp.Period())
39+
encoder.AddInt64("period", p.Period())
3840

3941
cs, err := p.getComments()
4042
joinedErr = errors.Join(joinedErr, err)
4143
joinedErr = errors.Join(joinedErr, encoder.AddArray("comments", cs))
4244

43-
dst, err := p.getString(pp.DefaultSampleTypeStrindex())
45+
dst, err := p.getString(p.DefaultSampleTypeIndex())
4446
joinedErr = errors.Join(joinedErr, err)
4547
encoder.AddString("default_sample_type", dst)
4648

47-
pid := pp.ProfileID()
49+
pid := p.ProfileID()
4850
encoder.AddString("profile_id", hex.EncodeToString(pid[:]))
49-
encoder.AddUint32("dropped_attributes_count", pp.DroppedAttributesCount())
50-
encoder.AddString("original_payload_format", pp.OriginalPayloadFormat())
51-
encoder.AddByteString("original_payload", pp.OriginalPayload().AsRaw())
51+
encoder.AddUint32("dropped_attributes_count", p.DroppedAttributesCount())
52+
encoder.AddString("original_payload_format", p.OriginalPayloadFormat())
53+
encoder.AddByteString("original_payload", p.OriginalPayload().AsRaw())
5254

53-
ats, err := newAttributes(p, pp.AttributeIndices())
55+
ats, err := newAttributes(p, p.AttributeIndices())
5456
joinedErr = errors.Join(joinedErr, err)
5557
joinedErr = errors.Join(joinedErr, encoder.AddArray("attributes", ats))
5658

5759
return joinedErr
5860
}
5961

6062
func (p Profile) getString(idx int32) (string, error) {
61-
pp := pprofile.Profile(p)
62-
strTable := pp.StringTable()
63+
strTable := p.Dictionary.StringTable()
6364
if idx >= int32(strTable.Len()) {
6465
return "", fmt.Errorf("string index out of bounds: %d", idx)
6566
}
6667
return strTable.At(int(idx)), nil
6768
}
6869

6970
func (p Profile) getFunction(idx int32) (function, error) {
70-
pp := pprofile.Profile(p)
71-
fnTable := pp.FunctionTable()
71+
fnTable := p.Dictionary.FunctionTable()
7272
if idx >= int32(fnTable.Len()) {
7373
return function{}, fmt.Errorf("function index out of bounds: %d", idx)
7474
}
7575
return newFunction(p, fnTable.At(int(idx)))
7676
}
7777

7878
func (p Profile) getMapping(idx int32) (mapping, error) {
79-
pp := pprofile.Profile(p)
80-
mTable := pp.MappingTable()
79+
mTable := p.Dictionary.MappingTable()
8180
if idx >= int32(mTable.Len()) {
8281
return mapping{}, fmt.Errorf("mapping index out of bounds: %d", idx)
8382
}
8483
return newMapping(p, mTable.At(int(idx)))
8584
}
8685

8786
func (p Profile) getLink(idx int32) (link, error) {
88-
pp := pprofile.Profile(p)
89-
lTable := pp.LinkTable()
87+
lTable := p.Dictionary.LinkTable()
9088
if idx >= int32(lTable.Len()) {
9189
return link{}, fmt.Errorf("link index out of bounds: %d", idx)
9290
}
9391
return link{lTable.At(int(idx))}, nil
9492
}
9593

9694
func (p Profile) getLocations(start, length int32) (locations, error) {
97-
pp := pprofile.Profile(p)
98-
locTable := pp.LocationTable()
95+
locTable := p.Dictionary.LocationTable()
9996
if start >= int32(locTable.Len()) {
10097
return locations{}, fmt.Errorf("location start index out of bounds: %d", start)
10198
}
@@ -115,8 +112,7 @@ func (p Profile) getLocations(start, length int32) (locations, error) {
115112
}
116113

117114
func (p Profile) getAttribute(idx int32) (attribute, error) {
118-
pp := pprofile.Profile(p)
119-
attrTable := pp.AttributeTable()
115+
attrTable := p.Dictionary.AttributeTable()
120116
if idx >= int32(attrTable.Len()) {
121117
return attribute{}, fmt.Errorf("attribute index out of bounds: %d", idx)
122118
}
@@ -504,11 +500,10 @@ type comments []string
504500

505501
func (p Profile) getComments() (comments, error) {
506502
var joinedErr error
507-
pp := pprofile.Profile(p)
508-
l := pp.CommentStrindices().Len()
503+
l := p.CommentStrindices().Len()
509504
cs := make(comments, 0, l)
510505
for i := range l {
511-
c, err := p.getString(pp.CommentStrindices().At(i))
506+
c, err := p.getString(p.CommentStrindices().At(i))
512507
if err != nil {
513508
joinedErr = errors.Join(joinedErr, err)
514509
}

pkg/ottl/contexts/internal/logprofile/logging_test.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,35 @@ import (
1717
func TestProfile_MarshalLogObject(t *testing.T) {
1818
tests := []struct {
1919
name string
20-
profile pprofile.Profile
20+
profile func() (pprofile.ProfilesDictionary, pprofile.Profile)
2121
contains []string
2222
notContains []string
2323
}{
2424
{
2525
name: "valid",
26-
profile: func() pprofile.Profile {
26+
profile: func() (pprofile.ProfilesDictionary, pprofile.Profile) {
27+
dic := pprofile.NewProfilesDictionary()
2728
p := &pprofiletest.Profile{
2829
ProfileID: pprofile.ProfileID([]byte("profileid1111111")),
2930
Attributes: []pprofiletest.Attribute{{Key: "container-attr1", Value: "value1"}},
3031
}
31-
return p.Transform(pprofile.NewScopeProfiles())
32-
}(),
32+
return dic, p.Transform(dic, pprofile.NewScopeProfiles())
33+
},
3334
notContains: []string{"profileError"},
3435
},
3536
{
36-
name: "invalid",
37-
profile: pprofile.NewProfile(), // doesn't include the required empty string in stringTable
37+
name: "invalid",
38+
profile: func() (pprofile.ProfilesDictionary, pprofile.Profile) {
39+
return pprofile.NewProfilesDictionary(), pprofile.NewProfile()
40+
},
3841
contains: []string{"profileError", "string index out of bounds: 0"},
3942
},
4043
}
4144
for _, tt := range tests {
4245
t.Run(tt.name, func(t *testing.T) {
46+
dic, prof := tt.profile()
4347
encoder := zapcore.NewJSONEncoder(zapcore.EncoderConfig{})
44-
buf, err := encoder.EncodeEntry(zapcore.Entry{}, []zapcore.Field{zap.Object("profile", Profile(tt.profile))})
48+
buf, err := encoder.EncodeEntry(zapcore.Entry{}, []zapcore.Field{zap.Object("profile", Profile{prof, dic})})
4549
assert.NoError(t, err)
4650

4751
for _, s := range tt.contains {

pkg/ottl/contexts/ottlprofile/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ The following paths are supported.
3232
| profile.period_type | the period type of the profile being processed | pprofile.ValueType |
3333
| profile.period | the period of the profile being processed | int64 |
3434
| profile.comment_string_indices | the comment string indices of the profile being processed | []int64 |
35-
| profile.default_sample_type_string_index | the default sample type string index of the profile being processed | int64 |
35+
| profile.default_sample_type_index | the default sample type string index of the profile being processed | int64 |
3636
| profile.profile_id | the profile id of the profile being processed | pprofile.ProfileID |
3737
| profile.profile_id.string | a string representation of the profile id | string |
3838
| profile.attribute_indices | the attribute indices of the profile being processed | []int64 |

pkg/ottl/contexts/ottlprofile/profile.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ var (
3737
func (tCtx TransformContext) MarshalLogObject(encoder zapcore.ObjectEncoder) error {
3838
err := encoder.AddObject("resource", logging.Resource(tCtx.resource))
3939
err = errors.Join(err, encoder.AddObject("scope", logging.InstrumentationScope(tCtx.instrumentationScope)))
40-
err = errors.Join(err, encoder.AddObject("profile", logprofile.Profile(tCtx.profile)))
40+
err = errors.Join(err, encoder.AddObject("profile", logprofile.Profile{Profile: tCtx.profile, Dictionary: tCtx.dictionary}))
4141
err = errors.Join(err, encoder.AddObject("cache", logging.Map(tCtx.cache)))
4242
return err
4343
}
4444

4545
// TransformContext represents a profile and its associated hierarchy.
4646
type TransformContext struct {
4747
profile pprofile.Profile
48+
dictionary pprofile.ProfilesDictionary
4849
instrumentationScope pcommon.InstrumentationScope
4950
resource pcommon.Resource
5051
cache pcommon.Map
@@ -56,9 +57,10 @@ type TransformContext struct {
5657
type TransformContextOption func(*TransformContext)
5758

5859
// NewTransformContext creates a new TransformContext with the provided parameters.
59-
func NewTransformContext(profile pprofile.Profile, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource, scopeProfiles pprofile.ScopeProfiles, resourceProfiles pprofile.ResourceProfiles, options ...TransformContextOption) TransformContext {
60+
func NewTransformContext(profile pprofile.Profile, dictionary pprofile.ProfilesDictionary, instrumentationScope pcommon.InstrumentationScope, resource pcommon.Resource, scopeProfiles pprofile.ScopeProfiles, resourceProfiles pprofile.ResourceProfiles, options ...TransformContextOption) TransformContext {
6061
tc := TransformContext{
6162
profile: profile,
63+
dictionary: dictionary,
6264
instrumentationScope: instrumentationScope,
6365
resource: resource,
6466
cache: pcommon.NewMap(),

pkg/ottl/contexts/ottlprofile/profile_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,12 @@ func Test_newPathGetSetter(t *testing.T) {
120120

121121
profile := createProfileTelemetry()
122122

123-
tCtx := NewTransformContext(profile, pcommon.NewInstrumentationScope(), pcommon.NewResource(), pprofile.NewScopeProfiles(), pprofile.NewResourceProfiles())
123+
tCtx := NewTransformContext(profile, pprofile.NewProfilesDictionary(), pcommon.NewInstrumentationScope(), pcommon.NewResource(), pprofile.NewScopeProfiles(), pprofile.NewResourceProfiles())
124124
got, err := accessor.Get(context.Background(), tCtx)
125125
assert.NoError(t, err)
126126
assert.Equal(t, tt.orig, got)
127127

128-
tCtx = NewTransformContext(profile, pcommon.NewInstrumentationScope(), pcommon.NewResource(), pprofile.NewScopeProfiles(), pprofile.NewResourceProfiles())
128+
tCtx = NewTransformContext(profile, pprofile.NewProfilesDictionary(), pcommon.NewInstrumentationScope(), pcommon.NewResource(), pprofile.NewScopeProfiles(), pprofile.NewResourceProfiles())
129129
err = accessor.Set(context.Background(), tCtx, tt.newVal)
130130
assert.NoError(t, err)
131131

@@ -146,7 +146,7 @@ func Test_newPathGetSetter_higherContextPath(t *testing.T) {
146146
instrumentationScope := pcommon.NewInstrumentationScope()
147147
instrumentationScope.SetName("instrumentation_scope")
148148

149-
ctx := NewTransformContext(pprofile.NewProfile(), instrumentationScope, resource, pprofile.NewScopeProfiles(), pprofile.NewResourceProfiles())
149+
ctx := NewTransformContext(pprofile.NewProfile(), pprofile.NewProfilesDictionary(), instrumentationScope, resource, pprofile.NewScopeProfiles(), pprofile.NewResourceProfiles())
150150

151151
tests := []struct {
152152
name string

0 commit comments

Comments
 (0)