Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[chore] add test helper to verify exporter behavior on errors #8143

Merged
merged 14 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add unit tests to contract checker
  • Loading branch information
omrozowicz-splunk authored and dmitryax committed Oct 23, 2023
commit fd0b333cf0f66fc09c3526c38488acf196567aa7
13 changes: 6 additions & 7 deletions exporter/exportertest/contract_checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func checkMetrics(params CheckConsumeContractParams, mockReceiver component.Comp
err = exp.ConsumeMetrics(ctx, data)
}

reqCounter := mockConsumer.reqCounter
reqCounter := mockConsumer.getRequestCounter()
// The overall number of requests sent by exporter
fmt.Printf("Number of export tries: %d\n", reqCounter.total)
// Successfully delivered items
Expand All @@ -132,7 +132,7 @@ func checkMetrics(params CheckConsumeContractParams, mockReceiver component.Comp
fmt.Printf("Number of non-permanent errors: %d\n", reqCounter.error.nonpermanent)

assert.EventuallyWithT(params.T, func(c *assert.CollectT) {
checkIfTestPassed(params.T, params.NumberOfTestElements, reqCounter)
checkIfTestPassed(params.T, params.NumberOfTestElements, *reqCounter)
}, 2*time.Second, 100*time.Millisecond)
}

Expand Down Expand Up @@ -163,7 +163,7 @@ func checkTraces(params CheckConsumeContractParams, mockReceiver component.Compo
err = exp.ConsumeTraces(ctx, data)
}

reqCounter := mockConsumer.reqCounter
reqCounter := mockConsumer.getRequestCounter()
// The overall number of requests sent by exporter
fmt.Printf("Number of export tries: %d\n", reqCounter.total)
// Successfully delivered items
Expand All @@ -173,7 +173,7 @@ func checkTraces(params CheckConsumeContractParams, mockReceiver component.Compo
fmt.Printf("Number of non-permanent errors: %d\n", reqCounter.error.nonpermanent)

assert.EventuallyWithT(params.T, func(c *assert.CollectT) {
checkIfTestPassed(params.T, params.NumberOfTestElements, reqCounter)
checkIfTestPassed(params.T, params.NumberOfTestElements, *reqCounter)
}, 2*time.Second, 100*time.Millisecond)
}

Expand Down Expand Up @@ -203,8 +203,7 @@ func checkLogs(params CheckConsumeContractParams, mockReceiver component.Compone

err = exp.ConsumeLogs(ctx, data)
}

reqCounter := mockConsumer.reqCounter
reqCounter := mockConsumer.getRequestCounter()
// The overall number of requests sent by exporter
fmt.Printf("Number of export tries: %d\n", reqCounter.total)
// Successfully delivered items
Expand All @@ -214,7 +213,7 @@ func checkLogs(params CheckConsumeContractParams, mockReceiver component.Compone
fmt.Printf("Number of non-permanent errors: %d\n", reqCounter.error.nonpermanent)

assert.EventuallyWithT(params.T, func(c *assert.CollectT) {
checkIfTestPassed(params.T, params.NumberOfTestElements, reqCounter)
checkIfTestPassed(params.T, params.NumberOfTestElements, *reqCounter)
}, 2*time.Second, 100*time.Millisecond)
}

Expand Down
265 changes: 265 additions & 0 deletions exporter/exportertest/contract_checker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,265 @@
package exportertest

import (
"context"
"testing"
"time"

"github.com/cenkalti/backoff/v4"
"go.opentelemetry.io/otel/metric/noop"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/plog"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.opentelemetry.io/collector/pdata/ptrace"
)

func (bor *testMockReceiver) Start(_ context.Context, _ component.Host) error {
return nil
}

func (bor *testMockReceiver) Shutdown(_ context.Context) error {
return nil
}

func newExampleFactory() exporter.Factory {
return exporter.NewFactory(
typeStr,
createDefaultConfig,
exporter.WithTraces(createTracesExporter, component.StabilityLevelStable),
exporter.WithMetrics(createMetricsExporter, component.StabilityLevelStable),
exporter.WithLogs(CreateLogsExporter, component.StabilityLevelBeta),
exporter.WithTraces(CreateTracesExporter, component.StabilityLevelBeta),
exporter.WithMetrics(CreateMetricsExporter, component.StabilityLevelBeta),
)
}

func CreateLogsExporter(_ context.Context, _ exporter.CreateSettings, cfg component.Config) (exporter.Logs, error) {
set := exporter.CreateSettings{
ID: component.NewIDWithName(component.DataTypeLogs, "test-exporter"),
TelemetrySettings: component.TelemetrySettings{
Logger: zap.NewNop(),
TracerProvider: trace.NewNoopTracerProvider(),
MeterProvider: noop.NewMeterProvider(),
MetricsLevel: 0,
Resource: pcommon.Resource{},
ReportComponentStatus: nil,
},
BuildInfo: component.BuildInfo{Version: "0.0.0"},
}
expConfig := cfg.(*ConnectionConfig)
le, err := exporterhelper.NewLogsExporter(
context.Background(),
set,
cfg,
pushLogs,
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(expConfig.RetrySettings),
exporterhelper.WithQueue(expConfig.QueueSettings),
exporterhelper.WithStart(startLogs))
return le, err
}

