forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pkg/ottl] Add ConvertAttributesToElementsXML Converter (open-telemet…
…ry#35328) This adds a converter called `ElementizeAttributesXML`. This serves as one of the granular transformations described in open-telemetry#35281 which will allow users to migrate any arbitrary XML document into a JSON-equivalent state. Also see open-telemetry#35364
- Loading branch information
1 parent
2d2aae4
commit d7e5154
Showing
6 changed files
with
255 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: pkg/ottl | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Add ConvertAttributesToElementsXML Converter | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [35328] | ||
|
||
# (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
69 changes: 69 additions & 0 deletions
69
pkg/ottl/ottlfuncs/func_convert_attributes_to_elements_xml.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,69 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/antchfx/xmlquery" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
type ConvertAttributesToElementsXMLArguments[K any] struct { | ||
Target ottl.StringGetter[K] | ||
XPath ottl.Optional[string] | ||
} | ||
|
||
func NewConvertAttributesToElementsXMLFactory[K any]() ottl.Factory[K] { | ||
return ottl.NewFactory("ConvertAttributesToElementsXML", &ConvertAttributesToElementsXMLArguments[K]{}, createConvertAttributesToElementsXMLFunction[K]) | ||
} | ||
|
||
func createConvertAttributesToElementsXMLFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) { | ||
args, ok := oArgs.(*ConvertAttributesToElementsXMLArguments[K]) | ||
|
||
if !ok { | ||
return nil, fmt.Errorf("ConvertAttributesToElementsXML args must be of type *ConvertAttributesToElementsXMLAguments[K]") | ||
} | ||
|
||
xPath := args.XPath.Get() | ||
if xPath == "" { | ||
xPath = "//@*" // All attributes in the document | ||
} | ||
if err := validateXPath(xPath); err != nil { | ||
return nil, err | ||
} | ||
|
||
return convertAttributesToElementsXML(args.Target, xPath), nil | ||
} | ||
|
||
// convertAttributesToElementsXML returns a string that is a result of converting all attributes of the | ||
// target XML into child elements. These new elements are added as the last child elements of the parent. | ||
// e.g. <a foo="bar" hello="world"><b/></a> -> <a><hello>world</hello><foo>bar</foo><b/></a> | ||
func convertAttributesToElementsXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K] { | ||
return func(ctx context.Context, tCtx K) (any, error) { | ||
var doc *xmlquery.Node | ||
if targetVal, err := target.Get(ctx, tCtx); err != nil { | ||
return nil, err | ||
} else if doc, err = parseNodesXML(targetVal); err != nil { | ||
return nil, err | ||
} | ||
for _, n := range xmlquery.Find(doc, xPath) { | ||
if n.Type != xmlquery.AttributeNode { | ||
continue | ||
} | ||
xmlquery.AddChild(n.Parent, &xmlquery.Node{ | ||
Type: xmlquery.ElementNode, | ||
Data: n.Data, | ||
FirstChild: &xmlquery.Node{ | ||
Type: xmlquery.TextNode, | ||
Data: n.InnerText(), | ||
}, | ||
}) | ||
n.Parent.RemoveAttr(n.Data) | ||
} | ||
return doc.OutputXML(false), nil | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
pkg/ottl/ottlfuncs/func_convert_attributes_to_elements_xml_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,126 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package ottlfuncs // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/ottlfuncs" | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" | ||
) | ||
|
||
func Test_ConvertAttributesToElementsXML(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
document string | ||
xPath string | ||
want string | ||
}{ | ||
{ | ||
name: "nop", | ||
document: `<a><b/></a>`, | ||
want: `<a><b></b></a>`, | ||
}, | ||
{ | ||
name: "nop declaration", | ||
document: `<?xml version="1.0" encoding="UTF-8"?><a><b/></a>`, | ||
want: `<?xml version="1.0" encoding="UTF-8"?><a><b></b></a>`, | ||
}, | ||
{ | ||
name: "single attribute", | ||
document: `<a foo="bar"/>`, | ||
want: `<a><foo>bar</foo></a>`, | ||
}, | ||
{ | ||
name: "multiple attributes - order 1", | ||
document: `<a foo="bar" hello="world"/>`, | ||
want: `<a><foo>bar</foo><hello>world</hello></a>`, | ||
}, | ||
{ | ||
name: "multiple attributes - order 2", | ||
document: `<a hello="world" foo="bar"/>`, | ||
want: `<a><hello>world</hello><foo>bar</foo></a>`, | ||
}, | ||
{ | ||
name: "with child elements", | ||
document: `<a hello="world" foo="bar"><b/><c/><b/></a>`, | ||
want: `<a><b></b><c></c><b></b><hello>world</hello><foo>bar</foo></a>`, | ||
}, | ||
{ | ||
name: "with child value", | ||
document: `<a hello="world" foo="bar">free value</a>`, | ||
want: `<a>free value<hello>world</hello><foo>bar</foo></a>`, | ||
}, | ||
{ | ||
name: "with child elements and values", | ||
document: `<a hello="world" foo="bar">free value<b/>2<c/></a>`, | ||
want: `<a>free value<b></b>2<c></c><hello>world</hello><foo>bar</foo></a>`, | ||
}, | ||
{ | ||
name: "multiple levels", | ||
document: `<a hello="world" foo="bar"><b href="www.example.com"></b></a>`, | ||
want: `<a><b><href>www.example.com</href></b><hello>world</hello><foo>bar</foo></a>`, | ||
}, | ||
{ | ||
name: "xpath filtered", | ||
document: `<a hello="world" foo="bar"><b href="www.example.com"></b></a>`, | ||
xPath: "/a/b/@*", // only convert attributes of b | ||
want: `<a hello="world" foo="bar"><b><href>www.example.com</href></b></a>`, | ||
}, | ||
{ | ||
name: "attributes found with non-attributes xpath", | ||
document: `<a hello="world" foo="bar"><b href="www.example.com"></b></a>`, | ||
xPath: "/a/b", // convert b (the attributes of b, even though the element b was selected) | ||
want: `<a hello="world" foo="bar"><b href="www.example.com"></b></a>`, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
args := &ConvertAttributesToElementsXMLArguments[any]{ | ||
Target: ottl.StandardStringGetter[any]{ | ||
Getter: func(_ context.Context, _ any) (any, error) { | ||
return tt.document, nil | ||
}, | ||
}, | ||
XPath: ottl.NewTestingOptional(tt.xPath), | ||
} | ||
exprFunc, err := createConvertAttributesToElementsXMLFunction[any](ottl.FunctionContext{}, args) | ||
assert.NoError(t, err) | ||
|
||
result, err := exprFunc(context.Background(), nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tt.want, result) | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateConvertAttributesToElementsXMLFunc(t *testing.T) { | ||
factory := NewConvertAttributesToElementsXMLFactory[any]() | ||
fCtx := ottl.FunctionContext{} | ||
|
||
// Invalid arg type | ||
exprFunc, err := factory.CreateFunction(fCtx, nil) | ||
assert.Error(t, err) | ||
assert.Nil(t, exprFunc) | ||
|
||
// Invalid XPath should error on function creation | ||
exprFunc, err = factory.CreateFunction( | ||
fCtx, &ConvertAttributesToElementsXMLArguments[any]{ | ||
XPath: ottl.NewTestingOptional("!"), | ||
}) | ||
assert.Error(t, err) | ||
assert.Nil(t, exprFunc) | ||
|
||
// Invalid XML should error on function execution | ||
exprFunc, err = factory.CreateFunction( | ||
fCtx, &ConvertAttributesToElementsXMLArguments[any]{ | ||
Target: invalidXMLGetter(), | ||
}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, exprFunc) | ||
_, err = exprFunc(context.Background(), nil) | ||
assert.Error(t, err) | ||
} |
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