-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[processor/transform] Add copy_metric function (#30846)
**Description:** Adds a new function to the metric functions that allows copying the current TransformContext's method. **Link to tracking Issue:** <Issue number if applicable> Closes #16221 **Testing:** <Describe what testing was performed and which tests were added.> unit tests **Documentation:** Updated README --------- Co-authored-by: Evan Bradley <[email protected]>
- Loading branch information
1 parent
8639f5c
commit 688aca5
Showing
8 changed files
with
269 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,27 @@ | ||
# 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: processor/transform | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add `copy_metric` function to allow duplicating a metric | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [30846] | ||
|
||
# (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: | ||
|
||
# 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: [] |
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
67 changes: 67 additions & 0 deletions
67
processor/transformprocessor/internal/metrics/func_copy_metric.go
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,67 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/metrics" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric" | ||
) | ||
|
||
type copyMetricArguments struct { | ||
Name ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]] | ||
Description ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]] | ||
Unit ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]] | ||
} | ||
|
||
func newCopyMetricFactory() ottl.Factory[ottlmetric.TransformContext] { | ||
return ottl.NewFactory("copy_metric", ©MetricArguments{}, createCopyMetricFunction) | ||
} | ||
|
||
func createCopyMetricFunction(_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[ottlmetric.TransformContext], error) { | ||
args, ok := oArgs.(*copyMetricArguments) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("createCopyMetricFunction args must be of type *copyMetricArguments") | ||
} | ||
|
||
return copyMetric(args.Name, args.Description, args.Unit) | ||
} | ||
|
||
func copyMetric(name ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]], desc ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]], unit ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]) (ottl.ExprFunc[ottlmetric.TransformContext], error) { | ||
return func(ctx context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
cur := tCtx.GetMetric() | ||
metrics := tCtx.GetMetrics() | ||
newMetric := metrics.AppendEmpty() | ||
cur.CopyTo(newMetric) | ||
|
||
if !name.IsEmpty() { | ||
n, err := name.Get().Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newMetric.SetName(n) | ||
} | ||
|
||
if !desc.IsEmpty() { | ||
d, err := desc.Get().Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newMetric.SetDescription(d) | ||
} | ||
|
||
if !unit.IsEmpty() { | ||
u, err := unit.Get().Get(ctx, tCtx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
newMetric.SetUnit(u) | ||
} | ||
|
||
return nil, nil | ||
}, nil | ||
} |
142 changes: 142 additions & 0 deletions
142
processor/transformprocessor/internal/metrics/func_copy_metric_test.go
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,142 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package metrics | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"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" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric" | ||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest" | ||
) | ||
|
||
func Test_copyMetric(t *testing.T) { | ||
tests := []struct { | ||
testName string | ||
name ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]] | ||
desc ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]] | ||
unit ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]] | ||
want func(s pmetric.MetricSlice) | ||
}{ | ||
{ | ||
testName: "basic copy", | ||
name: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
desc: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
unit: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
want: func(ms pmetric.MetricSlice) { | ||
metric := ms.At(0) | ||
newMetric := ms.AppendEmpty() | ||
metric.CopyTo(newMetric) | ||
}, | ||
}, | ||
{ | ||
testName: "set name", | ||
name: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ | ||
Getter: func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
return "new name", nil | ||
}, | ||
}), | ||
desc: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
unit: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
want: func(ms pmetric.MetricSlice) { | ||
metric := ms.At(0) | ||
newMetric := ms.AppendEmpty() | ||
metric.CopyTo(newMetric) | ||
newMetric.SetName("new name") | ||
}, | ||
}, | ||
{ | ||
testName: "set description", | ||
name: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
desc: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ | ||
Getter: func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
return "new desc", nil | ||
}, | ||
}), | ||
unit: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
want: func(ms pmetric.MetricSlice) { | ||
metric := ms.At(0) | ||
newMetric := ms.AppendEmpty() | ||
metric.CopyTo(newMetric) | ||
newMetric.SetDescription("new desc") | ||
}, | ||
}, | ||
{ | ||
testName: "set unit", | ||
name: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
desc: ottl.Optional[ottl.StringGetter[ottlmetric.TransformContext]]{}, | ||
unit: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ | ||
Getter: func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
return "new unit", nil | ||
}, | ||
}), | ||
want: func(ms pmetric.MetricSlice) { | ||
metric := ms.At(0) | ||
newMetric := ms.AppendEmpty() | ||
metric.CopyTo(newMetric) | ||
newMetric.SetUnit("new unit") | ||
}, | ||
}, | ||
{ | ||
testName: "set all", | ||
name: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ | ||
Getter: func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
return "new name", nil | ||
}, | ||
}), | ||
desc: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ | ||
Getter: func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
return "new desc", nil | ||
}, | ||
}), | ||
unit: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ | ||
Getter: func(_ context.Context, tCtx ottlmetric.TransformContext) (any, error) { | ||
return "new unit", nil | ||
}, | ||
}), | ||
want: func(ms pmetric.MetricSlice) { | ||
metric := ms.At(0) | ||
newMetric := ms.AppendEmpty() | ||
metric.CopyTo(newMetric) | ||
newMetric.SetName("new name") | ||
newMetric.SetDescription("new desc") | ||
newMetric.SetUnit("new unit") | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.testName, func(t *testing.T) { | ||
ms := pmetric.NewMetricSlice() | ||
input := ms.AppendEmpty() | ||
input.SetName("test") | ||
input.SetDescription("test") | ||
input.SetUnit("test") | ||
input.SetEmptySum() | ||
d := input.Sum().DataPoints().AppendEmpty() | ||
d.SetIntValue(1) | ||
|
||
expected := pmetric.NewMetricSlice() | ||
ms.CopyTo(expected) | ||
tt.want(expected) | ||
|
||
exprFunc, err := copyMetric(tt.name, tt.desc, tt.unit) | ||
assert.NoError(t, err) | ||
_, err = exprFunc(nil, ottlmetric.NewTransformContext(input, ms, pcommon.NewInstrumentationScope(), pcommon.NewResource())) | ||
assert.NoError(t, err) | ||
|
||
x := pmetric.NewScopeMetrics() | ||
y := pmetric.NewScopeMetrics() | ||
|
||
expected.CopyTo(x.Metrics()) | ||
ms.CopyTo(y.Metrics()) | ||
|
||
assert.NoError(t, pmetrictest.CompareScopeMetrics(x, y)) | ||
}) | ||
} | ||
} |
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