diff --git a/receiver/mysqlreceiver/README.md b/receiver/mysqlreceiver/README.md index 631f4d31e9ed..b0bb1b0f311e 100644 --- a/receiver/mysqlreceiver/README.md +++ b/receiver/mysqlreceiver/README.md @@ -24,6 +24,8 @@ The following settings are optional: - `collection_interval` (default = `10s`): This receiver collects metrics on an interval. This value must be a string readable by Golang's [time.ParseDuration](https://pkg.go.dev/time#ParseDuration). Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`. +- `transport`: (default = `tcp`): Defines the network to use for connecting to the server. + ### Example Configuration ```yaml diff --git a/receiver/mysqlreceiver/client.go b/receiver/mysqlreceiver/client.go index f230c510ae51..d34bca7205f3 100644 --- a/receiver/mysqlreceiver/client.go +++ b/receiver/mysqlreceiver/client.go @@ -23,35 +23,34 @@ import ( ) type client interface { + Connect() error getGlobalStats() (map[string]string, error) getInnodbStats() (map[string]string, error) Close() error } type mySQLClient struct { - client *sql.DB + connStr string + client *sql.DB } var _ client = (*mySQLClient)(nil) -type mySQLConfig struct { - username string - password string - database string - endpoint string -} +func newMySQLClient(conf *Config) client { + connStr := fmt.Sprintf("%s:%s@%s(%s)/%s", conf.Username, conf.Password, conf.Transport, conf.Endpoint, conf.Database) -func newMySQLClient(conf mySQLConfig) (*mySQLClient, error) { - connStr := fmt.Sprintf("%s:%s@tcp(%s)/%s", conf.username, conf.password, conf.endpoint, conf.database) + return &mySQLClient{ + connStr: connStr, + } +} - db, err := sql.Open("mysql", connStr) +func (c *mySQLClient) Connect() error { + clientDB, err := sql.Open("mysql", c.connStr) if err != nil { - return nil, err + return fmt.Errorf("unable to connect to database: %w", err) } - - return &mySQLClient{ - client: db, - }, nil + c.client = clientDB + return nil } // getGlobalStats queries the db for global status metrics. @@ -85,5 +84,8 @@ func Query(c mySQLClient, query string) (map[string]string, error) { } func (c *mySQLClient) Close() error { - return c.client.Close() + if c.client != nil { + return c.client.Close() + } + return nil } diff --git a/receiver/mysqlreceiver/client_mock_test.go b/receiver/mysqlreceiver/client_mock_test.go index f6d6325a18e5..6424afb614d4 100644 --- a/receiver/mysqlreceiver/client_mock_test.go +++ b/receiver/mysqlreceiver/client_mock_test.go @@ -23,8 +23,7 @@ import ( var _ client = (*fakeClient)(nil) -type fakeClient struct { -} +type fakeClient struct{} func readFile(fname string) (map[string]string, error) { var stats = map[string]string{} @@ -42,6 +41,10 @@ func readFile(fname string) (map[string]string, error) { return stats, nil } +func (c *fakeClient) Connect() error { + return nil +} + func (c *fakeClient) getGlobalStats() (map[string]string, error) { return readFile("global_stats") } diff --git a/receiver/mysqlreceiver/config.go b/receiver/mysqlreceiver/config.go index 2a2deba4a1a2..c241a7b8e84a 100644 --- a/receiver/mysqlreceiver/config.go +++ b/receiver/mysqlreceiver/config.go @@ -17,6 +17,7 @@ package mysqlreceiver import ( "errors" + "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/receiver/scraperhelper" "go.uber.org/multierr" ) @@ -26,7 +27,7 @@ type Config struct { Username string `mapstructure:"username"` Password string `mapstructure:"password"` Database string `mapstructure:"database"` - Endpoint string `mapstructure:"endpoint"` + confignet.NetAddr `mapstructure:",squash"` } // Errors for missing required config parameters. diff --git a/receiver/mysqlreceiver/factory.go b/receiver/mysqlreceiver/factory.go index 0f6cb57826b9..25f8840c9e6c 100644 --- a/receiver/mysqlreceiver/factory.go +++ b/receiver/mysqlreceiver/factory.go @@ -22,6 +22,7 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/receiver/receiverhelper" "go.opentelemetry.io/collector/receiver/scraperhelper" @@ -44,7 +45,10 @@ func createDefaultConfig() config.Receiver { ReceiverSettings: config.NewReceiverSettings(config.NewComponentID(typeStr)), CollectionInterval: 10 * time.Second, }, - Endpoint: "localhost:3306", + NetAddr: confignet.NetAddr{ + Endpoint: "localhost:3306", + Transport: "tcp", + }, } } diff --git a/receiver/mysqlreceiver/factory_test.go b/receiver/mysqlreceiver/factory_test.go index d34869b5bf6b..a355e6026e01 100644 --- a/receiver/mysqlreceiver/factory_test.go +++ b/receiver/mysqlreceiver/factory_test.go @@ -22,6 +22,7 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/component/componenttest" "go.opentelemetry.io/collector/config" + "go.opentelemetry.io/collector/config/confignet" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/receiver/scraperhelper" ) @@ -53,7 +54,9 @@ func TestCreateMetricsReceiver(t *testing.T) { }, Username: "otel", Password: "otel", - Endpoint: "localhost:3306", + NetAddr: confignet.NetAddr{ + Endpoint: "localhost:3306", + }, }, consumertest.NewNop(), ) diff --git a/receiver/mysqlreceiver/go.mod b/receiver/mysqlreceiver/go.mod index 5c0f47525507..ac85cd8e716c 100644 --- a/receiver/mysqlreceiver/go.mod +++ b/receiver/mysqlreceiver/go.mod @@ -4,7 +4,7 @@ go 1.17 require ( github.com/go-sql-driver/mysql v1.6.0 - github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.0.0-00010101000000-000000000000 + github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest v0.0.0-20211110181051-0d46baeab9e9 github.com/stretchr/testify v1.7.0 go.opentelemetry.io/collector v0.38.1-0.20211103215828-cffbecb2ac9e go.opentelemetry.io/collector/model v0.38.1-0.20211103215828-cffbecb2ac9e diff --git a/receiver/mysqlreceiver/scraper.go b/receiver/mysqlreceiver/scraper.go index 3ee1be2e3942..a5bbeb54d61c 100644 --- a/receiver/mysqlreceiver/scraper.go +++ b/receiver/mysqlreceiver/scraper.go @@ -29,8 +29,8 @@ import ( ) type mySQLScraper struct { - client client - stopOnce sync.Once + sqlclient client + stopOnce sync.Once logger *zap.Logger config *Config @@ -48,16 +48,13 @@ func newMySQLScraper( // start starts the scraper by initializing the db client connection. func (m *mySQLScraper) start(_ context.Context, host component.Host) error { - client, err := newMySQLClient(mySQLConfig{ - username: m.config.Username, - password: m.config.Password, - database: m.config.Database, - endpoint: m.config.Endpoint, - }) + sqlclient := newMySQLClient(m.config) + + err := sqlclient.Connect() if err != nil { return err } - m.client = client + m.sqlclient = sqlclient return nil } @@ -66,7 +63,7 @@ func (m *mySQLScraper) start(_ context.Context, host component.Host) error { func (m *mySQLScraper) shutdown(context.Context) error { var err error m.stopOnce.Do(func() { - err = m.client.Close() + err = m.sqlclient.Close() }) return err } @@ -100,8 +97,7 @@ func addToIntMetric(metric pdata.NumberDataPointSlice, labels pdata.AttributeMap // scrape scrapes the mysql db metric stats, transforms them and labels them into a metric slices. func (m *mySQLScraper) scrape(context.Context) (pdata.Metrics, error) { - - if m.client == nil { + if m.sqlclient == nil { return pdata.Metrics{}, errors.New("failed to connect to http client") } @@ -127,7 +123,7 @@ func (m *mySQLScraper) scrape(context.Context) (pdata.Metrics, error) { threads := initMetric(ilm.Metrics(), metadata.M.MysqlThreads).Sum().DataPoints() // collect innodb metrics. - innodbStats, err := m.client.getInnodbStats() + innodbStats, err := m.sqlclient.getInnodbStats() if err != nil { m.logger.Error("Failed to fetch InnoDB stats", zap.Error(err)) } @@ -144,7 +140,7 @@ func (m *mySQLScraper) scrape(context.Context) (pdata.Metrics, error) { } // collect global status metrics. - globalStats, err := m.client.getGlobalStats() + globalStats, err := m.sqlclient.getGlobalStats() if err != nil { m.logger.Error("Failed to fetch global stats", zap.Error(err)) return pdata.Metrics{}, err diff --git a/receiver/mysqlreceiver/scraper_test.go b/receiver/mysqlreceiver/scraper_test.go index 3f9bf780db78..ef95ff3c034e 100644 --- a/receiver/mysqlreceiver/scraper_test.go +++ b/receiver/mysqlreceiver/scraper_test.go @@ -20,6 +20,7 @@ import ( "testing" "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/config/confignet" "go.uber.org/zap" "github.com/open-telemetry/opentelemetry-collector-contrib/internal/scrapertest" @@ -30,9 +31,11 @@ func TestScrape(t *testing.T) { sc := newMySQLScraper(zap.NewNop(), &Config{ Username: "otel", Password: "otel", - Endpoint: "localhost:3306", + NetAddr: confignet.NetAddr{ + Endpoint: "localhost:3306", + }, }) - sc.client = &mysqlMock + sc.sqlclient = &mysqlMock scrapedRMS, err := sc.scrape(context.Background()) require.NoError(t, err)