Skip to content

Commit

Permalink
added tests for row-ops metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
naman47vyas committed Jul 25, 2024
1 parent ee6c7e9 commit dc00aaf
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 21 deletions.
11 changes: 1 addition & 10 deletions receiver/mysqlreceiver/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,16 +253,7 @@ func (c *mySQLClient) getRowOperationStats() (RowOperationStats, error) {

rows, err := c.client.Query(query)
rowOpsStats := new(RowOperationStats)
/*
+----------------+-----------------+---------------+-----------+---------------+
| statement_type | execution_count | rows_affected | rows_sent | rows_examined |
+----------------+-----------------+---------------+-----------+---------------+
| SELECT | 4862 | 0 | 246533 | 43871844 |
| UPDATE | 2800 | 395 | 0 | 480395 |
| DELETE | 400 | 0 | 0 | 79800 |
| INSERT | 800 | 400 | 0 | 80200 |
+----------------+-----------------+---------------+-----------+---------------+
*/

if err != nil {
return *rowOpsStats, err
}
Expand Down
2 changes: 1 addition & 1 deletion receiver/mysqlreceiver/metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ metrics:
attributes: [dbname]

mysql.statement_event.errors:
enabled: true
enabled: true
description: the error count of the summarized events
unit: 1
sum:
Expand Down
7 changes: 0 additions & 7 deletions receiver/mysqlreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"go.opentelemetry.io/collector/receiver/scrapererror"
"go.uber.org/zap"

"github.com/k0kubun/pp"
"github.com/open-telemetry/opentelemetry-collector-contrib/receiver/mysqlreceiver/internal/metadata"
)

