Skip to content

[connector/routing] Add support for standard converter functions in routing conditions #38312

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

Merged
Merged
Show file tree
Hide file tree
Changes from 5 commits
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
27 changes: 27 additions & 0 deletions .chloggen/feat_add-routing-ismatch-isStr.yaml
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: routingconnector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Adds Standard Converter functions to routing connector.

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [38282]

# (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: []
2 changes: 2 additions & 0 deletions connector/routingconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ The following settings are available:
### Supported [OTTL] functions

- [IsMatch](../../pkg/ottl/ottlfuncs/README.md#IsMatch)
- [IsString](../../pkg/ottl/ottlfuncs/README.md#IsString)
- [IsMap](../../pkg/ottl/ottlfuncs/README.md#IsMap)
- [delete_key](../../pkg/ottl/ottlfuncs/README.md#delete_key)
- [delete_matching_keys](../../pkg/ottl/ottlfuncs/README.md#delete_matching_keys)

Expand Down
21 changes: 13 additions & 8 deletions connector/routingconnector/internal/common/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@ func createRouteFunction[K any](_ ottl.FunctionContext, _ ottl.Arguments) (ottl.
}

func Functions[K any]() map[string]ottl.Factory[K] {
return ottl.CreateFactoryMap(
ottlfuncs.NewIsMatchFactory[K](),
ottlfuncs.NewDeleteKeyFactory[K](),
ottlfuncs.NewDeleteMatchingKeysFactory[K](),
// noop function, it is required since the parsing of conditions is not implemented yet,
////github.com/open-telemetry/opentelemetry-collector-contrib/issues/13545
ottl.NewFactory("route", nil, createRouteFunction[K]),
)
// standard converters do not transform data, so we can safely use them
funcs := ottlfuncs.StandardConverters[K]()

deleteKey := ottlfuncs.NewDeleteKeyFactory[K]()
funcs[deleteKey.Name()] = deleteKey

deleteMatchingKeys := ottlfuncs.NewDeleteMatchingKeysFactory[K]()
funcs[deleteMatchingKeys.Name()] = deleteMatchingKeys

route := ottl.NewFactory("route", nil, createRouteFunction[K])
funcs[route.Name()] = route

return funcs
}
32 changes: 32 additions & 0 deletions connector/routingconnector/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,9 @@ func TestLogsConnectorDetailed(t *testing.T) {
isLogX := `body == "logX"`
isLogY := `body == "logY"`

isBodyString := `IsString(body) == true`
isBodyMap := `IsMap(body) == true`

isScopeCFromLowerContext := `instrumentation_scope.name == "scopeC"`
isScopeDFromLowerContext := `instrumentation_scope.name == "scopeD"`

Expand Down Expand Up @@ -846,6 +849,30 @@ func TestLogsConnectorDetailed(t *testing.T) {
expectSink1: plogutiltest.NewLogs("AB", "CD", "E"),
expectSinkD: plog.Logs{},
},
{
name: "log/with_is_string_condition",
cfg: testConfig(
withRoute("log", isBodyString, idSink0),
withDefault(idSinkD),
),
input: plogutiltest.NewLogs("AB", "CD", "EF"),
expectSink0: plogutiltest.NewLogs("AB", "CD", "EF"),
expectSinkD: plog.Logs{},
},
{
name: "log/with_is_map_condition",
cfg: testConfig(
withRoute("log", isBodyMap, idSink0),
withDefault(idSinkD),
),
input: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A", plogutiltest.Scope("B", setLogRecordMap(plogutiltest.LogRecord("C"), "key", "value"))),
),
expectSink0: plogutiltest.NewLogsFromOpts(
plogutiltest.Resource("A", plogutiltest.Scope("B", setLogRecordMap(plogutiltest.LogRecord("C"), "key", "value"))),
),
expectSinkD: plog.Logs{},
},
}

for _, tt := range testCases {
Expand Down Expand Up @@ -886,3 +913,8 @@ func TestLogsConnectorDetailed(t *testing.T) {
})
}
}

func setLogRecordMap(lr plog.LogRecord, key, value string) plog.LogRecord {
lr.Body().SetEmptyMap().PutStr(key, value)
return lr
}
23 changes: 23 additions & 0 deletions connector/routingconnector/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ func TestMetricsConnectorDetailed(t *testing.T) {
isResourceX := `attributes["resourceName"] == "resourceX"`
isResourceY := `attributes["resourceName"] == "resourceY"`

isResourceString := `IsString(attributes["resourceName"]) == true`
isAttributesMap := `IsMap(attributes) == true`

isMetricE := `name == "metricE"`
isMetricF := `name == "metricF"`
isMetricX := `name == "metricX"`
Expand Down Expand Up @@ -658,6 +661,26 @@ func TestMetricsConnectorDetailed(t *testing.T) {
expectSink1: pmetric.Metrics{},
expectSinkD: pmetric.Metrics{},
},
{
name: "resource/with_is_string_condition",
cfg: testConfig(
withRoute("resource", isResourceString, idSink0),
withDefault(idSinkD),
),
input: pmetricutiltest.NewGauges("AB", "CD", "EF", "GH"),
expectSink0: pmetricutiltest.NewGauges("AB", "CD", "EF", "GH"),
expectSinkD: pmetric.Metrics{},
},
{
name: "resource/with_is_map_condition",
cfg: testConfig(
withRoute("resource", isAttributesMap, idSink0),
withDefault(idSinkD),
),
input: pmetricutiltest.NewGauges("AB", "CD", "EF", "GH"),
expectSink0: pmetricutiltest.NewGauges("AB", "CD", "EF", "GH"),
expectSinkD: pmetric.Metrics{},
},
{
name: "metric/all_match_first_only",
cfg: testConfig(
Expand Down
23 changes: 23 additions & 0 deletions connector/routingconnector/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,9 @@ func TestTracesConnectorDetailed(t *testing.T) {
isResourceX := `attributes["resourceName"] == "resourceX"`
isResourceY := `attributes["resourceName"] == "resourceY"`

isResourceString := `IsString(attributes["resourceName"]) == true`
isAttributesMap := `IsMap(attributes) == true`

isSpanE := `name == "spanE"`
isSpanF := `name == "spanF"`
isSpanX := `name == "spanX"`
Expand Down Expand Up @@ -612,6 +615,26 @@ func TestTracesConnectorDetailed(t *testing.T) {
expectSink1: ptrace.Traces{},
expectSinkD: ptrace.Traces{},
},
{
name: "resource/with_is_string_condition",
cfg: testConfig(
withRoute("resource", isResourceString, idSink0),
withDefault(idSinkD),
),
input: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
expectSink0: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
expectSinkD: ptrace.Traces{},
},
{
name: "resource/with_is_map_condition",
cfg: testConfig(
withRoute("resource", isAttributesMap, idSink0),
withDefault(idSinkD),
),
input: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
expectSink0: ptraceutiltest.NewTraces("AB", "CD", "EF", "GH"),
expectSinkD: ptrace.Traces{},
},
{
name: "span/all_match_first_only",
cfg: testConfig(
Expand Down
Loading