mirror of
https://github.com/hanzoai/telemetry.git
synced 2026-10-30 20:01:49 +00:00
This is a huge PR that does not touch any of the critical logic "marshaling/unmarshaling" of the pdata. We have e2e tests with the go official generated protobufs to ensure nothing breaks. The easy way to review this is to only look at what change publicly in "pdata/pcommon", "pdata/plog", "pdata/ptrace", "pdata/pmetric", "pdata/pprofile", "pdata/xpdata". This PR also switches the xpada request to use the same generating mechanism as pdatagen. **Binary size before:** ``` 42246018 bin/otelcorecol_darwin_arm64 ``` **Binary size after:** ``` 40709202 bin/otelcorecol_darwin_arm64 ``` Signed-off-by: Bogdan Drutu <[email protected]>
80 lines
2.3 KiB
Go
80 lines
2.3 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package ptraceotlp // import "go.opentelemetry.io/collector/pdata/ptrace/ptraceotlp"
|
|
|
|
import (
|
|
"slices"
|
|
|
|
"go.opentelemetry.io/collector/pdata/internal"
|
|
"go.opentelemetry.io/collector/pdata/internal/json"
|
|
"go.opentelemetry.io/collector/pdata/internal/otlp"
|
|
"go.opentelemetry.io/collector/pdata/ptrace"
|
|
)
|
|
|
|
// ExportRequest represents the request for gRPC/HTTP client/server.
|
|
// It's a wrapper for ptrace.Traces data.
|
|
type ExportRequest struct {
|
|
orig *internal.ExportTraceServiceRequest
|
|
state *internal.State
|
|
}
|
|
|
|
// NewExportRequest returns an empty ExportRequest.
|
|
func NewExportRequest() ExportRequest {
|
|
return ExportRequest{
|
|
orig: &internal.ExportTraceServiceRequest{},
|
|
state: internal.NewState(),
|
|
}
|
|
}
|
|
|
|
// NewExportRequestFromTraces returns a ExportRequest from ptrace.Traces.
|
|
// Because ExportRequest is a wrapper for ptrace.Traces,
|
|
// any changes to the provided Traces struct will be reflected in the ExportRequest and vice versa.
|
|
func NewExportRequestFromTraces(td ptrace.Traces) ExportRequest {
|
|
return ExportRequest{
|
|
orig: internal.GetTracesOrig(internal.TracesWrapper(td)),
|
|
state: internal.GetTracesState(internal.TracesWrapper(td)),
|
|
}
|
|
}
|
|
|
|
// MarshalProto marshals ExportRequest into proto bytes.
|
|
func (ms ExportRequest) MarshalProto() ([]byte, error) {
|
|
size := ms.orig.SizeProto()
|
|
buf := make([]byte, size)
|
|
_ = ms.orig.MarshalProto(buf)
|
|
return buf, nil
|
|
}
|
|
|
|
// UnmarshalProto unmarshalls ExportRequest from proto bytes.
|
|
func (ms ExportRequest) UnmarshalProto(data []byte) error {
|
|
err := ms.orig.UnmarshalProto(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
otlp.MigrateTraces(ms.orig.ResourceSpans)
|
|
return nil
|
|
}
|
|
|
|
// MarshalJSON marshals ExportRequest into JSON bytes.
|
|
func (ms ExportRequest) MarshalJSON() ([]byte, error) {
|
|
dest := json.BorrowStream(nil)
|
|
defer json.ReturnStream(dest)
|
|
ms.orig.MarshalJSON(dest)
|
|
if dest.Error() != nil {
|
|
return nil, dest.Error()
|
|
}
|
|
return slices.Clone(dest.Buffer()), nil
|
|
}
|
|
|
|
// UnmarshalJSON unmarshalls ExportRequest from JSON bytes.
|
|
func (ms ExportRequest) UnmarshalJSON(data []byte) error {
|
|
iter := json.BorrowIterator(data)
|
|
defer json.ReturnIterator(iter)
|
|
ms.orig.UnmarshalJSON(iter)
|
|
return iter.Error()
|
|
}
|
|
|
|
func (ms ExportRequest) Traces() ptrace.Traces {
|
|
return ptrace.Traces(internal.NewTracesWrapper(ms.orig, ms.state))
|
|
}
|