Skip to content

Commit

Permalink
[chore][pkg/ottl] Establish internal/ctxutil package for shared get, …
Browse files Browse the repository at this point in the history
…set, and parse functions (#38185)
  • Loading branch information
djaglowski authored Feb 25, 2025
1 parent 6138451 commit 87512a0
Show file tree
Hide file tree
Showing 18 changed files with 82 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
package ctxutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"

import (
"encoding/hex"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal
package ctxutil_test

import (
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

func TestParseSpanIDError(t *testing.T) {
Expand All @@ -28,7 +30,7 @@ func TestParseSpanIDError(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseSpanID(tt.input)
_, err := ctxutil.ParseSpanID(tt.input)
assert.EqualError(t, err, tt.wantErr)
})
}
Expand All @@ -53,7 +55,7 @@ func TestParseTraceIDError(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := ParseTraceID(tt.input)
_, err := ctxutil.ParseTraceID(tt.input)
assert.EqualError(t, err, tt.wantErr)
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
package ctxutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"

import (
"context"
Expand Down Expand Up @@ -58,7 +58,7 @@ func SetMapValue[K any](ctx context.Context, tCtx K, m pcommon.Map, keys []ottl.
if !ok {
currentValue = m.PutEmpty(*s)
}
return setIndexableValue[K](ctx, tCtx, currentValue, val, keys[1:])
return SetIndexableValue[K](ctx, tCtx, currentValue, val, keys[1:])
}

func FetchValueFromExpression[K any, T int64 | string](ctx context.Context, tCtx K, key ottl.Key[K]) (*T, error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal
package ctxutil_test

import (
"context"
Expand All @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/pathtest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest"
)
Expand Down Expand Up @@ -121,7 +122,7 @@ func Test_GetMapValue_Invalid(t *testing.T) {
s := m.PutEmptySlice("slice")
s.AppendEmpty()

_, err := GetMapValue[any](context.Background(), nil, m, tt.keys)
_, err := ctxutil.GetMapValue[any](context.Background(), nil, m, tt.keys)
assert.Equal(t, tt.err.Error(), err.Error())
})
}
Expand All @@ -138,13 +139,13 @@ func Test_GetMapValue_MissingKey(t *testing.T) {
S: ottltest.Strp("unknown key"),
},
}
result, err := GetMapValue[any](context.Background(), nil, m, keys)
result, err := ctxutil.GetMapValue[any](context.Background(), nil, m, keys)
assert.NoError(t, err)
assert.Nil(t, result)
}

func Test_GetMapValue_NilKey(t *testing.T) {
_, err := GetMapValue[any](context.Background(), nil, pcommon.NewMap(), nil)
_, err := ctxutil.GetMapValue[any](context.Background(), nil, pcommon.NewMap(), nil)
assert.Error(t, err)
}

Expand Down Expand Up @@ -253,7 +254,7 @@ func Test_SetMapValue_Invalid(t *testing.T) {
s := m.PutEmptySlice("slice")
s.AppendEmpty()

err := SetMapValue[any](context.Background(), nil, m, tt.keys, "value")
err := ctxutil.SetMapValue[any](context.Background(), nil, m, tt.keys, "value")
assert.Equal(t, tt.err.Error(), err.Error())
})
}
Expand All @@ -273,7 +274,7 @@ func Test_SetMapValue_AddingNewSubMap(t *testing.T) {
S: ottltest.Strp("foo"),
},
}
err := SetMapValue[any](context.Background(), nil, m, keys, "bar")
err := ctxutil.SetMapValue[any](context.Background(), nil, m, keys, "bar")
assert.NoError(t, err)

expected := pcommon.NewMap()
Expand All @@ -297,7 +298,7 @@ func Test_SetMapValue_EmptyMap(t *testing.T) {
S: ottltest.Strp("foo"),
},
}
err := SetMapValue[any](context.Background(), nil, m, keys, "bar")
err := ctxutil.SetMapValue[any](context.Background(), nil, m, keys, "bar")
assert.NoError(t, err)

expected := pcommon.NewMap()
Expand All @@ -307,6 +308,6 @@ func Test_SetMapValue_EmptyMap(t *testing.T) {
}

