Skip to content

Commit

Permalink
[extension/jsonlogencoding] use goccy/go-json instead of jsoniter
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Nov 3, 2024
1 parent 8dd2b7b commit e20aaf9
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 6 deletions.
27 changes: 27 additions & 0 deletions .chloggen/jsoniter_replacement.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: 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: []
8 changes: 4 additions & 4 deletions extension/encoding/jsonlogencodingextension/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}

Expand Down Expand Up @@ -96,7 +96,7 @@ func (e *jsonLogExtension) logProcessor(ld plog.Logs) ([]byte, error) {
}
}

return jsoniter.Marshal(logs)
return json.Marshal(logs)
}

type logBody struct {
Expand Down
3 changes: 2 additions & 1 deletion extension/encoding/jsonlogencodingextension/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions extension/encoding/jsonlogencodingextension/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 extension/encoding/jsonlogencodingextension/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit e20aaf9

Please sign in to comment.