From e20aaf9cec8f8bde7eaf7d4ac1645c6985c97b8d Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Sun, 3 Nov 2024 09:13:23 -0800 Subject: [PATCH] [extension/jsonlogencoding] use goccy/go-json instead of jsoniter --- .chloggen/jsoniter_replacement.yaml | 27 +++++++++++++++++++ .../jsonlogencodingextension/extension.go | 8 +++--- .../encoding/jsonlogencodingextension/go.mod | 3 ++- .../encoding/jsonlogencodingextension/go.sum | 2 ++ .../jsonlogencodingextension/json_test.go | 2 +- 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 .chloggen/jsoniter_replacement.yaml diff --git a/.chloggen/jsoniter_replacement.yaml b/.chloggen/jsoniter_replacement.yaml new file mode 100644 index 000000000000..6cb767b8feec --- /dev/null +++ b/.chloggen/jsoniter_replacement.yaml @@ -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: jsonlogencodingextension + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Use goccy/go-json instead of jsoniter + +# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. +issues: [36154] + +# (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: [] diff --git a/extension/encoding/jsonlogencodingextension/extension.go b/extension/encoding/jsonlogencodingextension/extension.go index 1b3f10c01564..87025988c044 100644 --- a/extension/encoding/jsonlogencodingextension/extension.go +++ b/extension/encoding/jsonlogencodingextension/extension.go @@ -8,7 +8,7 @@ import ( "fmt" "time" - jsoniter "github.com/json-iterator/go" + "github.com/goccy/go-json" "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/plog" @@ -37,7 +37,7 @@ func (e *jsonLogExtension) MarshalLogs(ld plog.Logs) ([]byte, error) { default: return nil, fmt.Errorf("Marshal: Expected 'Map' found '%v'", logRecord.Type().String()) } - buf, err := jsoniter.Marshal(raw) + buf, err := json.Marshal(raw) if err != nil { return nil, err } @@ -49,7 +49,7 @@ func (e *jsonLogExtension) UnmarshalLogs(buf []byte) (plog.Logs, error) { // get json logs from the buffer jsonVal := map[string]any{} - if err := jsoniter.Unmarshal(buf, &jsonVal); err != nil { + if err := json.Unmarshal(buf, &jsonVal); err != nil { return p, err } @@ -96,7 +96,7 @@ func (e *jsonLogExtension) logProcessor(ld plog.Logs) ([]byte, error) { } } - return jsoniter.Marshal(logs) + return json.Marshal(logs) } type logBody struct { diff --git a/extension/encoding/jsonlogencodingextension/go.mod b/extension/encoding/jsonlogencodingextension/go.mod index d66a8026212c..c10326e8a97a 100644 --- a/extension/encoding/jsonlogencodingextension/go.mod +++ b/extension/encoding/jsonlogencodingextension/go.mod @@ -3,7 +3,7 @@ module github.com/open-telemetry/opentelemetry-collector-contrib/extension/encod go 1.22.0 require ( - github.com/json-iterator/go v1.1.12 + github.com/goccy/go-json v0.10.3 github.com/open-telemetry/opentelemetry-collector-contrib/extension/encoding v0.112.0 github.com/stretchr/testify v1.9.0 go.opentelemetry.io/collector/component v0.112.0 @@ -20,6 +20,7 @@ require ( github.com/go-viper/mapstructure/v2 v2.2.1 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect github.com/knadh/koanf/maps v0.1.1 // indirect github.com/knadh/koanf/providers/confmap v0.1.0 // indirect github.com/knadh/koanf/v2 v2.1.1 // indirect diff --git a/extension/encoding/jsonlogencodingextension/go.sum b/extension/encoding/jsonlogencodingextension/go.sum index 028642741c90..ce4e903e3ad7 100644 --- a/extension/encoding/jsonlogencodingextension/go.sum +++ b/extension/encoding/jsonlogencodingextension/go.sum @@ -9,6 +9,8 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss= github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM= +github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA= +github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q= github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= diff --git a/extension/encoding/jsonlogencodingextension/json_test.go b/extension/encoding/jsonlogencodingextension/json_test.go index afb77d24f5b4..e0b735eb3fdf 100644 --- a/extension/encoding/jsonlogencodingextension/json_test.go +++ b/extension/encoding/jsonlogencodingextension/json_test.go @@ -47,7 +47,7 @@ func TestInvalidUnmarshal(t *testing.T) { }, } _, err := e.UnmarshalLogs([]byte("NOT A JSON")) - assert.ErrorContains(t, err, "ReadMapCB: expect { or n, but found N") + assert.ErrorContains(t, err, "expected { character for map value") } func TestPrettyLogProcessor(t *testing.T) {