Skip to content

Commit

Permalink
Merge branch 'main' into ottl-xml-rename
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski committed Sep 20, 2024
2 parents 28b03fe + f02bdf9 commit 7f97a6e
Show file tree
Hide file tree
Showing 40 changed files with 263 additions and 150 deletions.
27 changes: 27 additions & 0 deletions .chloggen/ottl-faster-json.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: pkg/ottl

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Improved JSON unmarshaling performance by 10-20% by switching dependencies.

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

# (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: []
1 change: 1 addition & 0 deletions connector/countconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions connector/countconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions connector/routingconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions connector/routingconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions connector/sumconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions connector/sumconnector/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions exporter/honeycombmarkerexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/snappy v0.0.4 // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporter/honeycombmarkerexporter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/filter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.1.0 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions internal/filter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions pkg/ottl/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"strconv"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/goccy/go-json"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/internal/ottlcommon"
Expand Down Expand Up @@ -429,22 +429,25 @@ func (g StandardStringLikeGetter[K]) Get(ctx context.Context, tCtx K) (*string,
case []byte:
result = hex.EncodeToString(v)
case pcommon.Map:
result, err = jsoniter.MarshalToString(v.AsRaw())
resultBytes, err := json.Marshal(v.AsRaw())
if err != nil {
return nil, err
}
result = string(resultBytes)
case pcommon.Slice:
result, err = jsoniter.MarshalToString(v.AsRaw())
resultBytes, err := json.Marshal(v.AsRaw())
if err != nil {
return nil, err
}
result = string(resultBytes)
case pcommon.Value:
result = v.AsString()
default:
result, err = jsoniter.MarshalToString(v)
resultBytes, err := json.Marshal(v)
if err != nil {
return nil, TypeError(fmt.Sprintf("unsupported type: %T", v))
}
result = string(resultBytes)
}
return &result, nil
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/ottl/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ require (
github.com/antchfx/xpath v1.3.1
github.com/elastic/go-grok v0.3.1
github.com/gobwas/glob v0.2.3
github.com/goccy/go-json v0.10.3
github.com/google/uuid v1.6.0
github.com/iancoleman/strcase v0.3.0
github.com/json-iterator/go v1.1.12
github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal v0.109.0
github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.109.0
github.com/stretchr/testify v1.9.0
Expand All @@ -35,6 +35,7 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/magefile/mage v1.15.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions pkg/ottl/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/ottl/ottlfuncs/func_parse_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"
"fmt"

jsoniter "github.com/json-iterator/go"
"github.com/goccy/go-json"
"go.opentelemetry.io/collector/pdata/pcommon"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand Down Expand Up @@ -47,7 +47,7 @@ func parseJSON[K any](target ottl.StringGetter[K]) ottl.ExprFunc[K] {
return nil, err
}
var parsedValue any
err = jsoniter.UnmarshalFromString(targetVal, &parsedValue)
err = json.Unmarshal([]byte(targetVal), &parsedValue)
if err != nil {
return nil, err
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/ottl/ottlfuncs/func_parse_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,63 @@ func Test_ParseJSON_Error(t *testing.T) {
_, err := exprFunc(context.Background(), nil)
assert.Error(t, err)
}

const benchData = `{
"_id": "667cb0db02f4dfc7648b0f6b",
"index": 0,
"guid": "2e419732-8214-4e36-a158-d3ced0217ab6",
"isActive": true,
"balance": "$1,105.05",
"picture": "http://example.com/1",
"age": 22,
"eyeColor": "blue",
"name": "Vincent Knox",
"gender": "male",
"company": "ANIVET",
"email": "vincentknox@anivet.com",
"phone": "+1 (914) 599-2454",
"address": "483 Gerritsen Avenue, Succasunna, Massachusetts, 7803",
"about": "Elit aliqua qui amet duis esse eiusmod cillum proident quis amet elit tempor dolor exercitation. Eu ut tempor exercitation excepteur est. Lorem ad elit sit reprehenderit quis ad sunt laborum amet veniam commodo sit sunt aliqua. Sint incididunt eu ut est magna amet mollit qui deserunt nostrud labore ad. Nostrud officia proident occaecat et irure ut quis culpa mollit veniam. Laboris labore ea reprehenderit veniam mollit enim et proident ipsum id. In qui sit officia laborum.\r\nIn ad consectetur duis ad nisi proident. Non in officia do mollit amet sint voluptate minim nostrud voluptate elit. Veniam Lorem cillum fugiat adipisicing qui ea commodo irure tempor ipsum pariatur sit voluptate. Eiusmod cillum occaecat excepteur cillum aliquip laboris velit aute proident amet.\r\nIpsum sunt eiusmod do ut voluptate sit anim. Consequat nisi nisi consequat amet excepteur ea ad incididunt pariatur veniam exercitation eu ex in. Incididunt sint tempor pariatur Lorem do. Occaecat laborum ad ad id enim dolor deserunt ipsum amet Lorem Lorem. Cillum veniam labore eu do duis.\r\nCillum dolor eiusmod sit amet commodo voluptate pariatur ex irure eu culpa sunt. Incididunt non exercitation est pariatur est. Incididunt mollit Lorem velit ullamco excepteur esse quis id magna et ullamco labore. Laboris consequat tempor est ea amet enim et nisi amet officia dolore magna veniam. Nostrud officia consectetur ea culpa laborum et ut Lorem laboris.\r\nDeserunt labore ullamco dolor exercitation laboris consectetur nulla cupidatat duis. Occaecat quis velit deserunt culpa nostrud eiusmod elit fugiat nulla duis deserunt Lorem do. Proident anim proident aute amet pariatur et do irure. Ad magna qui elit consequat sit exercitation sit. Magna adipisicing id esse aliqua officia magna. Et veniam aliqua minim reprehenderit in culpa. Adipisicing quis eu do Lorem cupidatat consequat ad aute quis.\r\nIn aliquip ea laborum esse dolor reprehenderit qui sit culpa occaecat. Consectetur Lorem dolore adipisicing amet incididunt. Dolor veniam Lorem nulla ex. Eiusmod amet tempor sit eiusmod do reprehenderit proident sit commodo elit cupidatat.\r\nNulla nulla consequat cillum mollit tempor eiusmod irure deserunt amet et voluptate. Fugiat et veniam culpa eiusmod minim ex pariatur. Eiusmod adipisicing pariatur pariatur adipisicing in consequat cillum ut qui veniam amet incididunt ullamco anim.\r\nDolor nulla laborum tempor adipisicing qui id. Exercitation labore aliqua ut laborum velit cupidatat officia. Est qui dolor sint laboris aliqua ea nulla culpa.\r\nAute reprehenderit nulla elit nisi reprehenderit pariatur officia veniam dolore ea occaecat nostrud sunt fugiat. Cillum consequat labore nostrud veniam nisi ea proident est officia incididunt adipisicing qui sint nisi. Ad enim reprehenderit minim labore minim irure dolor. Voluptate commodo dolor excepteur est tempor dolor sunt esse fugiat ea eu et.\r\nIpsum sit velit deserunt aliqua eu labore ad esse eu. Duis eiusmod non exercitation consequat nulla. Enim elit consectetur pariatur sunt labore sunt dolore non do. Sint consequat aliqua tempor consectetur veniam minim. Veniam eu aute occaecat consectetur dolore ullamco dolore officia.\r\n",
"registered": "2023-06-08T12:29:06 +07:00",
"latitude": -59.802339,
"longitude": -160.473187,
"tags": [
"pariatur",
"anim",
"id",
"duis",
"fugiat",
"qui",
"veniam"
],
"friends": [
{
"id": 0,
"name": "Hester Bruce"
},
{
"id": 1,
"name": "Laurel Mcknight"
},
{
"id": 2,
"name": "Wynn Moses"
}
],
"greeting": "Hello, Vincent Knox! You have 1 unread messages.",
"favoriteFruit": "apple"
}`

func BenchmarkParseJSON(b *testing.B) {
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := parseJSON(ottl.StandardStringGetter[any]{
Getter: func(_ context.Context, _ any) (any, error) {
return benchData, nil
},
})(ctx, nil)
require.NoError(b, err)
}
}
45 changes: 30 additions & 15 deletions pkg/ottl/ottlfuncs/func_remove_xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,22 @@ func createRemoveXMLFunction[K any](_ ottl.FunctionContext, oArgs ottl.Arguments
return removeXML(args.Target, args.XPath), nil
}

// removeXML returns a `pcommon.String` that is a result of renaming the target XML or s
// removeXML returns a `pcommon.String` that is a result of removing all matching nodes from the target XML.
// This currently supports removal of elements, attributes, text values, comments, and CharData.
func removeXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K] {
return func(ctx context.Context, tCtx K) (any, error) {
targetVal, err := target.Get(ctx, tCtx)
if err != nil {
if err := validateXPath(xPath); err != nil {
return nil, err
}

// Validate the xpath
_, err = xpath.Compile(xPath)
targetVal, err := target.Get(ctx, tCtx)
if err != nil {
return nil, fmt.Errorf("invalid xpath: %w", err)
return nil, err
}

// Check if the xml starts with a declaration node
preserveDeclearation := strings.HasPrefix(targetVal, "<?xml")

top, err := xmlquery.Parse(strings.NewReader(targetVal))
top, err := parseNodesXML(targetVal)
if err != nil {
return nil, fmt.Errorf("parse xml: %w", err)
}

if !preserveDeclearation {
xmlquery.RemoveFromTree(top.FirstChild)
return nil, err
}

xmlquery.FindEach(top, xPath, func(_ int, n *xmlquery.Node) {
Expand All @@ -77,3 +69,26 @@ func removeXML[K any](target ottl.StringGetter[K], xPath string) ottl.ExprFunc[K
return top.OutputXML(false), nil
}
}

func validateXPath(xPath string) error {
_, err := xpath.Compile(xPath)
if err != nil {
return fmt.Errorf("invalid xpath: %w", err)
}
return nil
}

// Aside from parsing the XML document, this function also ensures that
// the XML declaration is included in the result only if it was present in
// the original document.
func parseNodesXML(targetVal string) (*xmlquery.Node, error) {
preserveDeclearation := strings.HasPrefix(targetVal, "<?xml")
top, err := xmlquery.Parse(strings.NewReader(targetVal))
if err != nil {
return nil, fmt.Errorf("parse xml: %w", err)
}
if !preserveDeclearation {
xmlquery.RemoveFromTree(top.FirstChild)
}
return top, nil
}
Loading

0 comments on commit 7f97a6e

Please sign in to comment.