| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package ptrace // import "go.opentelemetry.io/collector/pdata/ptrace" |
| 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 Traces to JSON bytes using the OTLP/JSON format. |
| 14 |
type JSONMarshaler struct{} |
| 15 |
|
| 16 |
// MarshalTraces to the OTLP/JSON format. |
| 17 |
func (*JSONMarshaler) MarshalTraces(td Traces) ([]byte, error) { |
| 18 |
dest := json.BorrowStream(nil) |
| 19 |
defer json.ReturnStream(dest) |
| 20 |
td.getOrig().MarshalJSON(dest) |
| 21 |
if dest.Error() != nil { |
| 22 |
return nil, dest.Error() |
| 23 |
} |
| 24 |
return slices.Clone(dest.Buffer()), nil |
| 25 |
} |
| 26 |
|
| 27 |
// JSONUnmarshaler unmarshals OTLP/JSON formatted-bytes to Traces. |
| 28 |
type JSONUnmarshaler struct{} |
| 29 |
|
| 30 |
// UnmarshalTraces from OTLP/JSON format into Traces. |
| 31 |
func (*JSONUnmarshaler) UnmarshalTraces(buf []byte) (Traces, error) { |
| 32 |
iter := json.BorrowIterator(buf) |
| 33 |
defer json.ReturnIterator(iter) |
| 34 |
td := NewTraces() |
| 35 |
td.getOrig().UnmarshalJSON(iter) |
| 36 |
if iter.Error() != nil { |
| 37 |
return Traces{}, iter.Error() |
| 38 |
} |
| 39 |
otlp.MigrateTraces(td.getOrig().ResourceSpans) |
| 40 |
return td, nil |
| 41 |
} |