-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Add isList OTTL Function (#32392)
Description: Add isList OTTL function that returns true if the target is a slice and returns false if it is not Link to tracking Issue: #27870 Testing: Added tests Documentation: Updated readme and added changelog entry --------- Co-authored-by: Tyler Helmuth <[email protected]>
- Loading branch information
1 parent
e98a366
commit a84971c
Showing
6 changed files
with
288 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: enhancement | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add `IsList` OTTL Function | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [27870] | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [user] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type IsListArguments[K any] struct { | ||
Target ottl.Getter[K] | ||
} | ||
|
||
func NewIsListFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("IsList", &IsListArguments[K]{}, createIsListFunction[K]) | ||
} | ||
|
||
func createIsListFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*IsListArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("IsListFactory args must be of type *IsListArguments[K]") | ||
} | ||
|
||
return isList(args.Target), nil | ||
} | ||
|
||
func isList[K any](target ottl.Getter[K]) ottl.ExprFunc[K] { | ||
return func(ctx context.Context, tCtx K) (any, error) { | ||
val, err := target.Get(ctx, tCtx) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
switch valType := val.(type) { | ||
case pcommon.Value: | ||
return valType.Type() == pcommon.ValueTypeSlice, nil | ||
|
||
case pcommon.Slice, plog.LogRecordSlice, plog.ResourceLogsSlice, plog.ScopeLogsSlice, pmetric.ExemplarSlice, pmetric.ExponentialHistogramDataPointSlice, pmetric.HistogramDataPointSlice, pmetric.MetricSlice, pmetric.NumberDataPointSlice, pmetric.ResourceMetricsSlice, pmetric.ScopeMetricsSlice, pmetric.SummaryDataPointSlice, pmetric.SummaryDataPointValueAtQuantileSlice, ptrace.ResourceSpansSlice, ptrace.ScopeSpansSlice, ptrace.SpanEventSlice, ptrace.SpanLinkSlice, ptrace.SpanSlice, []string, []bool, []int64, []float64, [][]byte, []any: | ||
return true, nil | ||
} | ||
|
||
return false, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,189 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/plog" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/pdata/ptrace" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_IsList(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
value any | ||
expected bool | ||
}{ | ||
{ | ||
name: "map", | ||
value: make(map[string]any, 0), | ||
expected: false, | ||
}, | ||
{ | ||
name: "ValueTypeMap", | ||
value: pcommon.NewValueMap(), | ||
expected: false, | ||
}, | ||
{ | ||
name: "not map", | ||
value: "not a map", | ||
expected: false, | ||
}, | ||
{ | ||
name: "ValueTypeSlice", | ||
value: pcommon.NewValueSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "nil", | ||
value: nil, | ||
expected: false, | ||
}, | ||
{ | ||
name: "plog.LogRecordSlice", | ||
value: plog.NewLogRecordSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "plog.ResourceLogsSlice", | ||
value: plog.NewResourceLogsSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "plog.ScopeLogsSlice", | ||
value: plog.NewScopeLogsSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.ExemplarSlice", | ||
value: pmetric.NewExemplarSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.ExponentialHistogramDataPointSlice", | ||
value: pmetric.NewExponentialHistogramDataPointSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.HistogramDataPointSlice", | ||
value: pmetric.NewHistogramDataPointSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.MetricSlice", | ||
value: pmetric.NewMetricSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.NumberDataPointSlice", | ||
value: pmetric.NewNumberDataPointSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.ResourceMetricsSlice", | ||
value: pmetric.NewResourceMetricsSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.ScopeMetricsSlice", | ||
value: pmetric.NewScopeMetricsSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.SummaryDataPointSlice", | ||
value: pmetric.NewSummaryDataPointSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "pmetric.SummaryDataPointValueAtQuantileSlice", | ||
value: pmetric.NewSummaryDataPointValueAtQuantileSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "ptrace.ResourceSpansSlice", | ||
value: ptrace.NewResourceSpansSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "ptrace.ScopeSpansSlice", | ||
value: ptrace.NewScopeSpansSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "ptrace.SpanEventSlice", | ||
value: ptrace.NewSpanEventSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "ptrace.SpanLinkSlice", | ||
value: ptrace.NewSpanLinkSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "ptrace.SpanSlice", | ||
value: ptrace.NewSpanSlice(), | ||
expected: true, | ||
}, | ||
{ | ||
name: "[]string", | ||
value: []string{}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "[]bool", | ||
value: []bool{}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "[]int64", | ||
value: []int64{}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "[]float64", | ||
value: []float64{}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "[][]byte", | ||
value: [][]byte{}, | ||
expected: true, | ||
}, | ||
{ | ||
name: "[]any", | ||
value: []any{}, | ||
expected: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
exprFunc := isList[any](&ottl.StandardGetSetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return tt.value, nil | ||
}, | ||
}) | ||
result, err := exprFunc(context.Background(), nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.expected, result) | ||
}) | ||
} | ||
} | ||
|
||
func Test_IsList_Error(t *testing.T) { | ||
exprFunc := isList[any](&ottl.StandardGetSetter[any]{ | ||
Getter: func(context.Context, any) (any, error) { | ||
return nil, ottl.TypeError("") | ||
}, | ||
}) | ||
result, err := exprFunc(context.Background(), nil) | ||
assert.Equal(t, false, result) | ||
assert.IsType(t, ottl.TypeError(""), err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters