Skip to content

Commit

Permalink
[chore] Simplify test only code in exporterhelper (#11620)
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu authored Nov 7, 2024
1 parent f0fb20d commit e690ac5
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 58 deletions.
20 changes: 11 additions & 9 deletions exporter/exporterhelper/exporterhelperprofiles/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestProfilesExporter_NilLogger(t *testing.T) {
}

func TestProfilesRequestExporter_NilLogger(t *testing.T) {
le, err := NewProfilesRequestExporter(context.Background(), exporter.Settings{}, (&internal.FakeRequestConverter{}).RequestFromProfilesFunc)
le, err := NewProfilesRequestExporter(context.Background(), exporter.Settings{}, internal.RequestFromProfilesFunc(nil))
require.Nil(t, le)
require.Equal(t, errNilLogger, err)
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func TestProfilesExporter_Default(t *testing.T) {
func TestProfilesRequestExporter_Default(t *testing.T) {
ld := pprofile.NewProfiles()
le, err := NewProfilesRequestExporter(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromProfilesFunc)
internal.RequestFromProfilesFunc(nil))
assert.NotNil(t, le)
require.NoError(t, err)

Expand All @@ -122,7 +122,7 @@ func TestProfilesExporter_WithCapabilities(t *testing.T) {
func TestProfilesRequestExporter_WithCapabilities(t *testing.T) {
capabilities := consumer.Capabilities{MutatesData: true}
le, err := NewProfilesRequestExporter(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromProfilesFunc, exporterhelper.WithCapabilities(capabilities))
internal.RequestFromProfilesFunc(nil), exporterhelper.WithCapabilities(capabilities))
require.NoError(t, err)
require.NotNil(t, le)

Expand All @@ -142,7 +142,9 @@ func TestProfilesRequestExporter_Default_ConvertError(t *testing.T) {
ld := pprofile.NewProfiles()
want := errors.New("convert_error")
le, err := NewProfilesRequestExporter(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{ProfilesError: want}).RequestFromProfilesFunc)
func(context.Context, pprofile.Profiles) (exporterhelper.Request, error) {
return nil, want
})
require.NoError(t, err)
require.NotNil(t, le)
require.Equal(t, consumererror.NewPermanent(want), le.ConsumeProfiles(context.Background(), ld))
Expand All @@ -152,7 +154,7 @@ func TestProfilesRequestExporter_Default_ExportError(t *testing.T) {
ld := pprofile.NewProfiles()
want := errors.New("export_error")
le, err := NewProfilesRequestExporter(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{RequestError: want}).RequestFromProfilesFunc)
internal.RequestFromProfilesFunc(want))
require.NoError(t, err)
require.NotNil(t, le)
require.Equal(t, want, le.ConsumeProfiles(context.Background(), ld))
Expand Down Expand Up @@ -202,7 +204,7 @@ func TestProfilesRequestExporter_WithSpan(t *testing.T) {
otel.SetTracerProvider(set.TracerProvider)
defer otel.SetTracerProvider(nooptrace.NewTracerProvider())

le, err := NewProfilesRequestExporter(context.Background(), set, (&internal.FakeRequestConverter{}).RequestFromProfilesFunc)
le, err := NewProfilesRequestExporter(context.Background(), set, internal.RequestFromProfilesFunc(nil))
require.NoError(t, err)
require.NotNil(t, le)
checkWrapSpanForProfilesExporter(t, sr, set.TracerProvider.Tracer("test"), le, nil, 1)
Expand Down Expand Up @@ -230,7 +232,7 @@ func TestProfilesRequestExporter_WithSpan_ReturnError(t *testing.T) {
defer otel.SetTracerProvider(nooptrace.NewTracerProvider())

want := errors.New("my_error")
le, err := NewProfilesRequestExporter(context.Background(), set, (&internal.FakeRequestConverter{RequestError: want}).RequestFromProfilesFunc)
le, err := NewProfilesRequestExporter(context.Background(), set, internal.RequestFromProfilesFunc(want))
require.NoError(t, err)
require.NotNil(t, le)
checkWrapSpanForProfilesExporter(t, sr, set.TracerProvider.Tracer("test"), le, want, 1)
Expand All @@ -253,7 +255,7 @@ func TestProfilesRequestExporter_WithShutdown(t *testing.T) {
shutdown := func(context.Context) error { shutdownCalled = true; return nil }

le, err := NewProfilesRequestExporter(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromProfilesFunc, exporterhelper.WithShutdown(shutdown))
internal.RequestFromProfilesFunc(nil), exporterhelper.WithShutdown(shutdown))
assert.NotNil(t, le)
require.NoError(t, err)

Expand All @@ -277,7 +279,7 @@ func TestProfilesRequestExporter_WithShutdown_ReturnError(t *testing.T) {
shutdownErr := func(context.Context) error { return want }

le, err := NewProfilesRequestExporter(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromProfilesFunc, exporterhelper.WithShutdown(shutdownErr))
internal.RequestFromProfilesFunc(nil), exporterhelper.WithShutdown(shutdownErr))
assert.NotNil(t, le)
require.NoError(t, err)

Expand Down
32 changes: 16 additions & 16 deletions exporter/exporterhelper/internal/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,26 @@ func (r *fakeRequest) MergeSplit(ctx context.Context, cfg exporterbatcher.MaxSiz
return res, nil
}

type FakeRequestConverter struct {
MetricsError error
TracesError error
LogsError error
ProfilesError error
RequestError error
}

func (frc *FakeRequestConverter) RequestFromMetricsFunc(_ context.Context, md pmetric.Metrics) (internal.Request, error) {
return &fakeRequest{items: md.DataPointCount(), exportErr: frc.RequestError}, frc.MetricsError
func RequestFromMetricsFunc(reqErr error) func(context.Context, pmetric.Metrics) (internal.Request, error) {
return func(_ context.Context, md pmetric.Metrics) (internal.Request, error) {
return &fakeRequest{items: md.DataPointCount(), exportErr: reqErr}, nil
}
}

func (frc *FakeRequestConverter) RequestFromTracesFunc(_ context.Context, md ptrace.Traces) (internal.Request, error) {
return &fakeRequest{items: md.SpanCount(), exportErr: frc.RequestError}, frc.TracesError
func RequestFromTracesFunc(reqErr error) func(context.Context, ptrace.Traces) (internal.Request, error) {
return func(_ context.Context, td ptrace.Traces) (internal.Request, error) {
return &fakeRequest{items: td.SpanCount(), exportErr: reqErr}, nil
}
}

func (frc *FakeRequestConverter) RequestFromLogsFunc(_ context.Context, md plog.Logs) (internal.Request, error) {
return &fakeRequest{items: md.LogRecordCount(), exportErr: frc.RequestError}, frc.LogsError
func RequestFromLogsFunc(reqErr error) func(context.Context, plog.Logs) (internal.Request, error) {
return func(_ context.Context, ld plog.Logs) (internal.Request, error) {
return &fakeRequest{items: ld.LogRecordCount(), exportErr: reqErr}, nil
}
}

func (frc *FakeRequestConverter) RequestFromProfilesFunc(_ context.Context, md pprofile.Profiles) (internal.Request, error) {
return &fakeRequest{items: md.SampleCount(), exportErr: frc.RequestError}, frc.ProfilesError
func RequestFromProfilesFunc(reqErr error) func(context.Context, pprofile.Profiles) (internal.Request, error) {
return func(_ context.Context, pd pprofile.Profiles) (internal.Request, error) {
return &fakeRequest{items: pd.SampleCount(), exportErr: reqErr}, nil
}
}
24 changes: 13 additions & 11 deletions exporter/exporterhelper/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestLogs_NilLogger(t *testing.T) {
}

func TestLogsRequest_NilLogger(t *testing.T) {
le, err := NewLogsRequest(context.Background(), exporter.Settings{}, (&internal.FakeRequestConverter{}).RequestFromLogsFunc)
le, err := NewLogsRequest(context.Background(), exporter.Settings{}, internal.RequestFromLogsFunc(nil))
require.Nil(t, le)
require.Equal(t, errNilLogger, err)
}
Expand Down Expand Up @@ -97,7 +97,7 @@ func TestLogs_Default(t *testing.T) {
func TestLogsRequest_Default(t *testing.T) {
ld := plog.NewLogs()
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromLogsFunc)
internal.RequestFromLogsFunc(nil))
assert.NotNil(t, le)
require.NoError(t, err)

Expand All @@ -119,7 +119,7 @@ func TestLogs_WithCapabilities(t *testing.T) {
func TestLogsRequest_WithCapabilities(t *testing.T) {
capabilities := consumer.Capabilities{MutatesData: true}
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromLogsFunc, WithCapabilities(capabilities))
internal.RequestFromLogsFunc(nil), WithCapabilities(capabilities))
require.NoError(t, err)
require.NotNil(t, le)

Expand All @@ -139,7 +139,9 @@ func TestLogsRequest_Default_ConvertError(t *testing.T) {
ld := plog.NewLogs()
want := errors.New("convert_error")
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{LogsError: want}).RequestFromLogsFunc)
func(context.Context, plog.Logs) (Request, error) {
return nil, want
})
require.NoError(t, err)
require.NotNil(t, le)
require.Equal(t, consumererror.NewPermanent(want), le.ConsumeLogs(context.Background(), ld))
Expand All @@ -149,7 +151,7 @@ func TestLogsRequest_Default_ExportError(t *testing.T) {
ld := plog.NewLogs()
want := errors.New("export_error")
le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{RequestError: want}).RequestFromLogsFunc)
internal.RequestFromLogsFunc(want))
require.NoError(t, err)
require.NotNil(t, le)
require.Equal(t, want, le.ConsumeLogs(context.Background(), ld))
Expand Down Expand Up @@ -213,7 +215,7 @@ func TestLogsRequest_WithRecordMetrics(t *testing.T) {

le, err := NewLogsRequest(context.Background(),
exporter.Settings{ID: fakeLogsName, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()},
(&internal.FakeRequestConverter{}).RequestFromLogsFunc)
internal.RequestFromLogsFunc(nil))
require.NoError(t, err)
require.NotNil(t, le)

Expand All @@ -240,7 +242,7 @@ func TestLogsRequest_WithRecordMetrics_ExportError(t *testing.T) {
t.Cleanup(func() { require.NoError(t, tt.Shutdown(context.Background())) })

le, err := NewLogsRequest(context.Background(), exporter.Settings{ID: fakeLogsName, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()},
(&internal.FakeRequestConverter{RequestError: want}).RequestFromLogsFunc)
internal.RequestFromLogsFunc(want))
require.NoError(t, err)
require.NotNil(t, le)

Expand Down Expand Up @@ -292,7 +294,7 @@ func TestLogsRequest_WithSpan(t *testing.T) {
otel.SetTracerProvider(set.TracerProvider)
defer otel.SetTracerProvider(nooptrace.NewTracerProvider())

le, err := NewLogsRequest(context.Background(), set, (&internal.FakeRequestConverter{}).RequestFromLogsFunc)
le, err := NewLogsRequest(context.Background(), set, internal.RequestFromLogsFunc(nil))
require.NoError(t, err)
require.NotNil(t, le)
checkWrapSpanForLogs(t, sr, set.TracerProvider.Tracer("test"), le, nil, 1)
Expand Down Expand Up @@ -320,7 +322,7 @@ func TestLogsRequest_WithSpan_ReturnError(t *testing.T) {
defer otel.SetTracerProvider(nooptrace.NewTracerProvider())

want := errors.New("my_error")
le, err := NewLogsRequest(context.Background(), set, (&internal.FakeRequestConverter{RequestError: want}).RequestFromLogsFunc)
le, err := NewLogsRequest(context.Background(), set, internal.RequestFromLogsFunc(want))
require.NoError(t, err)
require.NotNil(t, le)
checkWrapSpanForLogs(t, sr, set.TracerProvider.Tracer("test"), le, want, 1)
Expand All @@ -343,7 +345,7 @@ func TestLogsRequest_WithShutdown(t *testing.T) {
shutdown := func(context.Context) error { shutdownCalled = true; return nil }

le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromLogsFunc, WithShutdown(shutdown))
internal.RequestFromLogsFunc(nil), WithShutdown(shutdown))
assert.NotNil(t, le)
assert.NoError(t, err)

Expand All @@ -367,7 +369,7 @@ func TestLogsRequest_WithShutdown_ReturnError(t *testing.T) {
shutdownErr := func(context.Context) error { return want }

le, err := NewLogsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromLogsFunc, WithShutdown(shutdownErr))
internal.RequestFromLogsFunc(nil), WithShutdown(shutdownErr))
assert.NotNil(t, le)
require.NoError(t, err)

Expand Down
24 changes: 13 additions & 11 deletions exporter/exporterhelper/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func TestMetrics_NilLogger(t *testing.T) {

func TestMetricsRequest_NilLogger(t *testing.T) {
me, err := NewMetricsRequest(context.Background(), exporter.Settings{},
(&internal.FakeRequestConverter{}).RequestFromMetricsFunc)
internal.RequestFromMetricsFunc(nil))
require.Nil(t, me)
require.Equal(t, errNilLogger, err)
}
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestMetrics_Default(t *testing.T) {
func TestMetricsRequest_Default(t *testing.T) {
md := pmetric.NewMetrics()
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromMetricsFunc)
internal.RequestFromMetricsFunc(nil))
require.NoError(t, err)
assert.NotNil(t, me)

Expand All @@ -120,7 +120,7 @@ func TestMetrics_WithCapabilities(t *testing.T) {
func TestMetricsRequest_WithCapabilities(t *testing.T) {
capabilities := consumer.Capabilities{MutatesData: true}
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromMetricsFunc, WithCapabilities(capabilities))
internal.RequestFromMetricsFunc(nil), WithCapabilities(capabilities))
require.NoError(t, err)
assert.NotNil(t, me)

Expand All @@ -140,7 +140,9 @@ func TestMetricsRequest_Default_ConvertError(t *testing.T) {
md := pmetric.NewMetrics()
want := errors.New("convert_error")
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{MetricsError: want}).RequestFromMetricsFunc)
func(context.Context, pmetric.Metrics) (Request, error) {
return nil, want
})
require.NoError(t, err)
require.NotNil(t, me)
require.Equal(t, consumererror.NewPermanent(want), me.ConsumeMetrics(context.Background(), md))
Expand All @@ -150,7 +152,7 @@ func TestMetricsRequest_Default_ExportError(t *testing.T) {
md := pmetric.NewMetrics()
want := errors.New("export_error")
me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{RequestError: want}).RequestFromMetricsFunc)
internal.RequestFromMetricsFunc(want))
require.NoError(t, err)
require.NotNil(t, me)
require.Equal(t, want, me.ConsumeMetrics(context.Background(), md))
Expand Down Expand Up @@ -214,7 +216,7 @@ func TestMetricsRequest_WithRecordMetrics(t *testing.T) {

me, err := NewMetricsRequest(context.Background(),
exporter.Settings{ID: fakeMetricsName, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()},
(&internal.FakeRequestConverter{}).RequestFromMetricsFunc)
internal.RequestFromMetricsFunc(nil))
require.NoError(t, err)
require.NotNil(t, me)

