From cb309211741ebebba78708dd8407f7476d44e276 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 078b2abca52c..7639c6440aa7 100644 --- a/internal/coreinternal/go.mod +++ b/internal/coreinternal/go.mod @@ -17,6 +17,7 @@ require ( go.opentelemetry.io/collector/consumer/consumererror v0.115.0 go.opentelemetry.io/collector/consumer/consumertest v0.115.0 go.opentelemetry.io/collector/pdata v1.21.0 + go.opentelemetry.io/collector/pdata/pprofile v0.115.0 go.opentelemetry.io/collector/receiver v0.115.0 go.opentelemetry.io/collector/receiver/receivertest v0.115.0 go.opentelemetry.io/collector/semconv v0.115.0 @@ -76,7 +77,6 @@ require ( github.com/yusufpapurcu/wmi v1.2.4 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.115.0 // indirect go.opentelemetry.io/collector/consumer/consumerprofiles v0.115.0 // indirect - go.opentelemetry.io/collector/pdata/pprofile v0.115.0 // indirect go.opentelemetry.io/collector/pipeline v0.115.0 // indirect go.opentelemetry.io/collector/receiver/receiverprofiles v0.115.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 123d671c16cb..d4e1880e75ff 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.115.0 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.115.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 67bfd0ba3bce..95841ce7d89d 100644 --- a/internal/filter/go.sum +++ b/internal/filter/go.sum @@ -99,6 +99,8 @@ go.opentelemetry.io/collector/featuregate v1.21.0 h1:+EULHPJDLMipcwAGZVp9Nm8NriR go.opentelemetry.io/collector/featuregate v1.21.0/go.mod h1:3GaXqflNDVwWndNGBJ1+XJFy3Fv/XrFgjMN60N3z7yg= go.opentelemetry.io/collector/pdata v1.21.0 h1:PG+UbiFMJ35X/WcAR7Rf/PWmWtRdW0aHlOidsR6c5MA= go.opentelemetry.io/collector/pdata v1.21.0/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.opentelemetry.io/collector/semconv v0.115.0 h1:SoqMvg4ZEB3mz2EdAb6XYa+TuMo5Mir5FRBr3nVFUDY= go.opentelemetry.io/collector/semconv v0.115.0/go.mod h1:N6XE8Q0JKgBN2fAhkUQtqK9LT7rEGR6+Wu/Rtbal1iI= 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 53e13dca3efe..a5c484969f43 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.115.0 // indirect go.opentelemetry.io/collector/config/configtelemetry v0.115.0 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.115.0 // indirect go.opentelemetry.io/collector/pipeline v0.115.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 c1a0156d3b83..15e9fb4f2b45 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.115.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 33f90c2f1626..e9bab3b08469 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.21.0 h1:PG+UbiFMJ35X/WcAR7Rf/PWmWtRdW0aHlOidsR6c5MA= go.opentelemetry.io/collector/pdata v1.21.0/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.opentelemetry.io/collector/semconv v0.115.0 h1:SoqMvg4ZEB3mz2EdAb6XYa+TuMo5Mir5FRBr3nVFUDY= go.opentelemetry.io/collector/semconv v0.115.0/go.mod h1:N6XE8Q0JKgBN2fAhkUQtqK9LT7rEGR6+Wu/Rtbal1iI= 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 efe0a8b125c6..6e1ec73f636f 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.115.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + go.opentelemetry.io/collector/pdata/pprofile v0.115.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 667c6e0db9f4..69ae6db89265 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.21.0 h1:PG+UbiFMJ35X/WcAR7Rf/PWmWtRdW0aHlOidsR6c5MA= go.opentelemetry.io/collector/pdata v1.21.0/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.opentelemetry.io/collector/semconv v0.115.0 h1:SoqMvg4ZEB3mz2EdAb6XYa+TuMo5Mir5FRBr3nVFUDY= go.opentelemetry.io/collector/semconv v0.115.0/go.mod h1:N6XE8Q0JKgBN2fAhkUQtqK9LT7rEGR6+Wu/Rtbal1iI= 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 c974a5e1ca64..79c823940bc3 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.115.0 // indirect golang.org/x/net v0.29.0 // indirect golang.org/x/sys v0.27.0 // indirect golang.org/x/text v0.20.0 // indirect diff --git a/pkg/translator/prometheusremotewrite/go.sum b/pkg/translator/prometheusremotewrite/go.sum index 81a078d3506a..62542770eb11 100644 --- a/pkg/translator/prometheusremotewrite/go.sum +++ b/pkg/translator/prometheusremotewrite/go.sum @@ -47,6 +47,8 @@ go.opentelemetry.io/collector/featuregate v1.21.0 h1:+EULHPJDLMipcwAGZVp9Nm8NriR go.opentelemetry.io/collector/featuregate v1.21.0/go.mod h1:3GaXqflNDVwWndNGBJ1+XJFy3Fv/XrFgjMN60N3z7yg= go.opentelemetry.io/collector/pdata v1.21.0 h1:PG+UbiFMJ35X/WcAR7Rf/PWmWtRdW0aHlOidsR6c5MA= go.opentelemetry.io/collector/pdata v1.21.0/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.opentelemetry.io/collector/semconv v0.115.0 h1:SoqMvg4ZEB3mz2EdAb6XYa+TuMo5Mir5FRBr3nVFUDY= go.opentelemetry.io/collector/semconv v0.115.0/go.mod h1:N6XE8Q0JKgBN2fAhkUQtqK9LT7rEGR6+Wu/Rtbal1iI= 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 5cde104365a6..8b853b0fee5a 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.115.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 540cfb7d7c7e..073cdb4ea39f 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.21.0 h1:PG+UbiFMJ35X/WcAR7Rf/PWmWtRdW0aHlOidsR6c5MA= go.opentelemetry.io/collector/pdata v1.21.0/go.mod h1:GKb1/zocKJMvxKbS+sl0W85lxhYBTFJ6h6I1tphVyDU= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0 h1:NI89hy13vNDw7EOnQf7Jtitks4HJFO0SUWznTssmP94= +go.opentelemetry.io/collector/pdata/pprofile v0.115.0/go.mod h1:jGzdNfO0XTtfLjXCL/uCC1livg1LlfR+ix2WE/z3RpQ= go.opentelemetry.io/collector/semconv v0.115.0 h1:SoqMvg4ZEB3mz2EdAb6XYa+TuMo5Mir5FRBr3nVFUDY= go.opentelemetry.io/collector/semconv v0.115.0/go.mod h1:N6XE8Q0JKgBN2fAhkUQtqK9LT7rEGR6+Wu/Rtbal1iI= go.opentelemetry.io/otel v1.32.0 h1:WnBN+Xjcteh0zdk01SVqV55d/m62NJLJdIyb4y/WO5U=