func Test_SetMapValue_NilKey(t *testing.T) {
err := SetMapValue[any](context.Background(), nil, pcommon.NewMap(), nil, "bar")
err := ctxutil.SetMapValue[any](context.Background(), nil, pcommon.NewMap(), nil, "bar")
assert.Error(t, err)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
package ctxutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"

import (
"context"
Expand Down Expand Up @@ -61,5 +61,5 @@ func SetSliceValue[K any](ctx context.Context, tCtx K, s pcommon.Slice, keys []o
return fmt.Errorf("index %d out of bounds", idx)
}

return setIndexableValue[K](ctx, tCtx, s.At(idx), val, keys[1:])
return SetIndexableValue[K](ctx, tCtx, s.At(idx), val, keys[1:])
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal
package ctxutil_test

import (
"context"
Expand All @@ -12,6 +12,7 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/pathtest"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottltest"
)
Expand Down Expand Up @@ -81,14 +82,14 @@ func Test_GetSliceValue_Invalid(t *testing.T) {
s := pcommon.NewSlice()
s.AppendEmpty().SetStr("val")

_, err := GetSliceValue[any](context.Background(), nil, s, tt.keys)
_, err := ctxutil.GetSliceValue[any](context.Background(), nil, s, tt.keys)
assert.Equal(t, tt.err.Error(), err.Error())
})
}
}

func Test_GetSliceValue_NilKey(t *testing.T) {
_, err := GetSliceValue[any](context.Background(), nil, pcommon.NewSlice(), nil)
_, err := ctxutil.GetSliceValue[any](context.Background(), nil, pcommon.NewSlice(), nil)
assert.Error(t, err)
}

Expand Down Expand Up @@ -157,13 +158,13 @@ func Test_SetSliceValue_Invalid(t *testing.T) {
s := pcommon.NewSlice()
s.AppendEmpty().SetStr("val")

err := SetSliceValue[any](context.Background(), nil, s, tt.keys, "value")
err := ctxutil.SetSliceValue[any](context.Background(), nil, s, tt.keys, "value")
assert.Equal(t, tt.err.Error(), err.Error())
})
}
}

