| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package pprofileotlp |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"github.com/stretchr/testify/require" |
| 13 |
gootlpcollectorprofiles "go.opentelemetry.io/proto/slim/otlp/collector/profiles/v1development" |
| 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/pprofile" |
| 19 |
) |
| 20 |
|
| 21 |
var ( |
| 22 |
_ json.Unmarshaler = ExportRequest{} |
| 23 |
_ json.Marshaler = ExportRequest{} |
| 24 |
) |
| 25 |
|
| 26 |
var profilesRequestJSON = []byte(` |
| 27 |
{ |
| 28 |
"resourceProfiles": [ |
| 29 |
{ |
| 30 |
"resource": {}, |
| 31 |
"scopeProfiles": [ |
| 32 |
{ |
| 33 |
"scope": {}, |
| 34 |
"profiles": [ |
| 35 |
{ |
| 36 |
"sampleType": {}, |
| 37 |
"samples": [ |
| 38 |
{ |
| 39 |
"stackIndex": 42 |
| 40 |
} |
| 41 |
], |
| 42 |
"periodType": {} |
| 43 |
} |
| 44 |
] |
| 45 |
} |
| 46 |
] |
| 47 |
} |
| 48 |
], |
| 49 |
"dictionary": {} |
| 50 |
}`) |
| 51 |
|
| 52 |
func TestRequestToPData(t *testing.T) { |
| 53 |
tr := NewExportRequest() |
| 54 |
assert.Equal(t, 0, tr.Profiles().SampleCount()) |
| 55 |
tr.Profiles().ResourceProfiles().AppendEmpty().ScopeProfiles().AppendEmpty().Profiles().AppendEmpty().Samples().AppendEmpty() |
| 56 |
assert.Equal(t, 1, tr.Profiles().SampleCount()) |
| 57 |
} |
| 58 |
|
| 59 |
func TestRequestJSON(t *testing.T) { |
| 60 |
tr := NewExportRequest() |
| 61 |
require.NoError(t, tr.UnmarshalJSON(profilesRequestJSON)) |
| 62 |
assert.Equal(t, int32(42), tr.Profiles().ResourceProfiles().At(0).ScopeProfiles().At(0).Profiles().At(0).Samples().At(0).StackIndex()) |
| 63 |
|
| 64 |
got, err := tr.MarshalJSON() |
| 65 |
require.NoError(t, err) |
| 66 |
assert.Equal(t, strings.Join(strings.Fields(string(profilesRequestJSON)), ""), string(got)) |
| 67 |
} |
| 68 |
|
| 69 |
func TestProfilesProtoWireCompatibility(t *testing.T) { |
| 70 |
// This test verifies that OTLP ProtoBufs generated using goproto lib in |
| 71 |
// opentelemetry-proto repository OTLP ProtoBufs generated using gogoproto lib in |
| 72 |
// this repository are wire compatible. |
| 73 |
|
| 74 |
// Generate Profiles as pdata struct. |
| 75 |
pd := NewExportRequestFromProfiles(pprofile.Profiles(internal.GenTestProfilesWrapper())) |
| 76 |
|
| 77 |
// Marshal its underlying ProtoBuf to wire. |
| 78 |
wire1, err := pd.MarshalProto() |
| 79 |
require.NoError(t, err) |
| 80 |
assert.NotNil(t, wire1) |
| 81 |
|
| 82 |
// Unmarshal from the wire to OTLP Protobuf in goproto's representation. |
| 83 |
var goprotoMessage gootlpcollectorprofiles.ExportProfilesServiceRequest |
| 84 |
err = goproto.Unmarshal(wire1, &goprotoMessage) |
| 85 |
require.NoError(t, err) |
| 86 |
|
| 87 |
// Marshal to the wire again. |
| 88 |
wire2, err := goproto.Marshal(&goprotoMessage) |
| 89 |
require.NoError(t, err) |
| 90 |
assert.NotNil(t, wire2) |
| 91 |
|
| 92 |
// Unmarshal from the wire into gogoproto's representation. |
| 93 |
pd2 := NewExportRequest() |
| 94 |
err = pd2.UnmarshalProto(wire2) |
| 95 |
require.NoError(t, err) |
| 96 |
|
| 97 |
// Now compare that the original and final ProtoBuf messages are the same. |
| 98 |
// This proves that goproto and gogoproto marshaling/unmarshaling are wire compatible. |
| 99 |
// Migration logic will run, so run it on the original message as well. |
| 100 |
otlp.MigrateProfiles(pd.orig.ResourceProfiles) |
| 101 |
assert.Equal(t, pd, pd2) |
| 102 |
} |