Skip to content

Commit 7ddb350

Browse files
authored
[receiver/mysql] feat: add table size metrics (#32680)
**Description:** This adds table size metrics using the INFORMATION_SCHEMA TABLES table: https://dev.mysql.com/doc/refman/8.3/en/information-schema-tables-table.html. Specifically, we are adding the columns: `TABLE_ROWS`, `AVG_ROW_LENGTH`, `DATA_LENGTH`, `INDEX_LENGTH` Link to tracking issue: #32693 **Testing:** Added testing data, tested locally against MySQL 8. **Documentation:** By default, the metric is turned off. When enabled, make sure that the MySQL user has access to the tables being monitored.
1 parent cd916df commit 7ddb350

File tree

14 files changed

+579
-0
lines changed

14 files changed

+579
-0
lines changed

.chloggen/feat_table_stats.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: new_component
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: receiver/mysql
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Adds INFORMATION_SCHEMA TABLES metrics"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [32693]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext: |
19+
This adds table size metrics using the INFORMATION_SCHEMA TABLES table: https://dev.mysql.com/doc/refman/8.3/en/information-schema-tables-table.html.
20+
Specifically, we are adding the columns: `TABLE_ROWS`, `AVG_ROW_LENGTH`, `DATA_LENGTH`, `INDEX_LENGTH`.
21+
22+
23+
# If your change doesn't affect end users or the exported elements of any package,
24+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
25+
# Optional: The change log or logs in which this entry should be included.
26+
# e.g. '[user]' or '[user, api]'
27+
# Include 'user' if the change is relevant to end users.
28+
# Include 'api' if there is a change to a library API.
29+
# Default: '[user]'
30+
change_logs: []

receiver/mysqlreceiver/client.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type client interface {
1919
getVersion() (string, error)
2020
getGlobalStats() (map[string]string, error)
2121
getInnodbStats() (map[string]string, error)
22+
getTableStats() ([]TableStats, error)
2223
getTableIoWaitsStats() ([]TableIoWaitsStats, error)
2324
getIndexIoWaitsStats() ([]IndexIoWaitsStats, error)
2425
getStatementEventsStats() ([]StatementEventStats, error)
@@ -57,6 +58,15 @@ type IndexIoWaitsStats struct {
5758
index string
5859
}
5960

61+
type TableStats struct {
62+
schema string
63+
name string
64+
rows int64
65+
averageRowLength int64
66+
dataLength int64
67+
indexLength int64
68+
}
69+
6070
type StatementEventStats struct {
6171
schema string
6272
digest string
@@ -231,6 +241,33 @@ func (c *mySQLClient) getInnodbStats() (map[string]string, error) {
231241
return query(*c, q)
232242
}
233243

244+
// getTableStats queries the db for information_schema table size metrics.
245+
func (c *mySQLClient) getTableStats() ([]TableStats, error) {
246+
query := "SELECT TABLE_SCHEMA, TABLE_NAME, TABLE_ROWS, " +
247+
"AVG_ROW_LENGTH, DATA_LENGTH, INDEX_LENGTH " +
248+
"FROM information_schema.TABLES " +
249+
"WHERE TABLE_SCHEMA NOT in ('information_schema', 'sys') " +
250+
"ORDER BY TABLE_LENGTH DESC;"
251+
rows, err := c.client.Query(query)
252+
if err != nil {
253+
return nil, err
254+
}
255+
defer rows.Close()
256+
var stats []TableStats
257+
for rows.Next() {
258+
var s TableStats
259+
err := rows.Scan(&s.schema, &s.name,
260+
&s.rows, &s.averageRowLength,
261+
&s.dataLength, &s.indexLength)
262+
if err != nil {
263+
return nil, err
264+
}
265+
stats = append(stats, s)
266+
}
267+
268+
return stats, nil
269+
}
270+
234271
// getTableIoWaitsStats queries the db for table_io_waits metrics.
235272
func (c *mySQLClient) getTableIoWaitsStats() ([]TableIoWaitsStats, error) {
236273
query := "SELECT OBJECT_SCHEMA, OBJECT_NAME, " +

receiver/mysqlreceiver/documentation.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,21 @@ The total wait time of the summarized timed events.
519519
| digest | Digest. | Any Str |
520520
| digest_text | Text before digestion. | Any Str |
521521
522+
### mysql.table.average_row_length
523+
524+
The average row length in bytes for a given table.
525+
526+
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
527+
| ---- | ----------- | ---------- | ----------------------- | --------- |
528+
| By | Sum | Int | Cumulative | false |
529+
530+
#### Attributes
531+
532+
| Name | Description | Values |
533+
| ---- | ----------- | ------ |
534+
| table | Table name for event or process. | Any Str |
535+
| schema | The schema of the object. | Any Str |
536+
522537
### mysql.table.lock_wait.read.count
523538
524539
The total table lock wait read events.
@@ -583,6 +598,37 @@ The total table lock wait write events times.
583598
| table | Table name for event or process. | Any Str |
584599
| kind | Write operation types. | Str: ``allow_write``, ``concurrent_insert``, ``low_priority``, ``normal``, ``external`` |
585600
601+
### mysql.table.rows
602+
603+
The number of rows for a given table.
604+
605+
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
606+
| ---- | ----------- | ---------- | ----------------------- | --------- |
607+
| 1 | Sum | Int | Cumulative | false |
608+
609+
#### Attributes
610+
611+
| Name | Description | Values |
612+
| ---- | ----------- | ------ |
613+
| table | Table name for event or process. | Any Str |
614+
| schema | The schema of the object. | Any Str |
615+
616+
### mysql.table.size
617+
618+
The table size in bytes for a given table.
619+
620+
| Unit | Metric Type | Value Type | Aggregation Temporality | Monotonic |
621+
| ---- | ----------- | ---------- | ----------------------- | --------- |
622+
| By | Sum | Int | Cumulative | false |
623+
624+
#### Attributes
625+
626+
| Name | Description | Values |
627+
| ---- | ----------- | ------ |
628+
| table | Table name for event or process. | Any Str |
629+
| schema | The schema of the object. | Any Str |
630+
| kind | The table size types. | Str: ``data``, ``index`` |
631+
586632
### mysql.table_open_cache
587633
588634
The number of hits, misses or overflows for open tables cache lookups.

receiver/mysqlreceiver/internal/metadata/generated_config.go

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

receiver/mysqlreceiver/internal/metadata/generated_config_test.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)