Skip to content

Commit

Permalink
Add support for Metrics Slices to ottl len converter (#25890)
Browse files Browse the repository at this point in the history
Fixes #25868

**Description:** <Describe what has changed.>
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->

**Link to tracking Issue:** <Issue number if applicable>

**Testing:** <Describe what testing was performed and which tests were
added.>

**Documentation:** <Describe the documentation added.>
  • Loading branch information
markdingram authored Aug 23, 2023
1 parent e7dd234 commit 5c63c48
Show file tree
Hide file tree
Showing 3 changed files with 252 additions and 1 deletion.
20 changes: 20 additions & 0 deletions .chloggen/ottl-len-pdata-slices-issue-25868.yaml
Original file line number Diff line number Diff line change
@@ -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:
42 changes: 41 additions & 1 deletion pkg/ottl/ottlfuncs/func_len.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
191 changes: 191 additions & 0 deletions pkg/ottl/ottlfuncs/func_len_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 5c63c48

Please sign in to comment.