Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
haoqixu committed Dec 6, 2024
1 parent 96c4767 commit cb30921
Show file tree
Hide file tree
Showing 15 changed files with 150 additions and 7 deletions.
65 changes: 60 additions & 5 deletions exporter/fileexporter/file_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,27 +524,65 @@ 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,
profilesMarshaler: profilesMarshalers[formatTypeJSON],
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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion exporter/fileexporter/generated_package_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion internal/coreinternal/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions internal/coreinternal/testdata/profile.go
Original file line number Diff line number Diff line change
@@ -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")
}
1 change: 1 addition & 0 deletions internal/filter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/filter/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/resourcetotelemetry/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/translator/jaeger/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/translator/jaeger/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/translator/opencensus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/translator/opencensus/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/translator/prometheusremotewrite/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/translator/prometheusremotewrite/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pkg/translator/zipkin/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions pkg/translator/zipkin/go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb30921

Please sign in to comment.