Skip to content

Commit

Permalink
[chore][pkg/ottl] Use faster json parsing library (open-telemetry#35130)
Browse files Browse the repository at this point in the history
This follows
open-telemetry#33785
by switching to a faster json library.
  • Loading branch information
djaglowski authored Sep 19, 2024
1 parent 07ee9e4 commit 58c9d51
Show file tree
Hide file tree
Showing 28 changed files with 133 additions and 7 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 @@ -29,6 +29,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/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // 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 @@ -24,6 +24,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/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // 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 @@ -25,6 +25,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/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // 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 @@ -32,6 +32,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/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // 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 @@ -28,6 +28,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/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.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 @@ -6,9 +6,9 @@ require (
github.com/alecthomas/participle/v2 v2.1.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 @@ -32,6 +32,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gogo/protobuf v1.3.2 // 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": "[email protected]",
"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)
}
}
1 change: 1 addition & 0 deletions processor/attributesprocessor/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/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/attributesprocessor/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 processor/filterprocessor/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/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/filterprocessor/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 processor/routingprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/routingprocessor/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 processor/spanprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,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/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions processor/spanprocessor/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 processor/tailsamplingprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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/hashicorp/go-version v1.7.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
Expand Down
Loading

0 comments on commit 58c9d51

Please sign in to comment.