Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Metrics Slices to ottl len converter #25890

Merged
merged 2 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .chloggen/ottl-len-metrics-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 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:
24 changes: 23 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/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 {
Expand Down Expand Up @@ -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:
markdingram marked this conversation as resolved.
Show resolved Hide resolved
return int64(valType.Len()), nil
}

v := reflect.ValueOf(val)
Expand Down
100 changes: 100 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,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"
)
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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) {
Expand Down