Expand Down Expand Up @@ -242,7 +244,7 @@ func TestMetricsRequest_WithRecordMetrics_ExportError(t *testing.T) {

me, err := NewMetricsRequest(context.Background(),
exporter.Settings{ID: fakeMetricsName, TelemetrySettings: tt.TelemetrySettings(), BuildInfo: component.NewDefaultBuildInfo()},
(&internal.FakeRequestConverter{RequestError: want}).RequestFromMetricsFunc)
internal.RequestFromMetricsFunc(want))
require.NoError(t, err)
require.NotNil(t, me)

Expand Down Expand Up @@ -294,7 +296,7 @@ func TestMetricsRequest_WithSpan(t *testing.T) {
otel.SetTracerProvider(set.TracerProvider)
defer otel.SetTracerProvider(nooptrace.NewTracerProvider())

me, err := NewMetricsRequest(context.Background(), set, (&internal.FakeRequestConverter{}).RequestFromMetricsFunc)
me, err := NewMetricsRequest(context.Background(), set, internal.RequestFromMetricsFunc(nil))
require.NoError(t, err)
require.NotNil(t, me)
checkWrapSpanForMetrics(t, sr, set.TracerProvider.Tracer("test"), me, nil, 2)
Expand Down Expand Up @@ -322,7 +324,7 @@ func TestMetricsRequest_WithSpan_ExportError(t *testing.T) {
defer otel.SetTracerProvider(nooptrace.NewTracerProvider())

want := errors.New("my_error")
me, err := NewMetricsRequest(context.Background(), set, (&internal.FakeRequestConverter{RequestError: want}).RequestFromMetricsFunc)
me, err := NewMetricsRequest(context.Background(), set, internal.RequestFromMetricsFunc(want))
require.NoError(t, err)
require.NotNil(t, me)
checkWrapSpanForMetrics(t, sr, set.TracerProvider.Tracer("test"), me, want, 2)
Expand All @@ -346,7 +348,7 @@ func TestMetricsRequest_WithShutdown(t *testing.T) {
shutdown := func(context.Context) error { shutdownCalled = true; return nil }

me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromMetricsFunc, WithShutdown(shutdown))
internal.RequestFromMetricsFunc(nil), WithShutdown(shutdown))
assert.NotNil(t, me)
assert.NoError(t, err)

Expand All @@ -372,7 +374,7 @@ func TestMetricsRequest_WithShutdown_ReturnError(t *testing.T) {
shutdownErr := func(context.Context) error { return want }

me, err := NewMetricsRequest(context.Background(), exportertest.NewNopSettings(),
(&internal.FakeRequestConverter{}).RequestFromMetricsFunc, WithShutdown(shutdownErr))
internal.RequestFromMetricsFunc(nil), WithShutdown(shutdownErr))
assert.NotNil(t, me)
assert.NoError(t, err)

Expand Down
Loading

0 comments on commit e690ac5

Please sign in to comment.