From b621d71e60b3088dd06c021df34f46b8bbeda7d8 Mon Sep 17 00:00:00 2001 From: haoqixu Date: Thu, 21 Nov 2024 15:37:08 +0800 Subject: [PATCH] update tests --- exporter/fileexporter/file_exporter_test.go | 65 +++++++++++++++-- .../fileexporter/generated_package_test.go | 3 +- internal/coreinternal/go.mod | 2 +- internal/coreinternal/testdata/profile.go | 71 +++++++++++++++++++ internal/filter/go.mod | 1 + internal/filter/go.sum | 2 + pkg/resourcetotelemetry/go.mod | 1 + pkg/translator/jaeger/go.mod | 1 + pkg/translator/jaeger/go.sum | 2 + pkg/translator/opencensus/go.mod | 1 + pkg/translator/opencensus/go.sum | 2 + pkg/translator/prometheusremotewrite/go.mod | 1 + pkg/translator/prometheusremotewrite/go.sum | 2 + pkg/translator/zipkin/go.mod | 1 + pkg/translator/zipkin/go.sum | 2 + 15 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 internal/coreinternal/testdata/profile.go diff --git a/exporter/fileexporter/file_exporter_test.go b/exporter/fileexporter/file_exporter_test.go index d3b091cdf41f..a4680acb8367 100644 --- a/exporter/fileexporter/file_exporter_test.go +++ b/exporter/fileexporter/file_exporter_test.go @@ -524,13 +524,48 @@ func TestFileProfilesExporter(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - // TODO + conf := tt.args.conf + fe := &fileExporter{ + conf: conf, + } + require.NotNil(t, fe) + + pd := testdata.GenerateProfilesTwoProfileContainersSameResource() + assert.NoError(t, fe.Start(context.Background(), componenttest.NewNopHost())) + assert.NoError(t, fe.consumeProfiles(context.Background(), pd)) + assert.NoError(t, fe.consumeProfiles(context.Background(), pd)) + defer func() { + assert.NoError(t, fe.Shutdown(context.Background())) + }() + + fi, err := os.Open(fe.writer.path) + assert.NoError(t, err) + defer fi.Close() + br := bufio.NewReader(fi) + for { + buf, isEnd, err := func() ([]byte, bool, error) { + if fe.marshaller.formatType == formatTypeJSON && fe.marshaller.compression == "" { + return readJSONMessage(br) + } + return readMessageFromStream(br) + }() + assert.NoError(t, err) + if isEnd { + break + } + decoder := buildUnCompressor(fe.marshaller.compression) + buf, err = decoder(buf) + assert.NoError(t, err) + got, err := tt.args.unmarshaler.UnmarshalProfiles(buf) + assert.NoError(t, err) + assert.EqualValues(t, pd, got) + } }) } } func TestFileProfilesExporterErrors(t *testing.T) { - mf := &errorWriter{} + pf := &errorWriter{} fe := &fileExporter{ marshaller: &marshaller{ formatType: formatTypeJSON, @@ -538,13 +573,16 @@ func TestFileProfilesExporterErrors(t *testing.T) { compressor: noneCompress, }, writer: &fileWriter{ - file: mf, + file: pf, exporter: exportMessageAsLine, }, } require.NotNil(t, fe) - // TODO + pd := testdata.GenerateProfilesTwoProfileContainersSameResource() + // Cannot call Start since we inject directly the WriterCloser. + assert.Error(t, fe.consumeProfiles(context.Background(), pd)) + assert.NoError(t, fe.Shutdown(context.Background())) } func TestExportMessageAsBuffer(t *testing.T) { @@ -629,15 +667,17 @@ func decompress(src []byte) ([]byte, error) { func TestConcurrentlyCompress(t *testing.T) { wg := sync.WaitGroup{} - wg.Add(3) + wg.Add(4) var ( ctd []byte cmd []byte cld []byte + cpd []byte ) td := testdata.GenerateTracesTwoSpansSameResource() md := testdata.GenerateMetricsTwoMetrics() ld := testdata.GenerateLogsTwoLogRecordsSameResource() + pd := testdata.GenerateProfilesTwoProfileContainersSameResource() go func() { defer wg.Done() buf, err := tracesMarshalers[formatTypeJSON].MarshalTraces(td) @@ -662,6 +702,14 @@ func TestConcurrentlyCompress(t *testing.T) { } cld = zstdCompress(buf) }() + go func() { + defer wg.Done() + buf, err := profilesMarshalers[formatTypeJSON].MarshalProfiles(pd) + if err != nil { + return + } + cpd = zstdCompress(buf) + }() wg.Wait() buf, err := decompress(ctd) assert.NoError(t, err) @@ -683,6 +731,13 @@ func TestConcurrentlyCompress(t *testing.T) { gotLd, err := logsUnmarshaler.UnmarshalLogs(buf) assert.NoError(t, err) assert.EqualValues(t, ld, gotLd) + + buf, err = decompress(cpd) + assert.NoError(t, err) + profilesUnmarshaler := &pprofile.JSONUnmarshaler{} + gotPd, err := profilesUnmarshaler.UnmarshalProfiles(buf) + assert.NoError(t, err) + assert.EqualValues(t, pd, gotPd) } // tsBuffer is a thread safe buffer to prevent race conditions in the CI/CD. diff --git a/exporter/fileexporter/generated_package_test.go b/exporter/fileexporter/generated_package_test.go index c062542eec6f..a32fb0d65f95 100644 --- a/exporter/fileexporter/generated_package_test.go +++ b/exporter/fileexporter/generated_package_test.go @@ -3,8 +3,9 @@ package fileexporter import ( - "go.uber.org/goleak" "testing" + + "go.uber.org/goleak" ) func TestMain(m *testing.M) { diff --git a/internal/coreinternal/go.mod b/internal/coreinternal/go.mod index 9c1d6a0c9d7d..cf4072fd7310 100644 --- a/internal/coreinternal/go.mod +++ b/internal/coreinternal/go.mod @@ -17,6 +17,7 @@ require ( go.opentelemetry.io/collector/consumer/consumererror v0.114.0 go.opentelemetry.io/collector/consumer/consumertest v0.114.0 go.opentelemetry.io/collector/pdata v1.20.0 + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 go.opentelemetry.io/collector/receiver v0.114.0 go.opentelemetry.io/collector/receiver/receivertest v0.114.0 go.opentelemetry.io/collector/semconv v0.114.0 @@ -76,7 +77,6 @@ require ( github.com/yusufpapurcu/wmi v1.2.4 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.114.0 // indirect go.opentelemetry.io/collector/consumer/consumerprofiles v0.114.0 // indirect - go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect go.opentelemetry.io/collector/pipeline v0.114.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.114.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect diff --git a/internal/coreinternal/testdata/profile.go b/internal/coreinternal/testdata/profile.go new file mode 100644 index 000000000000..35b9a9249bb8 --- /dev/null +++ b/internal/coreinternal/testdata/profile.go @@ -0,0 +1,71 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package testdata + +import ( + "time" + + "go.opentelemetry.io/collector/pdata/pcommon" + "go.opentelemetry.io/collector/pdata/pprofile" +) + +var ( + TestProfileStartTime = time.Date(2020, 2, 11, 20, 26, 13, 789, time.UTC) + TestProfileEndTime = time.Date(2020, 2, 11, 20, 28, 13, 789, time.UTC) + TestProfileStartTimestamp = pcommon.NewTimestampFromTime(TestProfileStartTime) + TestProfileEndTimestamp = pcommon.NewTimestampFromTime(TestProfileEndTime) +) + +func GenerateProfilesOneEmptyResourceProfiles() pprofile.Profiles { + pd := pprofile.NewProfiles() + pd.ResourceProfiles().AppendEmpty() + return pd +} + +func GenerateProfilesNoProfileContainers() pprofile.Profiles { + pd := GenerateProfilesOneEmptyResourceProfiles() + initResource1(pd.ResourceProfiles().At(0).Resource()) + return pd +} + +func GenerateProfilesOneEmptyProfileContainer() pprofile.Profiles { + pd := GenerateProfilesNoProfileContainers() + rs0 := pd.ResourceProfiles().At(0) + rs0.ScopeProfiles().AppendEmpty().Profiles().AppendEmpty() + return pd +} + +func GenerateProfilesOneProfileContainer() pprofile.Profiles { + pd := GenerateProfilesOneEmptyProfileContainer() + fillProfileOne(pd.ResourceProfiles().At(0).ScopeProfiles().At(0).Profiles().At(0)) + return pd +} + +func GenerateProfilesTwoProfileContainersSameResource() pprofile.Profiles { + pd := GenerateProfilesOneEmptyProfileContainer() + profiles := pd.ResourceProfiles().At(0).ScopeProfiles().At(0).Profiles() + fillProfileOne(profiles.At(0)) + fillProfileTwo(profiles.AppendEmpty()) + return pd +} + +func fillProfileOne(profile pprofile.ProfileContainer) { + profile.SetStartTime(TestProfileStartTimestamp) + profile.SetEndTime(TestProfileEndTimestamp) + profile.SetProfileID([16]byte{0x01, 0x02, 0x03, 0x04}) + + attrs := profile.Attributes() + attrs.PutStr("app", "server") + attrs.PutInt("instance_num", 1) +} + +func fillProfileTwo(profile pprofile.ProfileContainer) { + profile.SetStartTime(TestProfileStartTimestamp) + profile.SetEndTime(TestProfileEndTimestamp) + profile.SetProfileID([16]byte{0x05, 0x06, 0x07, 0x08}) + + attrs := profile.Attributes() + attrs.PutStr("customer", "acme") + attrs.PutStr("env", "dev") +} diff --git a/internal/filter/go.mod b/internal/filter/go.mod index 76aa05b243ea..52ea9896cd58 100644 --- a/internal/filter/go.mod +++ b/internal/filter/go.mod @@ -50,6 +50,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/ua-parser/uap-go v0.0.0-20240611065828-3a4781585db6 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.114.0 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect go.opentelemetry.io/otel v1.32.0 // indirect go.opentelemetry.io/otel/metric v1.32.0 // indirect go.opentelemetry.io/otel/sdk v1.32.0 // indirect diff --git a/internal/filter/go.sum b/internal/filter/go.sum index 61cdbe4f8cf7..13a1b62ec9d6 100644 --- a/internal/filter/go.sum +++ b/internal/filter/go.sum @@ -99,6 +99,8 @@ go.opentelemetry.io/collector/featuregate v1.20.0 h1:Mi7nMy/q52eruI+6jWnMKUOeM55 go.opentelemetry.io/collector/featuregate v1.20.0/go.mod h1:47xrISO71vJ83LSMm8+yIDsUbKktUp48Ovt7RR6VbRs= go.opentelemetry.io/collector/pdata v1.20.0 h1:ePcwt4bdtISP0loHaE+C9xYoU2ZkIvWv89Fob16o9SM= go.opentelemetry.io/collector/pdata v1.20.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0 h1:pUNfTzsI/JUTiE+DScDM4lsrPoxnVNLI2fbTxR/oapo= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0/go.mod h1:4aNcj6WM1n1uXyFSXlhVs4ibrERgNYsTbzcYI2zGhxA= go.opentelemetry.io/collector/semconv v0.114.0 h1:/eKcCJwZepQUtEuFuxa0thx2XIOvhFpaf214ZG1a11k= go.opentelemetry.io/collector/semconv v0.114.0/go.mod h1:zCJ5njhWpejR+A40kiEoeFm1xq1uzyZwMnRNX6/D82A= go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U= diff --git a/pkg/resourcetotelemetry/go.mod b/pkg/resourcetotelemetry/go.mod index 3a989ebed870..9df0a32156e3 100644 --- a/pkg/resourcetotelemetry/go.mod +++ b/pkg/resourcetotelemetry/go.mod @@ -20,6 +20,7 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect go.opentelemetry.io/collector/component v0.114.0 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.114.0 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect go.opentelemetry.io/collector/pipeline v0.114.0 // indirect go.opentelemetry.io/otel v1.32.0 // indirect go.opentelemetry.io/otel/metric v1.32.0 // indirect diff --git a/pkg/translator/jaeger/go.mod b/pkg/translator/jaeger/go.mod index 508b0de64d85..d5060e1b5d4e 100644 --- a/pkg/translator/jaeger/go.mod +++ b/pkg/translator/jaeger/go.mod @@ -19,6 +19,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect go.opentelemetry.io/otel v1.32.0 // indirect go.opentelemetry.io/otel/trace v1.32.0 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/pkg/translator/jaeger/go.sum b/pkg/translator/jaeger/go.sum index 5f99d08e7ea3..2010ed008c80 100644 --- a/pkg/translator/jaeger/go.sum +++ b/pkg/translator/jaeger/go.sum @@ -37,6 +37,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/collector/pdata v1.20.0 h1:ePcwt4bdtISP0loHaE+C9xYoU2ZkIvWv89Fob16o9SM= go.opentelemetry.io/collector/pdata v1.20.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0 h1:pUNfTzsI/JUTiE+DScDM4lsrPoxnVNLI2fbTxR/oapo= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0/go.mod h1:4aNcj6WM1n1uXyFSXlhVs4ibrERgNYsTbzcYI2zGhxA= go.opentelemetry.io/collector/semconv v0.114.0 h1:/eKcCJwZepQUtEuFuxa0thx2XIOvhFpaf214ZG1a11k= go.opentelemetry.io/collector/semconv v0.114.0/go.mod h1:zCJ5njhWpejR+A40kiEoeFm1xq1uzyZwMnRNX6/D82A= go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U= diff --git a/pkg/translator/opencensus/go.mod b/pkg/translator/opencensus/go.mod index 9ab0005ea465..4501fc3ad4c5 100644 --- a/pkg/translator/opencensus/go.mod +++ b/pkg/translator/opencensus/go.mod @@ -27,6 +27,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.114.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/net v0.28.0 // indirect golang.org/x/sys v0.27.0 // indirect diff --git a/pkg/translator/opencensus/go.sum b/pkg/translator/opencensus/go.sum index e13e8768652b..2ae8767e90b6 100644 --- a/pkg/translator/opencensus/go.sum +++ b/pkg/translator/opencensus/go.sum @@ -77,6 +77,8 @@ go.opencensus.io v0.24.0 h1:y73uSU6J157QMP2kn2r30vwW1A2W2WFwSCGnAVxeaD0= go.opencensus.io v0.24.0/go.mod h1:vNK8G9p7aAivkbmorf4v+7Hgx+Zs0yY+0fOtgBfjQKo= go.opentelemetry.io/collector/pdata v1.20.0 h1:ePcwt4bdtISP0loHaE+C9xYoU2ZkIvWv89Fob16o9SM= go.opentelemetry.io/collector/pdata v1.20.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0 h1:pUNfTzsI/JUTiE+DScDM4lsrPoxnVNLI2fbTxR/oapo= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0/go.mod h1:4aNcj6WM1n1uXyFSXlhVs4ibrERgNYsTbzcYI2zGhxA= go.opentelemetry.io/collector/semconv v0.114.0 h1:/eKcCJwZepQUtEuFuxa0thx2XIOvhFpaf214ZG1a11k= go.opentelemetry.io/collector/semconv v0.114.0/go.mod h1:zCJ5njhWpejR+A40kiEoeFm1xq1uzyZwMnRNX6/D82A= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= diff --git a/pkg/translator/prometheusremotewrite/go.mod b/pkg/translator/prometheusremotewrite/go.mod index 58293ad0728f..8d477c8fc8d1 100644 --- a/pkg/translator/prometheusremotewrite/go.mod +++ b/pkg/translator/prometheusremotewrite/go.mod @@ -28,6 +28,7 @@ require ( github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_model v0.6.1 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.19.0 // indirect diff --git a/pkg/translator/prometheusremotewrite/go.sum b/pkg/translator/prometheusremotewrite/go.sum index 9caeb11c6ac9..4e95f8def6d0 100644 --- a/pkg/translator/prometheusremotewrite/go.sum +++ b/pkg/translator/prometheusremotewrite/go.sum @@ -47,6 +47,8 @@ go.opentelemetry.io/collector/featuregate v1.20.0 h1:Mi7nMy/q52eruI+6jWnMKUOeM55 go.opentelemetry.io/collector/featuregate v1.20.0/go.mod h1:47xrISO71vJ83LSMm8+yIDsUbKktUp48Ovt7RR6VbRs= go.opentelemetry.io/collector/pdata v1.20.0 h1:ePcwt4bdtISP0loHaE+C9xYoU2ZkIvWv89Fob16o9SM= go.opentelemetry.io/collector/pdata v1.20.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0 h1:pUNfTzsI/JUTiE+DScDM4lsrPoxnVNLI2fbTxR/oapo= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0/go.mod h1:4aNcj6WM1n1uXyFSXlhVs4ibrERgNYsTbzcYI2zGhxA= go.opentelemetry.io/collector/semconv v0.114.0 h1:/eKcCJwZepQUtEuFuxa0thx2XIOvhFpaf214ZG1a11k= go.opentelemetry.io/collector/semconv v0.114.0/go.mod h1:zCJ5njhWpejR+A40kiEoeFm1xq1uzyZwMnRNX6/D82A= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= diff --git a/pkg/translator/zipkin/go.mod b/pkg/translator/zipkin/go.mod index 3584a04bee72..5eda29fc1608 100644 --- a/pkg/translator/zipkin/go.mod +++ b/pkg/translator/zipkin/go.mod @@ -20,6 +20,7 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.114.0 // indirect go.opentelemetry.io/otel v1.32.0 // indirect go.opentelemetry.io/otel/trace v1.32.0 // indirect go.uber.org/multierr v1.11.0 // indirect diff --git a/pkg/translator/zipkin/go.sum b/pkg/translator/zipkin/go.sum index 2b6550cc21b1..6a8808e0307e 100644 --- a/pkg/translator/zipkin/go.sum +++ b/pkg/translator/zipkin/go.sum @@ -39,6 +39,8 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= go.opentelemetry.io/collector/pdata v1.20.0 h1:ePcwt4bdtISP0loHaE+C9xYoU2ZkIvWv89Fob16o9SM= go.opentelemetry.io/collector/pdata v1.20.0/go.mod h1:Ox1YVLe87cZDB/TL30i4SUz1cA5s6AM6SpFMfY61ICs= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0 h1:pUNfTzsI/JUTiE+DScDM4lsrPoxnVNLI2fbTxR/oapo= +go.opentelemetry.io/collector/pdata/pprofile v0.114.0/go.mod h1:4aNcj6WM1n1uXyFSXlhVs4ibrERgNYsTbzcYI2zGhxA= go.opentelemetry.io/collector/semconv v0.114.0 h1:/eKcCJwZepQUtEuFuxa0thx2XIOvhFpaf214ZG1a11k= go.opentelemetry.io/collector/semconv v0.114.0/go.mod h1:zCJ5njhWpejR+A40kiEoeFm1xq1uzyZwMnRNX6/D82A= go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U=