Skip to content

Commit

Permalink
[chore][receiver/mysql] Migrate away from timing based setup in integ…
Browse files Browse the repository at this point in the history
…ration test (#23003)

* [chore][receiver/mysql] Migrate away from timing based setup in integration test
  • Loading branch information
djaglowski authored Jun 5, 2023
1 parent 3844242 commit 6b85542
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 69 deletions.
20 changes: 19 additions & 1 deletion internal/coreinternal/scraperinttest/scraperint.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ type IntegrationTest struct {
compareOptions []pmetrictest.CompareMetricsOption
compareTimeout time.Duration

writeExpected bool
failOnErrorLogs bool
writeExpected bool
}

func (it *IntegrationTest) Run(t *testing.T) {
Expand Down Expand Up @@ -122,6 +123,14 @@ func (it *IntegrationTest) Run(t *testing.T) {
if len(allMetrics) == 0 {
return false
}
if it.failOnErrorLogs && len(observedLogs.All()) > 0 {
logs := strings.Builder{}
for _, e := range observedLogs.All() {
logs.WriteString(e.Message + "\n")
}
t.Errorf("full log:\n%s", logs.String())
}

if it.writeExpected {
require.NoError(t, golden.WriteMetrics(t, it.expectedFile, allMetrics[0]))
return true
Expand Down Expand Up @@ -229,6 +238,15 @@ func WithExpectedFile(f string) TestOption {
}
}

// This option is useful for debugging scrapers but should not be used permanently
// because the logs do not correlate to a single scrape interval. In other words,
// when a retryable failure occurs, this setting will likely force a failure anyways.
func FailOnErrorLogs() TestOption {
return func(it *IntegrationTest) {
it.failOnErrorLogs = true
}
}

func WriteExpected() TestOption {
return func(it *IntegrationTest) {
it.writeExpected = true
Expand Down
2 changes: 1 addition & 1 deletion receiver/mysqlreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ This receiver queries MySQL's global status and InnoDB tables.

This receiver supports MySQL version 8.0

Collecting most metrics requires the ability to execute `SHOW GLOBAL STATUS`. The `buffer_pool_size` metric requires access to the `information_schema.innodb_metrics` table. Please refer to [setup.sh](./testdata/integration/scripts/setup.sh) for an example of how to configure these permissions.
Collecting most metrics requires the ability to execute `SHOW GLOBAL STATUS`.

## Configuration

Expand Down
21 changes: 13 additions & 8 deletions receiver/mysqlreceiver/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ func TestIntegration(t *testing.T) {
NewFactory(),
scraperinttest.WithContainerRequest(
testcontainers.ContainerRequest{
FromDockerfile: testcontainers.FromDockerfile{
Context: filepath.Join("testdata", "integration"),
Dockerfile: "Dockerfile.mysql",
},
Image: "mysql:8.0.33",
ExposedPorts: []string{mysqlPort},
WaitingFor: wait.ForListeningPort(mysqlPort).
WithStartupTimeout(2 * time.Minute),
LifecycleHooks: []testcontainers.ContainerLifecycleHooks{{
PostStarts: []testcontainers.ContainerHook{
scraperinttest.RunScript([]string{"/setup.sh"}),
Env: map[string]string{
"MYSQL_ROOT_PASSWORD": "otel",
"MYSQL_DATABASE": "otel",
"MYSQL_USER": "otel",
"MYSQL_PASSWORD": "otel",
},
Files: []testcontainers.ContainerFile{
{
HostFilePath: filepath.Join("testdata", "integration", "init.sh"),
ContainerFilePath: "/docker-entrypoint-initdb.d/init.sh",
FileMode: 700,
},
}},
},
}),
scraperinttest.WithCustomConfig(
func(t *testing.T, cfg component.Config, ci *scraperinttest.ContainerInfo) {
Expand Down
7 changes: 3 additions & 4 deletions receiver/mysqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (m *mySQLScraper) scrape(context.Context) (pmetric.Metrics, error) {
m.scrapeGlobalStats(now, errs)

// colect replicas status metrics.
m.scrapeReplicaStatusStats(now, errs)
m.scrapeReplicaStatusStats(now)

m.mb.EmitForResource(metadata.WithMysqlInstanceEndpoint(m.config.Endpoint))

Expand Down Expand Up @@ -532,11 +532,10 @@ func (m *mySQLScraper) scrapeTableLockWaitEventStats(now pcommon.Timestamp, errs
}
}

func (m *mySQLScraper) scrapeReplicaStatusStats(now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
func (m *mySQLScraper) scrapeReplicaStatusStats(now pcommon.Timestamp) {
replicaStatusStats, err := m.sqlclient.getReplicaStatusStats()
if err != nil {
m.logger.Error("Failed to fetch replica status stats", zap.Error(err))
errs.AddPartial(8, err)
m.logger.Info("Failed to fetch replica status stats", zap.Error(err))
return
}

Expand Down
9 changes: 0 additions & 9 deletions receiver/mysqlreceiver/testdata/integration/Dockerfile.mysql

This file was deleted.

14 changes: 14 additions & 0 deletions receiver/mysqlreceiver/testdata/integration/init.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

# Copyright The OpenTelemetry Authors
# SPDX-License-Identifier: Apache-2.0

set -e

USER="otel"
ROOT_PASS="otel"

# # NOTE: -pPASSWORD is missing a space on purpose
mysql -u root -p"${ROOT_PASS}" -e "GRANT PROCESS ON *.* TO ${USER}" > /dev/null
mysql -u root -p"${ROOT_PASS}" -e "GRANT SELECT ON performance_schema.* TO ${USER}" > /dev/null
mysql -u root -p"${ROOT_PASS}" -e "FLUSH PRIVILEGES" > /dev/null
46 changes: 0 additions & 46 deletions receiver/mysqlreceiver/testdata/integration/scripts/setup.sh

This file was deleted.

0 comments on commit 6b85542

Please sign in to comment.