Skip to content

Commit

Permalink
Faster json parser (#33785)
Browse files Browse the repository at this point in the history
**Description:** 
Switch libraries to reduce the footprint of the JSON parser.

**Link to tracking Issue:**
Fixes #33784 

**Testing:**

Before:
```
goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json
BenchmarkProcess-10        49047             23951 ns/op           26277 B/op        177 allocs/op
BenchmarkProcess-10        50248             24002 ns/op           26275 B/op        177 allocs/op
BenchmarkProcess-10        50258             24517 ns/op           26276 B/op        177 allocs/op
BenchmarkProcess-10        50505             24731 ns/op           26276 B/op        177 allocs/op
BenchmarkProcess-10        45708             24730 ns/op           26276 B/op        177 allocs/op
BenchmarkProcess-10        50022             25021 ns/op           26277 B/op        177 allocs/op
BenchmarkProcess-10        47204             24794 ns/op           26275 B/op        177 allocs/op
BenchmarkProcess-10        47742             25335 ns/op           26274 B/op        177 allocs/op
BenchmarkProcess-10        46252             25205 ns/op           26276 B/op        177 allocs/op
BenchmarkProcess-10        47916             24379 ns/op           26277 B/op        177 allocs/op
PASS
ok      github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json       15.559s
```

After:
```
goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json
BenchmarkProcess-10        95613             12168 ns/op           14702 B/op        115 allocs/op
BenchmarkProcess-10        97904             12343 ns/op           14704 B/op        115 allocs/op
BenchmarkProcess-10        99006             12187 ns/op           14702 B/op        115 allocs/op
BenchmarkProcess-10        96964             12310 ns/op           14703 B/op        115 allocs/op
BenchmarkProcess-10        98661             12285 ns/op           14703 B/op        115 allocs/op
BenchmarkProcess-10        96896             12356 ns/op           14703 B/op        115 allocs/op
BenchmarkProcess-10        94118             12367 ns/op           14703 B/op        115 allocs/op
BenchmarkProcess-10        96123             12349 ns/op           14702 B/op        115 allocs/op
BenchmarkProcess-10        96948             12305 ns/op           14702 B/op        115 allocs/op
BenchmarkProcess-10        96626             12225 ns/op           14702 B/op        115 allocs/op
PASS
ok      github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json       13.619s
```

Benchstat:
```
goos: darwin
goarch: arm64
pkg: github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json
           │ before.txt  │              after.txt              │
           │   sec/op    │   sec/op     vs base                │
Process-10   24.73µ ± 3%   12.31µ ± 1%  -50.23% (p=0.000 n=10)

           │  before.txt  │              after.txt               │
           │     B/op     │     B/op      vs base                │
Process-10   25.66Ki ± 0%   14.36Ki ± 0%  -44.05% (p=0.000 n=10)

           │ before.txt │             after.txt              │
           │ allocs/op  │ allocs/op   vs base                │
Process-10   177.0 ± 0%   115.0 ± 0%  -35.03% (p=0.000 n=10)
```
  • Loading branch information
atoulme authored Jun 28, 2024
1 parent 450b088 commit e004ecd
Show file tree
Hide file tree
Showing 45 changed files with 157 additions and 21 deletions.
27 changes: 27 additions & 0 deletions .chloggen/faster_json_parser.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/stanza

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Switch JSON parser used by json_parser to github.com/goccy/go-json

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

# (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: 1 addition & 1 deletion cmd/otelcontribcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/go-zookeeper/zk v1.0.3 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gocql/gocql v1.6.0 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/godbus/dbus/v5 v5.0.6 // indirect
Expand Down
4 changes: 2 additions & 2 deletions cmd/otelcontribcol/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 cmd/oteltestbedcol/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/go-zookeeper/zk v1.0.3 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions cmd/oteltestbedcol/go.sum

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

2 changes: 1 addition & 1 deletion connector/datadogconnector/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ require (
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/godbus/dbus/v5 v5.0.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
4 changes: 2 additions & 2 deletions connector/datadogconnector/go.sum

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

2 changes: 1 addition & 1 deletion exporter/datadogexporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ require (
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/go-zookeeper/zk v1.0.3 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/godbus/dbus/v5 v5.0.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
Expand Down
4 changes: 2 additions & 2 deletions exporter/datadogexporter/go.sum

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

2 changes: 1 addition & 1 deletion exporter/datadogexporter/integrationtest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ require (
github.com/go-openapi/swag v0.22.9 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/godbus/dbus/v5 v5.0.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
4 changes: 2 additions & 2 deletions exporter/datadogexporter/integrationtest/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/elasticsearchexporter/integrationtest/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ require (
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/gobwas/glob v0.2.3 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/googleapis v1.4.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
2 changes: 2 additions & 0 deletions exporter/elasticsearchexporter/integrationtest/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 pkg/stanza/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/cespare/xxhash/v2 v2.3.0
github.com/expr-lang/expr v1.16.9
github.com/fsnotify/fsnotify v1.7.0
github.com/goccy/go-json v0.10.3
github.com/jpillora/backoff v1.0.0
github.com/json-iterator/go v1.1.12
github.com/leodido/go-syslog/v4 v4.1.0
Expand Down
2 changes: 2 additions & 0 deletions pkg/stanza/go.sum

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

2 changes: 0 additions & 2 deletions pkg/stanza/operator/parser/json/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package json // import "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/parser/json"

import (
jsoniter "github.com/json-iterator/go"
"go.opentelemetry.io/collector/component"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator"
Expand Down Expand Up @@ -43,6 +42,5 @@ func (c Config) Build(set component.TelemetrySettings) (operator.Operator, error

return &Parser{
ParserOperator: parserOperator,
json: jsoniter.ConfigFastest,
}, nil
}
5 changes: 2 additions & 3 deletions pkg/stanza/operator/parser/json/parser.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"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/entry"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/stanza/operator/helper"
Expand All @@ -16,7 +16,6 @@ import (
// Parser is an operator that parses JSON.
type Parser struct {
helper.ParserOperator
json jsoniter.API
}

// Process will parse an entry for JSON.
Expand All @@ -29,7 +28,7 @@ func (p *Parser) parse(value any) (any, error) {
var parsedValue map[string]any
switch m := value.(type) {
case string:
err := p.json.UnmarshalFromString(m, &parsedValue)
err := json.Unmarshal([]byte(m), &parsedValue)
if err != nil {
return nil, err
}
Expand Down
24 changes: 23 additions & 1 deletion pkg/stanza/operator/parser/json/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package json

import (
"context"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -46,7 +48,7 @@ func TestParserStringFailure(t *testing.T) {
parser := newTestParser(t)
_, err := parser.parse("invalid")
require.Error(t, err)
require.Contains(t, err.Error(), "error found in #1 byte")
require.Contains(t, err.Error(), "expected { character for map value")
}

func TestParserByteFailure(t *testing.T) {
Expand Down Expand Up @@ -164,3 +166,23 @@ func TestParser(t *testing.T) {
})
}
}

func BenchmarkProcess(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()

cfg := NewConfig()

parser, err := cfg.Build(componenttest.NewNopTelemetrySettings())
require.NoError(b, err)

body, err := os.ReadFile(filepath.Join("testdata", "testdata.json"))
require.NoError(b, err)

e := entry.Entry{Body: string(body)}

for i := 0; i < b.N; i++ {
err := parser.Process(context.Background(), &e)
require.NoError(b, err)
}
}
45 changes: 45 additions & 0 deletions pkg/stanza/operator/parser/json/testdata/testdata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"_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"
}
1 change: 1 addition & 0 deletions processor/logstransformprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // 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/logstransformprocessor/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 receiver/azureeventhubreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/goccy/go-json v0.10.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang-jwt/jwt/v4 v4.4.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
Expand Down
2 changes: 2 additions & 0 deletions receiver/azureeventhubreceiver/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 receiver/filelogreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // 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
Loading

0 comments on commit e004ecd

Please sign in to comment.