Expand Down Expand Up @@ -136,7 +135,6 @@ func (m *mySQLScraper) scrape(context.Context) (pmetric.Metrics, error) {
func (m *mySQLScraper) scrapeRowOperationStats(now pcommon.Timestamp, errs *scrapererror.ScrapeErrors) {
rowOperationStats, err := m.sqlclient.getRowOperationStats()
if err != nil {
pp.Print(err)
m.logger.Error("Failed to fetch row operation stats from performance schema", zap.Error(err))
errs.AddPartial(4, err)
return
Expand All @@ -146,11 +144,6 @@ func (m *mySQLScraper) scrapeRowOperationStats(now pcommon.Timestamp, errs *scra
rowsUpdated := strconv.FormatInt(rowOperationStats.rowsUpdated, 10)
rowsRead := strconv.FormatInt(rowOperationStats.rowsInserted, 10)

pp.Println(rowsDeleted)
pp.Println(rowsInserted)
pp.Println(rowsUpdated)
pp.Println(rowsRead)

m.mb.RecordMysqlPerformanceRowsDeletedDataPoint(now, rowsDeleted)
m.mb.RecordMysqlPerformanceRowsInsertedDataPoint(now, rowsInserted)
m.mb.RecordMysqlPerformanceRowsUpdatedDataPoint(now, rowsUpdated)
Expand Down
54 changes: 51 additions & 3 deletions receiver/mysqlreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func TestScrape(t *testing.T) {
innodbStatusStatsFile: "innodb_status_stats",
totalRowsFile: "total_rows_stats",
totalErrorsFile: "total_error_stats",
rowOperationsStatsFile: "row_operations_status",
}

scraper.renameCommands = true
Expand Down Expand Up @@ -119,6 +120,7 @@ func TestScrape(t *testing.T) {
innodbStatusStatsFile: "innodb_status_stats_empty",
totalRowsFile: "total_rows_empty",
totalErrorsFile: "total_errors_empty",
rowOperationsStatsFile: "row_operations_status_empty",
}

actualMetrics, scrapeErr := scraper.scrape(context.Background())
Expand All @@ -127,9 +129,15 @@ func TestScrape(t *testing.T) {
expectedFile := filepath.Join("testdata", "scraper", "expected_partial.yaml")
expectedMetrics, err := golden.ReadMetrics(expectedFile)
require.NoError(t, err)
assert.NoError(t, pmetrictest.CompareMetrics(actualMetrics, expectedMetrics,
pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp()))
assert.NoError(t, pmetrictest.CompareMetrics(
actualMetrics,
expectedMetrics,
pmetrictest.IgnoreMetricsOrder(),
pmetrictest.IgnoreMetricDataPointsOrder(),
pmetrictest.IgnoreStartTimestamp(),
pmetrictest.IgnoreTimestamp(),
),
)

var partialError scrapererror.PartialScrapeError
require.True(t, errors.As(scrapeErr, &partialError), "returned error was not PartialScrapeError")
Expand All @@ -154,6 +162,7 @@ type mockClient struct {
innodbStatusStatsFile string
totalRowsFile string
totalErrorsFile string
rowOperationsStatsFile string
}

func readFile(fname string) (map[string]string, error) {
Expand Down Expand Up @@ -188,6 +197,45 @@ func (c *mockClient) getInnodbStats() (map[string]string, error) {
return readFile(c.innodbStatsFile)
}

func (c *mockClient) getRowOperationStats() (RowOperationStats, error) {
rowOpsStats := new(RowOperationStats)
file, err := os.Open(filepath.Join("testdata", "scraper", c.rowOperationsStatsFile+".txt"))

if err != nil {
return *rowOpsStats, err
}

defer file.Close()

scanner := bufio.NewScanner(file)
for scanner.Scan() {

text := strings.Fields(scanner.Text())
rowsInserted, err := strconv.Atoi(text[0])
if err != nil {
return *rowOpsStats, err
}
rowsUpdated, err := strconv.Atoi(text[1])
if err != nil {
return *rowOpsStats, err
}
rowsRead, err := strconv.Atoi(text[2])
if err != nil {
return *rowOpsStats, err
}
rowsDeleted, err := strconv.Atoi(text[3])
if err != nil {
return *rowOpsStats, err
}

rowOpsStats.rowsDeleted = int64(rowsDeleted)
rowOpsStats.rowsInserted = int64(rowsInserted)
rowOpsStats.rowsRead = int64(rowsRead)
rowOpsStats.rowsUpdated = int64(rowsUpdated)
}
return *rowOpsStats, nil
}

func (c *mockClient) getInnodbStatusStats() (map[string]int64, error, int) {
ret := make(map[string]int64)
var totalErrs int
Expand Down
40 changes: 40 additions & 0 deletions receiver/mysqlreceiver/testdata/scraper/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,46 @@ resourceMetrics:
timeUnixNano: "2000000"
isMonotonic: true
unit: "1"
- description: The number of rows inserted in the database as per the performance schema.
name: mysql.performance.rows_inserted
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "100"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: The number of rows read in the database as per the performance schema.
name: mysql.performance.rows_read
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "100"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: The number of rows updated in the database as per the performance schema.
name: mysql.performance.rows_updated
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "100"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: The number of rows deleted in the database as per the performance schema.
name: mysql.performance.rows_deleted
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "100"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: Rate at which rows are being deleted in InnoDB.
name: mysql.innodb.rows_deleted
sum:
Expand Down
40 changes: 40 additions & 0 deletions receiver/mysqlreceiver/testdata/scraper/expected_partial.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,46 @@ resourceMetrics:
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
unit: "1"
- description: The number of rows inserted in the database as per the performance schema.
name: mysql.performance.rows_inserted
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "0"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: The number of rows read in the database as per the performance schema.
name: mysql.performance.rows_read
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "0"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: The number of rows updated in the database as per the performance schema.
name: mysql.performance.rows_updated
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "0"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
- description: The number of rows deleted in the database as per the performance schema.
name: mysql.performance.rows_deleted
sum:
aggregationTemporality: 2
dataPoints:
- asInt: "0"
startTimeUnixNano: "1000000"
timeUnixNano: "2000000"
isMonotonic: true
unit: '{row}'
scope:
name: otelcol/mysqlreceiver
version: latest
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100 100 100 100
Empty file.

0 comments on commit dc00aaf

Please sign in to comment.