| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package request // import "go.opentelemetry.io/collector/pdata/xpdata/request" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"fmt" |
| 9 |
|
| 10 |
"go.opentelemetry.io/collector/pdata/internal" |
| 11 |
"go.opentelemetry.io/collector/pdata/ptrace" |
| 12 |
) |
| 13 |
|
| 14 |
// MarshalTraces marshals ptrace.Traces along with the context into a byte slice. |
| 15 |
func MarshalTraces(ctx context.Context, ld ptrace.Traces) ([]byte, error) { |
| 16 |
tr := internal.TracesRequest{ |
| 17 |
FormatVersion: requestFormatVersion, |
| 18 |
TracesData: internal.TracesToProto(internal.TracesWrapper(ld)), |
| 19 |
RequestContext: encodeContext(ctx), |
| 20 |
} |
| 21 |
buf := make([]byte, tr.SizeProto()) |
| 22 |
tr.MarshalProto(buf) |
| 23 |
return buf, nil |
| 24 |
} |
| 25 |
|
| 26 |
// UnmarshalTraces unmarshals a byte slice into ptrace.Traces and the context. |
| 27 |
func UnmarshalTraces(buf []byte) (context.Context, ptrace.Traces, error) { |
| 28 |
ctx := context.Background() |
| 29 |
if !isRequestPayloadV1(buf) { |
| 30 |
return ctx, ptrace.Traces{}, ErrInvalidFormat |
| 31 |
} |
| 32 |
tr := internal.TracesRequest{} |
| 33 |
if err := tr.UnmarshalProto(buf); err != nil { |
| 34 |
return ctx, ptrace.Traces{}, fmt.Errorf("failed to unmarshal traces request: %w", err) |
| 35 |
} |
| 36 |
return decodeContext(ctx, tr.RequestContext), ptrace.Traces(internal.TracesFromProto(tr.TracesData)), nil |
| 37 |
} |