| 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/plog" |
| 12 |
) |
| 13 |
|
| 14 |
// MarshalLogs marshals plog.Logs along with the context into a byte slice. |
| 15 |
func MarshalLogs(ctx context.Context, ld plog.Logs) ([]byte, error) { |
| 16 |
lr := internal.LogsRequest{ |
| 17 |
FormatVersion: requestFormatVersion, |
| 18 |
LogsData: internal.LogsToProto(internal.LogsWrapper(ld)), |
| 19 |
RequestContext: encodeContext(ctx), |
| 20 |
} |
| 21 |
buf := make([]byte, lr.SizeProto()) |
| 22 |
lr.MarshalProto(buf) |
| 23 |
return buf, nil |
| 24 |
} |
| 25 |
|
| 26 |
// UnmarshalLogs unmarshals a byte slice into plog.Logs and the context. |
| 27 |
func UnmarshalLogs(buf []byte) (context.Context, plog.Logs, error) { |
| 28 |
ctx := context.Background() |
| 29 |
if !isRequestPayloadV1(buf) { |
| 30 |
return ctx, plog.Logs{}, ErrInvalidFormat |
| 31 |
} |
| 32 |
lr := internal.LogsRequest{} |
| 33 |
if err := lr.UnmarshalProto(buf); err != nil { |
| 34 |
return ctx, plog.Logs{}, fmt.Errorf("failed to unmarshal logs request: %w", err) |
| 35 |
} |
| 36 |
return decodeContext(ctx, lr.RequestContext), plog.Logs(internal.LogsFromProto(lr.LogsData)), nil |
| 37 |
} |