From 4296b17545a579ee10b12ffdc50d69819afe8a0d Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Sat, 9 Aug 2025 22:38:57 -0700 Subject: [PATCH] Avoid unnecessary buffer copy when JSON marshal fails. Signed-off-by: Bogdan Drutu --- .chloggen/json-marshal.yaml | 25 +++++++++++++++++++++++++ pdata/plog/json.go | 5 ++++- pdata/pmetric/json.go | 5 ++++- pdata/pprofile/json.go | 5 ++++- pdata/ptrace/json.go | 5 ++++- 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 .chloggen/json-marshal.yaml diff --git a/.chloggen/json-marshal.yaml b/.chloggen/json-marshal.yaml new file mode 100644 index 000000000000..4b8af1fcec66 --- /dev/null +++ b/.chloggen/json-marshal.yaml @@ -0,0 +1,25 @@ +# 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. otlpreceiver) +component: pdata + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Avoid unnecessary buffer copy when JSON marshal fails. + +# One or more tracking issues or pull requests related to the change +issues: [13598] + +# (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: + +# 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: [api, user] diff --git a/pdata/plog/json.go b/pdata/plog/json.go index ac66c96c76ef..ea449870311c 100644 --- a/pdata/plog/json.go +++ b/pdata/plog/json.go @@ -18,7 +18,10 @@ func (*JSONMarshaler) MarshalLogs(ld Logs) ([]byte, error) { dest := json.BorrowStream(nil) defer json.ReturnStream(dest) ld.marshalJSONStream(dest) - return slices.Clone(dest.Buffer()), dest.Error() + if dest.Error() != nil { + return nil, dest.Error() + } + return slices.Clone(dest.Buffer()), nil } var _ Unmarshaler = (*JSONUnmarshaler)(nil) diff --git a/pdata/pmetric/json.go b/pdata/pmetric/json.go index 904e0860c8a6..61113180c205 100644 --- a/pdata/pmetric/json.go +++ b/pdata/pmetric/json.go @@ -20,7 +20,10 @@ func (*JSONMarshaler) MarshalMetrics(md Metrics) ([]byte, error) { dest := json.BorrowStream(nil) defer json.ReturnStream(dest) md.marshalJSONStream(dest) - return slices.Clone(dest.Buffer()), dest.Error() + if dest.Error() != nil { + return nil, dest.Error() + } + return slices.Clone(dest.Buffer()), nil } // JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to Metrics. diff --git a/pdata/pprofile/json.go b/pdata/pprofile/json.go index d1997fd7664b..bfb66e2ce61f 100644 --- a/pdata/pprofile/json.go +++ b/pdata/pprofile/json.go @@ -18,7 +18,10 @@ func (*JSONMarshaler) MarshalProfiles(pd Profiles) ([]byte, error) { dest := json.BorrowStream(nil) defer json.ReturnStream(dest) pd.marshalJSONStream(dest) - return slices.Clone(dest.Buffer()), dest.Error() + if dest.Error() != nil { + return nil, dest.Error() + } + return slices.Clone(dest.Buffer()), nil } // JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to pprofile.Profiles. diff --git a/pdata/ptrace/json.go b/pdata/ptrace/json.go index 07a7d6b37945..94cd6328a906 100644 --- a/pdata/ptrace/json.go +++ b/pdata/ptrace/json.go @@ -18,7 +18,10 @@ func (*JSONMarshaler) MarshalTraces(td Traces) ([]byte, error) { dest := json.BorrowStream(nil) defer json.ReturnStream(dest) td.marshalJSONStream(dest) - return slices.Clone(dest.Buffer()), dest.Error() + if dest.Error() != nil { + return nil, dest.Error() + } + return slices.Clone(dest.Buffer()), nil } // JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to Traces.