diff --git a/.chloggen/ottl-len-pdata-slices-issue-25868.yaml b/.chloggen/ottl-len-pdata-slices-issue-25868.yaml new file mode 100644 index 000000000000..364758116a2d --- /dev/null +++ b/.chloggen/ottl-len-pdata-slices-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 Log, Metric and Trace 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..a1d9e50940a2 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/plog" + "go.opentelemetry.io/collector/pdata/pmetric" + "go.opentelemetry.io/collector/pdata/ptrace" "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) or a supported slice type from the plog, pmetric or ptrace packages` ) type LenArguments[K any] struct { @@ -58,6 +61,43 @@ 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 plog.LogRecordSlice: + return int64(valType.Len()), nil + case plog.ResourceLogsSlice: + return int64(valType.Len()), nil + case plog.ScopeLogsSlice: + 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 + + case ptrace.ResourceSpansSlice: + return int64(valType.Len()), nil + case ptrace.ScopeSpansSlice: + return int64(valType.Len()), nil + case ptrace.SpanEventSlice: + return int64(valType.Len()), nil + case ptrace.SpanLinkSlice: + return int64(valType.Len()), nil + case ptrace.SpanSlice: + 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..7595c12a9ff1 100644 --- a/pkg/ottl/ottlfuncs/func_len_test.go +++ b/pkg/ottl/ottlfuncs/func_len_test.go @@ -10,6 +10,9 @@ import ( "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" ) @@ -39,6 +42,108 @@ func Test_Len(t *testing.T) { t.Error(err) } + plogLogRecordSlice := plog.NewLogRecordSlice() + plogLogRecordSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + plogLogRecordSlice.AppendEmpty() + } + + plogResourceLogsSlice := plog.NewResourceLogsSlice() + plogResourceLogsSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + plogResourceLogsSlice.AppendEmpty() + } + + plogScopeLogsSlice := plog.NewScopeLogsSlice() + for i := 0; i < 5; i++ { + plogScopeLogsSlice.AppendEmpty() + } + plogScopeLogsSlice.EnsureCapacity(5) + + 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() + } + + ptraceResourceSpansSlice := ptrace.NewResourceSpansSlice() + ptraceResourceSpansSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + ptraceResourceSpansSlice.AppendEmpty() + } + + ptraceScopeSpansSlice := ptrace.NewScopeSpansSlice() + ptraceScopeSpansSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + ptraceScopeSpansSlice.AppendEmpty() + } + + ptraceSpanEventSlice := ptrace.NewSpanEventSlice() + ptraceSpanEventSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + ptraceSpanEventSlice.AppendEmpty() + } + + ptraceSpanLinkSlice := ptrace.NewSpanLinkSlice() + ptraceSpanLinkSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + ptraceSpanLinkSlice.AppendEmpty() + } + + ptraceSpanSlice := ptrace.NewSpanSlice() + ptraceSpanSlice.EnsureCapacity(5) + for i := 0; i < 5; i++ { + ptraceSpanSlice.AppendEmpty() + } + tests := []struct { name string value interface{} @@ -89,6 +194,92 @@ func Test_Len(t *testing.T) { value: pcommonValueMap, expected: 5, }, + { + name: "plog LogRecord slice", + value: plogLogRecordSlice, + expected: 5, + }, + { + name: "plog ResourceLogs slice", + value: plogResourceLogsSlice, + expected: 5, + }, + { + name: "plog ScopeLogs slice", + value: plogScopeLogsSlice, + 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, + }, + { + name: "ptrace ResourceSpans slice", + value: ptraceResourceSpansSlice, + expected: 5, + }, + { + name: "ptrace ScopeSpans slice", + value: ptraceScopeSpansSlice, + expected: 5, + }, + { + name: "ptrace SpanEvent slice", + value: ptraceSpanEventSlice, + expected: 5, + }, + + { + name: "ptrace SpanLink slice", + value: ptraceSpanLinkSlice, + expected: 5, + }, + { + name: "ptrace Span slice", + value: ptraceSpanSlice, + expected: 5, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {