Skip to content

Commit d32c5c2

Browse files
Add configurable limit to index metric (#20012)
* testing a branch * limit cardinality of index metrics * add changelog * delete zombie file and clarify config description
1 parent 9ddd493 commit d32c5c2

File tree

5 files changed

+63
-12
lines changed

5 files changed

+63
-12
lines changed

mysql/assets/configuration/spec.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,6 +567,13 @@ files:
567567
value:
568568
type: number
569569
example: 300
570+
- name: limit
571+
description: |
572+
Set the maximum number of unique (by combination of database, table, and index name)
573+
indexes to collect. Defaults to 1000.
574+
value:
575+
type: integer
576+
example: 1000
570577
- name: aws
571578
description: |
572579
This block defines the configuration for AWS RDS and Aurora instances.

mysql/changelog.d/20012.added

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
added configurable limit to index metric collection

mysql/datadog_checks/mysql/config_models/instance.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class IndexMetrics(BaseModel):
7474
)
7575
collection_interval: Optional[float] = None
7676
enabled: Optional[bool] = None
77+
limit: Optional[int] = None
7778

7879

7980
class MetricPatterns(BaseModel):

mysql/datadog_checks/mysql/data/conf.yaml.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,12 @@ instances:
537537
#
538538
# enabled: true
539539

540+
## @param limit - integer - optional - default: 1000
541+
## Set the maximum number of unique (by combination of database, table, and index name)
542+
## indexes to collect. Defaults to 1000.
543+
#
544+
# limit: 1000
545+
540546
## This block defines the configuration for AWS RDS and Aurora instances.
541547
##
542548
## Complete this section if you have installed the Datadog AWS Integration

mysql/datadog_checks/mysql/index_metrics.py

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,21 @@
99
'query': """
1010
SELECT
1111
database_name,
12-
table_name,
12+
CASE
13+
WHEN table_name LIKE '%#p#%' THEN SUBSTRING_INDEX(table_name, '#p#', 1)
14+
ELSE table_name
15+
END AS base_table_name,
1316
index_name,
14-
stat_value * @@innodb_page_size AS index_size_bytes
17+
SUM(stat_value * @@innodb_page_size) AS index_size_bytes
1518
FROM
1619
mysql.innodb_index_stats
1720
WHERE
1821
stat_name = 'size'
22+
GROUP BY
23+
database_name, base_table_name, index_name
24+
ORDER BY
25+
index_size_bytes DESC
26+
LIMIT {INDEX_LIMIT}
1927
""".strip(),
2028
'columns': [
2129
{'name': 'db', 'type': 'tag'},
@@ -28,16 +36,35 @@
2836
'name': 'performance_schema.table_io_waits_summary_by_index_usage',
2937
'query': """
3038
SELECT
31-
object_schema,
32-
object_name,
33-
index_name,
34-
count_read,
35-
count_update,
36-
count_delete
37-
FROM
38-
performance_schema.table_io_waits_summary_by_index_usage
39-
WHERE index_name IS NOT NULL
40-
AND object_schema NOT IN ('mysql', 'performance_schema')
39+
t.object_schema AS schema_name,
40+
t.object_name AS table_name,
41+
t.index_name,
42+
t.count_read,
43+
t.count_update,
44+
t.count_delete
45+
FROM performance_schema.table_io_waits_summary_by_index_usage t
46+
JOIN (
47+
SELECT
48+
database_name,
49+
CASE
50+
WHEN table_name LIKE '%#p#%' THEN SUBSTRING_INDEX(table_name, '#p#', 1)
51+
ELSE table_name
52+
END AS base_table_name,
53+
index_name,
54+
SUM(stat_value * @@innodb_page_size) AS index_size_bytes
55+
FROM
56+
mysql.innodb_index_stats
57+
WHERE
58+
stat_name = 'size'
59+
GROUP BY
60+
database_name, base_table_name, index_name
61+
ORDER BY
62+
index_size_bytes DESC
63+
LIMIT {INDEX_LIMIT}
64+
) i ON t.object_schema = i.database_name
65+
AND t.object_name = i.base_table_name
66+
AND t.index_name = i.index_name
67+
AND t.index_name IS NOT NULL
4168
""".strip(),
4269
'columns': [
4370
{'name': 'db', 'type': 'tag'},
@@ -50,6 +77,7 @@
5077
}
5178

5279
DEFAULT_INDEX_METRIC_COLLECTION_INTERVAL = 300 # 5 minutes
80+
DEFAULT_INDEX_LIMIT = 1000 # Default number of top indexes to collect
5381

5482

5583
class MySqlIndexMetrics:
@@ -64,12 +92,20 @@ def include_index_metrics(self) -> bool:
6492
def collection_interval(self) -> int:
6593
return int(self._config.index_config.get('collection_interval', DEFAULT_INDEX_METRIC_COLLECTION_INTERVAL))
6694

95+
@property
96+
def index_limit(self) -> int:
97+
return int(self._config.index_config.get('limit', DEFAULT_INDEX_LIMIT))
98+
6799
@property
68100
def queries(self):
69101
# make a copy of the query to avoid modifying the original
70102
# in case different instances have different collection intervals
71103
usage_query = QUERY_INDEX_USAGE.copy()
72104
size_query = QUERY_INDEX_SIZE.copy()
105+
106+
# Update the index limit in the queries
107+
size_query['query'] = size_query['query'].format(INDEX_LIMIT=self.index_limit)
108+
usage_query['query'] = usage_query['query'].format(INDEX_LIMIT=self.index_limit)
73109
usage_query['collection_interval'] = self.collection_interval
74110
size_query['collection_interval'] = self.collection_interval
75111
return [size_query, usage_query]

0 commit comments

Comments
 (0)