diff --git a/.chloggen/ottl-len-metrics-issue-25868.yaml b/.chloggen/ottl-len-metrics-issue-25868.yaml new file mode 100644 index 000000000000..3f5867d80b39 --- /dev/null +++ b/.chloggen/ottl-len-metrics-issue-25868.yaml @@ -0,0 +1,20 @@ +# Use this changelog template to create an entry for release notes. +# If your change doesn't affect end users, such as a test fix or a tooling change, +# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. + +# 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 support for Metrics Slices to `Len` converter + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [25868] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: \ No newline at end of file diff --git a/pkg/ottl/ottlfuncs/func_len.go b/pkg/ottl/ottlfuncs/func_len.go index 1766469035ff..9263e83a08d4 100644 --- a/pkg/ottl/ottlfuncs/func_len.go +++ b/pkg/ottl/ottlfuncs/func_len.go @@ -9,12 +9,15 @@ import ( "reflect" "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) const ( - typeError = "target arg must be of type string, []any, map[string]any, pcommon.Map, pcommon.Slice, or pcommon.Value (of type String, Map, Slice)" + typeError = `target arg must be of type string, []any, map[string]any, pcommon.Map, pcommon.Slice, pcommon.Value (of type String, Map, Slice), + pmetric.ExemplarSlice, pmetric.ExponentialHistogramDataPointSlice, pmetric.HistogramDataPointSlice, pmetric.MetricSlice, pmetric.NumberDataPointSlice, + pmetric.ResourceMetricsSlice, pmetric.ScopeMetricsSlice, pmetric.SummaryDataPointSlice, pmetric.SummaryDataPointValueAtQuantileSlice` ) type LenArguments[K any] struct { @@ -58,6 +61,25 @@ func computeLen[K any](target ottl.Getter[K]) ottl.ExprFunc[K] { return int64(valType.Len()), nil case pcommon.Slice: return int64(valType.Len()), nil + + case pmetric.ExemplarSlice: + return int64(valType.Len()), nil + case pmetric.ExponentialHistogramDataPointSlice: + return int64(valType.Len()), nil + case pmetric.HistogramDataPointSlice: + return int64(valType.Len()), nil + case pmetric.MetricSlice: + return int64(valType.Len()), nil + case pmetric.NumberDataPointSlice: + return int64(valType.Len()), nil + case pmetric.ResourceMetricsSlice: + return int64(valType.Len()), nil + case pmetric.ScopeMetricsSlice: + return int64(valType.Len()), nil + case pmetric.SummaryDataPointSlice: + return int64(valType.Len()), nil + case pmetric.SummaryDataPointValueAtQuantileSlice: + return int64(valType.Len()), nil } v := reflect.ValueOf(val) diff --git a/pkg/ottl/ottlfuncs/func_len_test.go b/pkg/ottl/ottlfuncs/func_len_test.go index 3b28a43cb70a..e39981097ee9 100644 --- a/pkg/ottl/ottlfuncs/func_len_test.go +++ b/pkg/ottl/ottlfuncs/func_len_test.go @@ -10,6 +10,7 @@ import ( "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pmetric" "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" ) @@ -39,6 +40,60 @@ func Test_Len(t *testing.T) { t.Error(err) } + pmetricExemplarSlice := pmetric.NewExemplarSlice() + pmetricExemplarSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricExemplarSlice.AppendEmpty() + } + + pmetricExponentialHistogramDataPointSlice := pmetric.NewExponentialHistogramDataPointSlice() + pmetricExponentialHistogramDataPointSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricExponentialHistogramDataPointSlice.AppendEmpty() + } + + pmetricHistogramDataPointSlice := pmetric.NewHistogramDataPointSlice() + pmetricHistogramDataPointSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricHistogramDataPointSlice.AppendEmpty() + } + + pmetricMetricSlice := pmetric.NewMetricSlice() + pmetricMetricSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricMetricSlice.AppendEmpty() + } + + pmetricNumberDataPointSlice := pmetric.NewNumberDataPointSlice() + pmetricNumberDataPointSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricNumberDataPointSlice.AppendEmpty() + } + + pmetricResourceSlice := pmetric.NewResourceMetricsSlice() + pmetricResourceSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricResourceSlice.AppendEmpty() + } + + pmetricScopeMetricsSlice := pmetric.NewScopeMetricsSlice() + pmetricScopeMetricsSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricScopeMetricsSlice.AppendEmpty() + } + + pmetricSummaryDataPointSlice := pmetric.NewSummaryDataPointSlice() + pmetricSummaryDataPointSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricSummaryDataPointSlice.AppendEmpty() + } + + pmetricSummaryDataPointValueAtQuantileSlice := pmetric.NewSummaryDataPointValueAtQuantileSlice() + pmetricSummaryDataPointValueAtQuantileSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + pmetricSummaryDataPointValueAtQuantileSlice.AppendEmpty() + } + tests := []struct { name string value interface{} @@ -89,6 +144,51 @@ func Test_Len(t *testing.T) { value: pcommonValueMap, expected: 5, }, + { + name: "pmetric Exemplar slice", + value: pmetricExemplarSlice, + expected: 5, + }, + { + name: "pmetric ExponentialHistogramDataPoint slice", + value: pmetricExponentialHistogramDataPointSlice, + expected: 5, + }, + { + name: "pmetric HistogramDataPoint slice", + value: pmetricHistogramDataPointSlice, + expected: 5, + }, + { + name: "pmetric Metric slice", + value: pmetricMetricSlice, + expected: 5, + }, + { + name: "pmetric NumberDataPoint slice", + value: pmetricNumberDataPointSlice, + expected: 5, + }, + { + name: "pmetric Resource slice", + value: pmetricResourceSlice, + expected: 5, + }, + { + name: "pmetric ScopeMetrics slice", + value: pmetricScopeMetricsSlice, + expected: 5, + }, + { + name: "pmetric SummaryDataPoint slice", + value: pmetricSummaryDataPointSlice, + expected: 5, + }, + { + name: "pmetric SummaryDataPointValueAtQuantile slice", + value: pmetricSummaryDataPointValueAtQuantileSlice, + expected: 5, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {