Files
Bogdan DrutuandGitHub 0a56a7db33 Remove usage of gogoproto and generate the structs with pdatagen (#14078)
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]>
2025-10-27 15:28:24 +00:00

98 lines
2.7 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package plogotlp
import (
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
gootlpcollectorlogs "go.opentelemetry.io/proto/slim/otlp/collector/logs/v1"
goproto "google.golang.org/protobuf/proto"
"go.opentelemetry.io/collector/pdata/internal"
"go.opentelemetry.io/collector/pdata/internal/otlp"
"go.opentelemetry.io/collector/pdata/plog"
)
var (
_ json.Unmarshaler = ExportRequest{}
_ json.Marshaler = ExportRequest{}
)
var logsRequestJSON = []byte(`
{
"resourceLogs": [
{
"resource": {},
"scopeLogs": [
{
"scope": {},
"logRecords": [
{
"body": {
"stringValue": "test_log_record"
}
}
]
}
]
}
]
}`)
func TestRequestToPData(t *testing.T) {
tr := NewExportRequest()
assert.Equal(t, 0, tr.Logs().LogRecordCount())
tr.Logs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty()
assert.Equal(t, 1, tr.Logs().LogRecordCount())
}
func TestRequestJSON(t *testing.T) {
lr := NewExportRequest()
require.NoError(t, lr.UnmarshalJSON(logsRequestJSON))
assert.Equal(t, "test_log_record", lr.Logs().ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Body().AsString())
got, err := lr.MarshalJSON()
require.NoError(t, err)
assert.Equal(t, strings.Join(strings.Fields(string(logsRequestJSON)), ""), string(got))
}
func TestLogsProtoWireCompatibility(t *testing.T) {
// This test verifies that OTLP ProtoBufs generated using goproto lib in
// opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in
// this repository are wire compatible.
// Generate Logs as pdata struct.
ld := NewExportRequestFromLogs(plog.Logs(internal.GenTestLogsWrapper()))
// Marshal its underlying ProtoBuf to wire.
wire1, err := ld.MarshalProto()
require.NoError(t, err)
assert.NotNil(t, wire1)
// Unmarshal from the wire to OTLP Protobuf in goproto's representation.
var goprotoMessage gootlpcollectorlogs.ExportLogsServiceRequest
err = goproto.Unmarshal(wire1, &goprotoMessage)
require.NoError(t, err)
// Marshal to the wire again.
wire2, err := goproto.Marshal(&goprotoMessage)
require.NoError(t, err)
assert.NotNil(t, wire2)
// Unmarshal from the wire into gogoproto's representation.
ld2 := NewExportRequest()
err = ld2.UnmarshalProto(wire2)
require.NoError(t, err)
// Now compare that the original and final ProtoBuf messages are the same.
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible.
// Migration logic will run, so run it on the original message as well.
otlp.MigrateLogs(ld.orig.ResourceLogs)
assert.Equal(t, ld, ld2)
}