func CreateTracesExporter(_ context.Context, _ exporter.CreateSettings, cfg component.Config) (exporter.Traces, error) {
set := exporter.CreateSettings{
ID: component.NewIDWithName(component.DataTypeTraces, "test-exporter"),
TelemetrySettings: component.TelemetrySettings{
Logger: zap.NewNop(),
TracerProvider: trace.NewNoopTracerProvider(),
MeterProvider: noop.NewMeterProvider(),
MetricsLevel: 0,
Resource: pcommon.Resource{},
ReportComponentStatus: nil,
},
BuildInfo: component.BuildInfo{Version: "0.0.0"},
}
expConfig := cfg.(*ConnectionConfig)
le, err := exporterhelper.NewTracesExporter(
context.Background(),
set,
cfg,
pushTraces,
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(expConfig.RetrySettings),
exporterhelper.WithQueue(expConfig.QueueSettings),
exporterhelper.WithStart(startTraces))
return le, err
}

func startTraces(_ context.Context, _ component.Host) error {
return nil
}

func pushTraces(_ context.Context, _ ptrace.Traces) error {
return pushAny()
}

func CreateMetricsExporter(_ context.Context, _ exporter.CreateSettings, cfg component.Config) (exporter.Metrics, error) {
set := exporter.CreateSettings{
ID: component.NewIDWithName(component.DataTypeMetrics, "test-exporter"),
TelemetrySettings: component.TelemetrySettings{
Logger: zap.NewNop(),
TracerProvider: trace.NewNoopTracerProvider(),
MeterProvider: noop.NewMeterProvider(),
MetricsLevel: 0,
Resource: pcommon.Resource{},
ReportComponentStatus: nil,
},
BuildInfo: component.BuildInfo{Version: "0.0.0"},
}
expConfig := cfg.(*ConnectionConfig)
le, err := exporterhelper.NewMetricsExporter(
context.Background(),
set,
cfg,
pushMetrics,
exporterhelper.WithTimeout(exporterhelper.TimeoutSettings{Timeout: 0}),
exporterhelper.WithRetry(expConfig.RetrySettings),
exporterhelper.WithQueue(expConfig.QueueSettings),
exporterhelper.WithStart(startMetrics))
return le, err
}

func pushAny() error {
joinedRequestCounter.total++
generatedError := fakeMockConsumer.exportErrorFunction()
if generatedError != nil {
if consumererror.IsPermanent(generatedError) {
joinedRequestCounter.error.permanent++
} else {
joinedRequestCounter.error.nonpermanent++
}
return generatedError
}
joinedRequestCounter.success++
return nil
}

func pushMetrics(_ context.Context, _ pmetric.Metrics) error {
return pushAny()
}

func startMetrics(_ context.Context, _ component.Host) error {
return nil
}

func startLogs(_ context.Context, _ component.Host) error {
return nil
}

func pushLogs(_ context.Context, _ plog.Logs) error {
return pushAny()
}

func createDefaultConfig() component.Config {
return &ConnectionConfig{
TimeoutSettings: exporterhelper.NewDefaultTimeoutSettings(),
RetrySettings: exporterhelper.NewDefaultRetrySettings(),
QueueSettings: exporterhelper.NewDefaultQueueSettings(),
}
}

func newTestRetrySettings() exporterhelper.RetrySettings {
return exporterhelper.RetrySettings{
Enabled: true,
// interval is short for the test purposes
InitialInterval: 10 * time.Millisecond,
RandomizationFactor: backoff.DefaultRandomizationFactor,
Multiplier: 1.1,
MaxInterval: 10 * time.Second,
MaxElapsedTime: 1 * time.Minute,
}
}

type ConnectionConfig struct {
exporterhelper.TimeoutSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
exporterhelper.QueueSettings `mapstructure:"sending_queue"`
exporterhelper.RetrySettings `mapstructure:"retry_on_failure"`
}

func testConfig() *ConnectionConfig {
return &ConnectionConfig{
TimeoutSettings: exporterhelper.TimeoutSettings{},
QueueSettings: exporterhelper.QueueSettings{Enabled: false},
RetrySettings: newTestRetrySettings(),
}
}

type testMockReceiver struct {
mockConsumer *MockConsumer
}

func newOTLPDataReceiver(mockConsumer *MockConsumer) *testMockReceiver {
return &testMockReceiver{mockConsumer: mockConsumer}
}

var joinedRequestCounter = newRequestCounter()

var fakeMockConsumer = &MockConsumer{reqCounter: joinedRequestCounter}

