Skip to content

Commit

Permalink
Rename function to ConvertAttributesToElementsXML
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Oct 10, 2024
1 parent 3108406 commit f4cff45
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .chloggen/elementize-attributes-xml.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ change_type: enhancement
component: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add ElementizeAttributesXML Converter
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]
Expand Down
12 changes: 6 additions & 6 deletions pkg/ottl/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,12 @@ func Test_e2e_converters(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("test", "FooBar")
},
},
{
statement: `set(attributes["test"], ConvertAttributesToElementsXML("<Log id=\"1\"><Message>This is a log message!</Message></Log>"))`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", `<Log><Message>This is a log message!</Message><id>1</id></Log>`)
},
},
{
statement: `set(attributes["test"], Double(1.0))`,
want: func(tCtx ottllog.TransformContext) {
Expand Down Expand Up @@ -377,12 +383,6 @@ func Test_e2e_converters(t *testing.T) {
tCtx.GetLogRecord().Attributes().PutStr("test", "pass")
},
},
{
statement: `set(attributes["test"], ElementizeAttributesXML("<Log id=\"1\"><Message>This is a log message!</Message></Log>"))`,
want: func(tCtx ottllog.TransformContext) {
tCtx.GetLogRecord().Attributes().PutStr("test", `<Log><Message>This is a log message!</Message><id>1</id></Log>`)
},
},
{
statement: `set(attributes["test"], ExtractPatterns("aa123bb", "(?P<numbers>\\d+)"))`,
want: func(tCtx ottllog.TransformContext) {
Expand Down
51 changes: 26 additions & 25 deletions pkg/ottl/ottlfuncs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ Available Converters:
- [Decode](#decode)
- [Concat](#concat)
- [ConvertCase](#convertcase)
- [ConvertAttributesToElementsXML](#convertattributestoelementsxml)
- [Day](#day)
- [Double](#double)
- [Duration](#duration)
Expand Down Expand Up @@ -547,6 +548,31 @@ Examples:

- `ConvertCase(metric.name, "snake")`

### ConvertAttributesToElementsXML

`ConvertAttributesToElementsXML(target, Optional[xpath])`

The `ConvertAttributesToElementsXML` Converter returns an edited version of an XML string where attributes are converted into child elements.

`target` is a Getter that returns a string. This string should be in XML format.
If `target` is not a string, nil, or cannot be parsed as XML, `ConvertAttributesToElementsXML` will return an error.

`xpath` (optional) is a string that specifies an [XPath](https://www.w3.org/TR/1999/REC-xpath-19991116/) expression that
selects one or more elements. Attributes will only be converted within the result(s) of the xpath.

For example, `<a foo="bar"><b>baz</b></a>` will be converted to `<a><b>baz</b><foo>bar</foo></a>`.

Examples:

Convert all attributes in a document

- `ConvertAttributesToElementsXML(body)`

Convert only attributes within "Record" elements

- `ConvertAttributesToElementsXML(body, "/Log/Record")`


### Day

`Day(value)`
Expand Down Expand Up @@ -600,31 +626,6 @@ Examples:
- `Duration("333ms")`
- `Duration("1000000h")`


### ElementizeAttributesXML

`ElementizeAttributesXML(target, Optional[xpath])`

The `ElementizeAttributesXML` Converter returns an edited version of an XML string where attributes are converted into child elements.

`target` is a Getter that returns a string. This string should be in XML format.
If `target` is not a string, nil, or cannot be parsed as XML, `ElementizeAttributesXML` will return an error.

`xpath` (optional) is a string that specifies an [XPath](https://www.w3.org/TR/1999/REC-xpath-19991116/) expression that
selects one or more elements. Attributes will only be converted within the result(s) of the xpath.

For example, `<a foo="bar"><b>baz</b></a>` will be converted to `<a><b>baz</b><foo>bar</foo></a>`.

Examples:

Convert all attributes in a document

- `ElementizeAttributesXML(body)`

Convert only attributes within "Record" elements

- `ElementizeAttributesXML(body, "/Log/Record")`

### ExtractPatterns

`ExtractPatterns(target, pattern)`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

type ElementizeAttributesXMLArguments[K any] struct {
type ConvertAttributesToElementsXMLArguments[K any] struct {
Target ottl.StringGetter[K]
XPath ottl.Optional[string]
}

func NewElementizeAttributesXMLFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("ElementizeAttributesXML", &ElementizeAttributesXMLArguments[K]{}, createElementizeAttributesXMLFunction[K])
func NewConvertAttributesToElementsXMLFactory[K any]() ottl.Factory[K] {
return ottl.NewFactory("ConvertAttributesToElementsXML", &ConvertAttributesToElementsXMLArguments[K]{}, createConvertAttributesToElementsXMLFunction[K])
}

func createElementizeAttributesXMLFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments) (ottl.ExprFunc[K], error) {
args, ok := oArgs.(*ElementizeAttributesXMLArguments[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("ElementizeAttributesXML args must be of type *ElementizeAttributesXMLAguments[K]")
return nil, fmt.Errorf("ConvertAttributesToElementsXML args must be of type *ConvertAttributesToElementsXMLAguments[K]")
}

xPath := args.XPath.Get()
Expand All @@ -36,13 +36,13 @@ func createElementizeAttributesXMLFunction[K any](_ ottl.FunctionContext, oArgs
return nil, err
}

return elementizeAttributesXML(args.Target, xPath), nil
return convertAttributesToElementsXML(args.Target, xPath), nil
}

// elementizeAttributesXML returns a `pcommon.String` that is a result of converting all attributes of the
// 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 elementizeAttributesXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K] {
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

func Test_ElementizeAttributesXML(t *testing.T) {
func Test_ConvertAttributesToElementsXML(t *testing.T) {
tests := []struct {
name string
document string
Expand Down Expand Up @@ -79,15 +79,15 @@ func Test_ElementizeAttributesXML(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
args := &ElementizeAttributesXMLArguments[any]{
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 := createElementizeAttributesXMLFunction[any](ottl.FunctionContext{}, args)
exprFunc, err := createConvertAttributesToElementsXMLFunction[any](ottl.FunctionContext{}, args)
assert.NoError(t, err)

result, err := exprFunc(context.Background(), nil)
Expand All @@ -97,8 +97,8 @@ func Test_ElementizeAttributesXML(t *testing.T) {
}
}

func TestCreateElementizeAttributesXMLFunc(t *testing.T) {
factory := NewElementizeAttributesXMLFactory[any]()
func TestCreateConvertAttributesToElementsXMLFunc(t *testing.T) {
factory := NewConvertAttributesToElementsXMLFactory[any]()
fCtx := ottl.FunctionContext{}

// Invalid arg type
Expand All @@ -108,15 +108,15 @@ func TestCreateElementizeAttributesXMLFunc(t *testing.T) {

// Invalid XPath should error on function creation
exprFunc, err = factory.CreateFunction(
fCtx, &ElementizeAttributesXMLArguments[any]{
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, &ElementizeAttributesXMLArguments[any]{
fCtx, &ConvertAttributesToElementsXMLArguments[any]{
Target: invalidXMLGetter(),
})
assert.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ottl/ottlfuncs/functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ func converters[K any]() []ottl.Factory[K] {
NewDecodeFactory[K](),
NewConcatFactory[K](),
NewConvertCaseFactory[K](),
NewConvertAttributesToElementsXMLFactory[K](),
NewDayFactory[K](),
NewDoubleFactory[K](),
NewDurationFactory[K](),
NewElementizeAttributesXMLFactory[K](),
NewExtractPatternsFactory[K](),
NewExtractGrokPatternsFactory[K](),
NewFnvFactory[K](),
Expand Down

0 comments on commit f4cff45

Please sign in to comment.