Debian Codegraph @

pdata/plog/json.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package plog // import "go.opentelemetry.io/collector/pdata/plog"
5
6 import (
7 "slices"
8
9 "go.opentelemetry.io/collector/pdata/internal/json"
10 "go.opentelemetry.io/collector/pdata/internal/otlp"
11 )
12
13 // JSONMarshaler marshals Logs to JSON bytes using the OTLP/JSON format.
14 type JSONMarshaler struct{}
15
16 // MarshalLogs to the OTLP/JSON format.
17 func (*JSONMarshaler) MarshalLogs(ld Logs) ([]byte, error) {
18 dest := json.BorrowStream(nil)
19 defer json.ReturnStream(dest)
20 ld.getOrig().MarshalJSON(dest)
21 if dest.Error() != nil {
22 return nil, dest.Error()
23 }
24 return slices.Clone(dest.Buffer()), nil
25 }
26
27 var _ Unmarshaler = (*JSONUnmarshaler)(nil)
28
29 // JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to Logs.
30 type JSONUnmarshaler struct{}
31
32 // UnmarshalLogs from OTLP/JSON format into Logs.
33 func (*JSONUnmarshaler) UnmarshalLogs(buf []byte) (Logs, error) {
34 iter := json.BorrowIterator(buf)
35 defer json.ReturnIterator(iter)
36 ld := NewLogs()
37 ld.getOrig().UnmarshalJSON(iter)
38 if iter.Error() != nil {
39 return Logs{}, iter.Error()
40 }
41 otlp.MigrateLogs(ld.getOrig().ResourceLogs)
42 return ld, nil
43 }