func Test_SetSliceValue_NilKey(t *testing.T) {
err := SetSliceValue[any](context.Background(), nil, pcommon.NewSlice(), nil, "value")
err := ctxutil.SetSliceValue[any](context.Background(), nil, pcommon.NewSlice(), nil, "value")
assert.Error(t, err)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
package ctxutil // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"

import (
"context"
Expand Down Expand Up @@ -112,7 +112,7 @@ func getIndexableValue[K any](ctx context.Context, tCtx K, value pcommon.Value,
return ottlcommon.GetValue(val), nil
}

func setIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.Value, val any, keys []ottl.Key[K]) error {
func SetIndexableValue[K any](ctx context.Context, tCtx K, currentValue pcommon.Value, val any, keys []ottl.Key[K]) error {
var newValue pcommon.Value
switch val.(type) {
case []string, []bool, []int64, []float64, [][]byte, []any:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package internal // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal"
package ctxutil_test

import (
"context"
Expand All @@ -11,13 +11,14 @@ import (
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/pathtest"
)

func Test_SetIndexableValue_InvalidValue(t *testing.T) {
keys := []ottl.Key[any]{
&pathtest.Key[any]{},
}
err := setIndexableValue[any](context.Background(), nil, pcommon.NewValueStr("str"), nil, keys)
err := ctxutil.SetIndexableValue[any](context.Background(), nil, pcommon.NewValueStr("str"), nil, keys)
assert.Error(t, err)
}
5 changes: 3 additions & 2 deletions pkg/ottl/contexts/internal/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxcache"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxresource"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

type ResourceContext interface {
Expand Down Expand Up @@ -57,10 +58,10 @@ func accessResourceAttributes[K ResourceContext]() ottl.StandardGetSetter[K] {
func accessResourceAttributesKey[K ResourceContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return GetMapValue[K](ctx, tCtx, tCtx.GetResource().Attributes(), keys)
return ctxutil.GetMapValue[K](ctx, tCtx, tCtx.GetResource().Attributes(), keys)
},
Setter: func(ctx context.Context, tCtx K, val any) error {
return SetMapValue[K](ctx, tCtx, tCtx.GetResource().Attributes(), keys, val)
return ctxutil.SetMapValue[K](ctx, tCtx, tCtx.GetResource().Attributes(), keys, val)
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/ottl/contexts/internal/scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxcache"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxscope"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

type InstrumentationScopeContext interface {
Expand Down Expand Up @@ -62,10 +63,10 @@ func accessInstrumentationScopeAttributes[K InstrumentationScopeContext]() ottl.
func accessInstrumentationScopeAttributesKey[K InstrumentationScopeContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return GetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys)
return ctxutil.GetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys)
},
Setter: func(ctx context.Context, tCtx K, val any) error {
return SetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys, val)
return ctxutil.SetMapValue[K](ctx, tCtx, tCtx.GetInstrumentationScope().Attributes(), keys, val)
},
}
}
Expand Down
11 changes: 6 additions & 5 deletions pkg/ottl/contexts/internal/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxcache"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxerror"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxspan"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
)

type SpanContext interface {
Expand Down Expand Up @@ -143,7 +144,7 @@ func accessStringTraceID[K SpanContext]() ottl.StandardGetSetter[K] {
},
Setter: func(_ context.Context, tCtx K, val any) error {
if str, ok := val.(string); ok {
id, err := ParseTraceID(str)
id, err := ctxutil.ParseTraceID(str)
if err != nil {
return err
}
Expand Down Expand Up @@ -176,7 +177,7 @@ func accessStringSpanID[K SpanContext]() ottl.StandardGetSetter[K] {
},
Setter: func(_ context.Context, tCtx K, val any) error {
if str, ok := val.(string); ok {
id, err := ParseSpanID(str)
id, err := ctxutil.ParseSpanID(str)
if err != nil {
return err
}
Expand Down Expand Up @@ -261,7 +262,7 @@ func accessStringParentSpanID[K SpanContext]() ottl.StandardGetSetter[K] {
},
Setter: func(_ context.Context, tCtx K, val any) error {
if str, ok := val.(string); ok {
id, err := ParseSpanID(str)
id, err := ctxutil.ParseSpanID(str)
if err != nil {
return err
}
Expand Down Expand Up @@ -435,10 +436,10 @@ func accessAttributes[K SpanContext]() ottl.StandardGetSetter[K] {
func accessAttributesKey[K SpanContext](keys []ottl.Key[K]) ottl.StandardGetSetter[K] {
return ottl.StandardGetSetter[K]{
Getter: func(ctx context.Context, tCtx K) (any, error) {
return GetMapValue[K](ctx, tCtx, tCtx.GetSpan().Attributes(), keys)
return ctxutil.GetMapValue[K](ctx, tCtx, tCtx.GetSpan().Attributes(), keys)
},
Setter: func(ctx context.Context, tCtx K, val any) error {
return SetMapValue[K](ctx, tCtx, tCtx.GetSpan().Attributes(), keys, val)
return ctxutil.SetMapValue[K](ctx, tCtx, tCtx.GetSpan().Attributes(), keys, val)
},
}
}
Expand Down
21 changes: 11 additions & 10 deletions pkg/ottl/contexts/ottldatapoint/datapoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxmetric"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxresource"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxscope"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/ctxutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/internal/logging"
)

Expand Down Expand Up @@ -327,10 +328,10 @@ func accessCache() ottl.StandardGetSetter[TransformContext] {
func accessCacheKey(key []ottl.Key[TransformContext]) ottl.StandardGetSetter[TransformContext] {
return ottl.StandardGetSetter[TransformContext]{
Getter: func(ctx context.Context, tCtx TransformContext) (any, error) {
return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key)
return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key)
},
Setter: func(ctx context.Context, tCtx TransformContext, val any) error {
return internal.SetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key, val)
return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.getCache(), key, val)
},
}
}
Expand Down Expand Up @@ -379,26 +380,26 @@ func accessAttributesKey(key []ottl.Key[TransformContext]) ottl.StandardGetSette
Getter: func(ctx context.Context, tCtx TransformContext) (any, error) {
switch tCtx.GetDataPoint().(type) {
case pmetric.NumberDataPoint:
return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key)
return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key)
case pmetric.HistogramDataPoint:
return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key)
return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key)
case pmetric.ExponentialHistogramDataPoint:
return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key)
return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key)
case pmetric.SummaryDataPoint:
return internal.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key)
return ctxutil.GetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key)
}
return nil, nil
},
Setter: func(ctx context.Context, tCtx TransformContext, val any) error {
switch tCtx.GetDataPoint().(type) {
case pmetric.NumberDataPoint:
return internal.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key, val)
return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.NumberDataPoint).Attributes(), key, val)
case pmetric.HistogramDataPoint:
return internal.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key, val)
return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.HistogramDataPoint).Attributes(), key, val)
case pmetric.ExponentialHistogramDataPoint:
return internal.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key, val)
return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.ExponentialHistogramDataPoint).Attributes(), key, val)
case pmetric.SummaryDataPoint:
return internal.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key, val)
return ctxutil.SetMapValue[TransformContext](ctx, tCtx, tCtx.GetDataPoint().(pmetric.SummaryDataPoint).Attributes(), key, val)
}
return nil
},
Expand Down
Loading

0 comments on commit 87512a0

Please sign in to comment.