Skip to content

Commit

Permalink
make exporter more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinnoel-be committed Jan 21, 2025
1 parent 2ea5c99 commit 57e9516
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions exporter/googlecloudpubsubexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ type pubsubExporter struct {
metricsWatermarkFunc metricsWatermarkFunc
logsMarshaler plog.Marshaler
logsWatermarkFunc logsWatermarkFunc

// To be overridden in tests
makeUUID func() (uuid.UUID, error)
makeClient func(ctx context.Context, cfg *Config, userAgent string) (publisherClient, error)
}

type encoding int
Expand Down Expand Up @@ -61,7 +65,7 @@ func (ex *pubsubExporter) start(ctx context.Context, _ component.Host) error {
ctx, ex.cancel = context.WithCancel(ctx)

if ex.client == nil {
client, err := newPublisherClient(ctx, ex.config, ex.userAgent)
client, err := ex.makeClient(ctx, ex.config, ex.userAgent)
if err != nil {
return fmt.Errorf("failed creating the gRPC client to Pubsub: %w", err)
}
Expand All @@ -82,7 +86,7 @@ func (ex *pubsubExporter) shutdown(_ context.Context) error {
}

func (ex *pubsubExporter) publishMessage(ctx context.Context, encoding encoding, data []byte, watermark time.Time) error {
id, err := uuid.NewRandom()
id, err := ex.makeUUID()
if err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions exporter/googlecloudpubsubexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/google/uuid"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/exporter"
Expand Down Expand Up @@ -51,6 +52,8 @@ func ensureExporter(params exporter.Settings, pCfg *Config) *pubsubExporter {
tracesMarshaler: &ptrace.ProtoMarshaler{},
metricsMarshaler: &pmetric.ProtoMarshaler{},
logsMarshaler: &plog.ProtoMarshaler{},
makeUUID: uuid.NewRandom,
makeClient: newPublisherClient,
}
// we ignore the error here as the config is already validated with the same method
receiver.ceCompression, _ = pCfg.parseCompression()
Expand Down

0 comments on commit 57e9516

Please sign in to comment.