| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package plogotlp |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
gootlpcollectorlogs "go.opentelemetry.io/proto/slim/otlp/collector/logs/v1" |
| 14 |
goproto "google.golang.org/protobuf/proto" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/pdata/internal" |
| 17 |
"go.opentelemetry.io/collector/pdata/internal/otlp" |
| 18 |
"go.opentelemetry.io/collector/pdata/plog" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
_ json.Unmarshaler = ExportRequest{} |
| 23 |
_ json.Marshaler = ExportRequest{} |
| 24 |
) |
| 25 |
|
| 26 |
var logsRequestJSON = []byte(` |
| 27 |
{ |
| 28 |
"resourceLogs": [ |
| 29 |
{ |
| 30 |
"resource": {}, |
| 31 |
"scopeLogs": [ |
| 32 |
{ |
| 33 |
"scope": {}, |
| 34 |
"logRecords": [ |
| 35 |
{ |
| 36 |
"body": { |
| 37 |
"stringValue": "test_log_record" |
| 38 |
} |
| 39 |
} |
| 40 |
] |
| 41 |
} |
| 42 |
] |
| 43 |
} |
| 44 |
] |
| 45 |
}`) |
| 46 |
|
| 47 |
func TestRequestToPData(t *testing.T) { |
| 48 |
tr := NewExportRequest() |
| 49 |
assert.Equal(t, 0, tr.Logs().LogRecordCount()) |
| 50 |
tr.Logs().ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords().AppendEmpty() |
| 51 |
assert.Equal(t, 1, tr.Logs().LogRecordCount()) |
| 52 |
} |
| 53 |
|
| 54 |
func TestRequestJSON(t *testing.T) { |
| 55 |
lr := NewExportRequest() |
| 56 |
require.NoError(t, lr.UnmarshalJSON(logsRequestJSON)) |
| 57 |
assert.Equal(t, "test_log_record", lr.Logs().ResourceLogs().At(0).ScopeLogs().At(0).LogRecords().At(0).Body().AsString()) |
| 58 |
|
| 59 |
got, err := lr.MarshalJSON() |
| 60 |
require.NoError(t, err) |
| 61 |
assert.Equal(t, strings.Join(strings.Fields(string(logsRequestJSON)), ""), string(got)) |
| 62 |
} |
| 63 |
|
| 64 |
func TestLogsProtoWireCompatibility(t *testing.T) { |
| 65 |
// This test verifies that OTLP ProtoBufs generated using goproto lib in |
| 66 |
// opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in |
| 67 |
// this repository are wire compatible. |
| 68 |
|
| 69 |
// Generate Logs as pdata struct. |
| 70 |
ld := NewExportRequestFromLogs(plog.Logs(internal.GenTestLogsWrapper())) |
| 71 |
|
| 72 |
// Marshal its underlying ProtoBuf to wire. |
| 73 |
wire1, err := ld.MarshalProto() |
| 74 |
require.NoError(t, err) |
| 75 |
assert.NotNil(t, wire1) |
| 76 |
|
| 77 |
// Unmarshal from the wire to OTLP Protobuf in goproto's representation. |
| 78 |
var goprotoMessage gootlpcollectorlogs.ExportLogsServiceRequest |
| 79 |
err = goproto.Unmarshal(wire1, &goprotoMessage) |
| 80 |
require.NoError(t, err) |
| 81 |
|
| 82 |
// Marshal to the wire again. |
| 83 |
wire2, err := goproto.Marshal(&goprotoMessage) |
| 84 |
require.NoError(t, err) |
| 85 |
assert.NotNil(t, wire2) |
| 86 |
|
| 87 |
// Unmarshal from the wire into gogoproto's representation. |
| 88 |
ld2 := NewExportRequest() |
| 89 |
err = ld2.UnmarshalProto(wire2) |
| 90 |
require.NoError(t, err) |
| 91 |
|
| 92 |
// Now compare that the original and final ProtoBuf messages are the same. |
| 93 |
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible. |
| 94 |
// Migration logic will run, so run it on the original message as well. |
| 95 |
otlp.MigrateLogs(ld.orig.ResourceLogs) |
| 96 |
assert.Equal(t, ld, ld2) |
| 97 |
} |