// Define a function that matches the MockReceiverFactory signature
func createMockOtlpReceiver(mockConsumer *MockConsumer) component.Component {
rcv := newOTLPDataReceiver(mockConsumer)
joinedRequestCounter = newRequestCounter()
fakeMockConsumer.exportErrorFunction = mockConsumer.exportErrorFunction
mockConsumer.reqCounter = joinedRequestCounter
err := rcv.Start(context.Background(), nil)
if err != nil {
return nil
}
return rcv
}

func TestCheckConsumeContractLogs(t *testing.T) {

// Create a CheckConsumeContractParams
params := CheckConsumeContractParams{
T: t,
Factory: newExampleFactory(), // Replace with your exporter factory
DataType: component.DataTypeLogs, // Change to the appropriate data type
Config: testConfig(),
NumberOfTestElements: 10, // Number of test elements you want to send
MockReceiverFactory: createMockOtlpReceiver,
}

// Run the contract tests
CheckConsumeContract(params)
}

func TestCheckConsumeContractMetrics(t *testing.T) {

// Create a CheckConsumeContractParams
params := CheckConsumeContractParams{
T: t,
Factory: newExampleFactory(), // Replace with your exporter factory
DataType: component.DataTypeMetrics, // Change to the appropriate data type
Config: testConfig(),
NumberOfTestElements: 10, // Number of test elements you want to send
MockReceiverFactory: createMockOtlpReceiver,
}

// Run the contract tests
CheckConsumeContract(params)
}

func TestCheckConsumeContractTraces(t *testing.T) {

// Create a CheckConsumeContractParams
params := CheckConsumeContractParams{
T: t,
Factory: newExampleFactory(), // Replace with your exporter factory
DataType: component.DataTypeTraces, // Change to the appropriate data type
Config: testConfig(),
NumberOfTestElements: 10, // Number of test elements you want to send
MockReceiverFactory: createMockOtlpReceiver,
}

// Run the contract tests
CheckConsumeContract(params)
}
11 changes: 8 additions & 3 deletions exporter/exportertest/mock_receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func randomErrorsConsumeDecision() error {
}

type MockConsumer struct {
reqCounter requestCounter
reqCounter *requestCounter
mux sync.Mutex
exportErrorFunction func() error
receivedTraces []ptrace.Traces
Expand All @@ -79,6 +79,7 @@ func newMockConsumer(decisionFunc func() error) MockConsumer {
}

func (r *MockConsumer) ConsumeLogs(_ context.Context, ld plog.Logs) error {
fmt.Println("Inside CreateLogsExporter")
r.mux.Lock()
defer r.mux.Unlock()
r.reqCounter.total++
Expand Down Expand Up @@ -148,6 +149,10 @@ func (r *MockConsumer) clear() {
r.reqCounter = newRequestCounter()
}

func (r *MockConsumer) getRequestCounter() *requestCounter {
return r.reqCounter
}

type requestCounter struct {
success int
error errorCounter
Expand All @@ -166,8 +171,8 @@ func newErrorCounter() errorCounter {
}
}

func newRequestCounter() requestCounter {
return requestCounter{
func newRequestCounter() *requestCounter {
return &requestCounter{
success: 0,
error: newErrorCounter(),
total: 0,
Expand Down
2 changes: 1 addition & 1 deletion exporter/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.26.0
golang.org/x/sys v0.13.0
google.golang.org/grpc v1.59.0
)

require (
Expand Down Expand Up @@ -56,7 +57,6 @@ require (
golang.org/x/net v0.17.0 // indirect
golang.org/x/text v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230822172742-b8732ec3820d // indirect
google.golang.org/grpc v1.59.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions exporter/otlpexporter/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
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/config/confighttp v0.87.0 h1:FOC4ArxbvJRiwABXsv/bSrRlD3m9nAEAACEYXmpNC+g=
go.opentelemetry.io/collector/config/confighttp v0.87.0/go.mod h1:Vt4DECSuhncd/bTKU3pB6MUjHwBKfPqiIkFg5fHJHIE=
go.opentelemetry.io/collector/receiver/otlpreceiver v0.87.0 h1:iXO30EKZwEP1TEuLlQjxVaeVeffDkdJqz9DuqjzME9c=
go.opentelemetry.io/collector/receiver/otlpreceiver v0.87.0/go.mod h1:1IE82wJuyGW0z0BeJ3A0SoPxsPlqf9aefCycbtuxUO0=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.45.0 h1:RsQi0qJ2imFfCvZabqzM9cNXBG8k6gXMv1A0cXRmH6A=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.45.0/go.mod h1:vsh3ySueQCiKPxFLvjWC4Z135gIa34TQ/NSqkDTZYUM=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 h1:x8Z78aZx8cOF0+Kkazoc7lwUNMGy0LrzEMxTm4BbTxg=
Expand Down