| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package plogotlp // import "go.opentelemetry.io/collector/pdata/plog/plogotlp" |
| 5 |
|
| 6 |
import ( |
| 7 |
"slices" |
| 8 |
|
| 9 |
"go.opentelemetry.io/collector/pdata/internal" |
| 10 |
"go.opentelemetry.io/collector/pdata/internal/json" |
| 11 |
"go.opentelemetry.io/collector/pdata/internal/otlp" |
| 12 |
"go.opentelemetry.io/collector/pdata/plog" |
| 13 |
) |
| 14 |
|
| 15 |
// ExportRequest represents the request for gRPC/HTTP client/server. |
| 16 |
// It's a wrapper for plog.Logs data. |
| 17 |
type ExportRequest struct { |
| 18 |
orig *internal.ExportLogsServiceRequest |
| 19 |
state *internal.State |
| 20 |
} |
| 21 |
|
| 22 |
// NewExportRequest returns an empty ExportRequest. |
| 23 |
func NewExportRequest() ExportRequest { |
| 24 |
return ExportRequest{ |
| 25 |
orig: &internal.ExportLogsServiceRequest{}, |
| 26 |
state: internal.NewState(), |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
// NewExportRequestFromLogs returns a ExportRequest from plog.Logs. |
| 31 |
// Because ExportRequest is a wrapper for plog.Logs, |
| 32 |
// any changes to the provided Logs struct will be reflected in the ExportRequest and vice versa. |
| 33 |
func NewExportRequestFromLogs(ld plog.Logs) ExportRequest { |
| 34 |
return ExportRequest{ |
| 35 |
orig: internal.GetLogsOrig(internal.LogsWrapper(ld)), |
| 36 |
state: internal.GetLogsState(internal.LogsWrapper(ld)), |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
// MarshalProto marshals ExportRequest into proto bytes. |
| 41 |
func (ms ExportRequest) MarshalProto() ([]byte, error) { |
| 42 |
size := ms.orig.SizeProto() |
| 43 |
buf := make([]byte, size) |
| 44 |
_ = ms.orig.MarshalProto(buf) |
| 45 |
return buf, nil |
| 46 |
} |
| 47 |
|
| 48 |
// UnmarshalProto unmarshalls ExportRequest from proto bytes. |
| 49 |
func (ms ExportRequest) UnmarshalProto(data []byte) error { |
| 50 |
err := ms.orig.UnmarshalProto(data) |
| 51 |
if err != nil { |
| 52 |
return err |
| 53 |
} |
| 54 |
otlp.MigrateLogs(ms.orig.ResourceLogs) |
| 55 |
return nil |
| 56 |
} |
| 57 |
|
| 58 |
// MarshalJSON marshals ExportRequest into JSON bytes. |
| 59 |
func (ms ExportRequest) MarshalJSON() ([]byte, error) { |
| 60 |
dest := json.BorrowStream(nil) |
| 61 |
defer json.ReturnStream(dest) |
| 62 |
ms.orig.MarshalJSON(dest) |
| 63 |
if dest.Error() != nil { |
| 64 |
return nil, dest.Error() |
| 65 |
} |
| 66 |
return slices.Clone(dest.Buffer()), nil |
| 67 |
} |
| 68 |
|
| 69 |
// UnmarshalJSON unmarshalls ExportRequest from JSON bytes. |
| 70 |
func (ms ExportRequest) UnmarshalJSON(data []byte) error { |
| 71 |
iter := json.BorrowIterator(data) |
| 72 |
defer json.ReturnIterator(iter) |
| 73 |
ms.orig.UnmarshalJSON(iter) |
| 74 |
return iter.Error() |
| 75 |
} |
| 76 |
|
| 77 |
func (ms ExportRequest) Logs() plog.Logs { |
| 78 |
return plog.Logs(internal.NewLogsWrapper(ms.orig, ms.state)) |
| 79 |
} |