diff --git a/exporter/cassandraexporter/config.go b/exporter/cassandraexporter/config.go index 89961fb7b957..313496b0a58c 100644 --- a/exporter/cassandraexporter/config.go +++ b/exporter/cassandraexporter/config.go @@ -2,7 +2,9 @@ // SPDX-License-Identifier: Apache-2.0 package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter" -import "go.opentelemetry.io/collector/config/configopaque" +import ( + "go.opentelemetry.io/collector/config/configopaque" +) type Config struct { DSN string `mapstructure:"dsn"` diff --git a/exporter/cassandraexporter/exporter_logs.go b/exporter/cassandraexporter/exporter_logs.go index 43b8051e0b8e..0536b5fc5819 100644 --- a/exporter/cassandraexporter/exporter_logs.go +++ b/exporter/cassandraexporter/exporter_logs.go @@ -24,18 +24,10 @@ type logsExporter struct { } func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -45,9 +37,7 @@ func newLogsExporter(logger *zap.Logger, cfg *Config) (*logsExporter, error) { func initializeLogKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil { @@ -68,6 +58,19 @@ func initializeLogKernel(cfg *Config) error { return nil } +func newCluster(cfg *Config) *gocql.ClusterConfig { + cluster := gocql.NewCluster(cfg.DSN) + if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { + cluster.Authenticator = gocql.PasswordAuthenticator{ + Username: cfg.Auth.UserName, + Password: string(cfg.Auth.Password), + } + } + cluster.Consistency = gocql.Quorum + cluster.Port = cfg.Port + return cluster +} + func (e *logsExporter) Start(_ context.Context, _ component.Host) error { initializeErr := initializeLogKernel(e.cfg) return initializeErr diff --git a/exporter/cassandraexporter/exporter_logs_test.go b/exporter/cassandraexporter/exporter_logs_test.go new file mode 100644 index 000000000000..0b9b1e7f77fb --- /dev/null +++ b/exporter/cassandraexporter/exporter_logs_test.go @@ -0,0 +1,59 @@ +// Copyright The OpenTelemetry Authors +// SPDX-License-Identifier: Apache-2.0 + +package cassandraexporter + +import ( + "github.com/gocql/gocql" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestNewCluster(t *testing.T) { + testCases := map[string]struct { + cfg *Config + expectedAuthenticator gocql.Authenticator + }{ + "empty_auth": { + cfg: withDefaultConfig(), + expectedAuthenticator: nil, + }, + "empty_username": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.Password = "pass" + }), + expectedAuthenticator: nil, + }, + "empty_password": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + }), + expectedAuthenticator: nil, + }, + "success_auth": { + cfg: withDefaultConfig(func(config *Config) { + config.Auth.UserName = "user" + config.Auth.Password = "pass" + }), + expectedAuthenticator: gocql.PasswordAuthenticator{ + Username: "user", + Password: "pass", + }, + }, + } + for name, test := range testCases { + t.Run(name, func(t *testing.T) { + c := newCluster(test.cfg) + require.Equal(t, test.expectedAuthenticator, c.Authenticator) + }) + } +} + +func withDefaultConfig(fns ...func(*Config)) *Config { + cfg := createDefaultConfig().(*Config) + for _, fn := range fns { + fn(cfg) + } + return cfg +} diff --git a/exporter/cassandraexporter/exporter_traces.go b/exporter/cassandraexporter/exporter_traces.go index e7f34ded49b0..c9eebb91e059 100644 --- a/exporter/cassandraexporter/exporter_traces.go +++ b/exporter/cassandraexporter/exporter_traces.go @@ -23,18 +23,10 @@ type tracesExporter struct { } func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) { - cluster := gocql.NewCluster(cfg.DSN) - if cfg.Auth.UserName != "" && cfg.Auth.Password != "" { - cluster.Authenticator = gocql.PasswordAuthenticator{ - Username: cfg.Auth.UserName, - Password: string(cfg.Auth.Password), - } - } - session, err := cluster.CreateSession() + cluster := newCluster(cfg) cluster.Keyspace = cfg.Keyspace - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + session, err := cluster.CreateSession() if err != nil { return nil, err } @@ -44,9 +36,7 @@ func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) func initializeTraceKernel(cfg *Config) error { ctx := context.Background() - cluster := gocql.NewCluster(cfg.DSN) - cluster.Consistency = gocql.Quorum - cluster.Port = cfg.Port + cluster := newCluster(cfg) session, err := cluster.CreateSession() if err != nil {