Skip to content

Commit 1d748f0

Browse files
committed
partitions portion of dictionary re-written
1 parent a76dc68 commit 1d748f0

File tree

4 files changed

+228
-201
lines changed

4 files changed

+228
-201
lines changed

mysql/assets/configuration/spec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ files:
385385
hidden: true
386386
description: |
387387
Configure collection of schemas (databases).
388+
Only tables and schemas for which the user has been granted SELECT privileges are collected.
388389
options:
389390
- name: enabled
390391
description: |

mysql/datadog_checks/mysql/databases_data.py

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import json
1010
import time
1111
from contextlib import closing
12+
from collections import defaultdict
1213

1314
import pymysql
1415

@@ -209,6 +210,7 @@ def _collect_databases_data(self, tags):
209210
- index_type (str): The index method used.
210211
- columns (list): A list of column dictionaries
211212
- column (dict): A dictionary representing a column.
213+
- name (str): The name of the column.
212214
- sub_part (int): The number of indexed characters if column is partially indexed.
213215
- packed (str): How the index is packed.
214216
- nullable (bool): Whether the column is nullable.
@@ -340,6 +342,8 @@ def _populate_with_index_data(self, table_name_to_table_index, table_list, table
340342
self._cursor_run(cursor, query=SQL_INDEXES.format(table_names), params=db_name)
341343
rows = cursor.fetchall()
342344
for row in rows:
345+
print("ALLEN!!!")
346+
print("row:", json.dumps(row, indent=4))
343347
table_name = str(row.pop("table_name"))
344348
table_list[table_name_to_table_index[table_name]].setdefault("indexes", [])
345349
if "nullables" in row:
@@ -351,7 +355,6 @@ def _populate_with_index_data(self, table_name_to_table_index, table_list, table
351355
else:
352356
nullables_converted += "false,"
353357
row["nullables"] = nullables_converted[:-1]
354-
# TODO ALLEN: make this booleans rather than strings
355358
table_list[table_name_to_table_index[table_name]]["indexes"].append(row)
356359

357360
@tracked_method(agent_check_getter=agent_check_getter, track_result_length=True)
@@ -367,7 +370,46 @@ def _populate_with_foreign_keys_data(self, table_name_to_table_index, table_list
367370
def _populate_with_partitions_data(self, table_name_to_table_index, table_list, table_names, db_name, cursor):
368371
self._cursor_run(cursor, query=SQL_PARTITION.format(table_names), params=db_name)
369372
rows = cursor.fetchall()
373+
if not rows:
374+
return
375+
table_partitions_dict = defaultdict(lambda: defaultdict(lambda: {
376+
"name": None,
377+
"subpartitions": [],
378+
"partition_ordinal_position": None,
379+
"partition_method": None,
380+
"partition_expression": None,
381+
"partition_description": None,
382+
"table_rows": 0,
383+
"data_length": 0,
384+
}))
385+
370386
for row in rows:
371-
table_name = str(row.pop("table_name"))
387+
table_name = str(row["table_name"])
372388
table_list[table_name_to_table_index[table_name]].setdefault("partitions", [])
373-
table_list[table_name_to_table_index[table_name]]["partitions"].append(row)
389+
partition_name = str(row["name"])
390+
partition_data = table_partitions_dict[table_name][partition_name]
391+
392+
# Update partition-level info
393+
partition_data["name"] = partition_name
394+
partition_data["partition_ordinal_position"] = int(row["partition_ordinal_position"])
395+
partition_data["partition_method"] = str(row["partition_method"])
396+
partition_data["partition_expression"] = str(row["partition_expression"]).strip().lower()
397+
partition_data["partition_description"] = str(row["partition_description"])
398+
partition_data["table_rows"] += int(row["table_rows"])
399+
partition_data["data_length"] += int(row["data_length"])
400+
401+
# Add subpartition info, if exists
402+
if row["subpartition_name"]:
403+
subpartition = {
404+
"name": row["subpartition_name"],
405+
"subpartition_ordinal_position": int(row["subpartition_ordinal_position"]),
406+
"subpartition_method": str(row["subpartition_method"]),
407+
"subpartition_expression": str(row["subpartition_expression"]).strip().lower(),
408+
"table_rows": int(row["table_rows"]),
409+
"data_length": int(row["data_length"]),
410+
}
411+
partition_data["subpartitions"].append(subpartition)
412+
for table_name, partitions_dict in table_partitions_dict.items():
413+
table_list[table_name_to_table_index[table_name]]["partitions"] = list(partitions_dict.values())
414+
415+
print(json.dumps(partition_data, indent=4))

mysql/datadog_checks/mysql/queries.py

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -118,19 +118,19 @@
118118
SQL_INDEXES = """\
119119
SELECT
120120
table_name as `table_name`,
121-
index_schema as `index_schema`,
122121
index_name as `name`,
123122
collation as `collation`,
123+
cardinality as `cardinality`,
124124
index_type as `index_type`,
125-
group_concat(seq_in_index order by seq_in_index asc) as seq_in_index,
126-
group_concat(column_name order by seq_in_index asc) as columns,
127-
group_concat(sub_part order by seq_in_index asc) as sub_parts,
128-
group_concat(packed order by seq_in_index asc) as packed,
129-
group_concat(nullable order by seq_in_index asc) as nullables,
130-
group_concat(non_unique order by seq_in_index asc) as non_uniques
125+
seq_in_index as `seq_in_index`,
126+
column_name as `column_name`,
127+
sub_part as `sub_part`,
128+
packed as `packed`,
129+
nullable as `nullable`,
130+
non_unique as `non_unique`,
131+
IFNULL(expression, NULL) as `expression`
131132
FROM INFORMATION_SCHEMA.STATISTICS
132-
WHERE table_schema = %s AND table_name IN ({})
133-
GROUP BY table_name, index_schema, index_name, collation, index_type;
133+
WHERE table_schema = %s AND table_name IN ({});
134134
"""
135135

136136
SQL_FOREIGN_KEYS = """\
@@ -163,12 +163,7 @@
163163
subpartition_expression as `subpartition_expression`,
164164
partition_description as `partition_description`,
165165
table_rows as `table_rows`,
166-
data_length as `data_length`,
167-
max_data_length as `max_data_length`,
168-
index_length as `index_length`,
169-
data_free as `data_free`,
170-
partition_comment as `partition_comment`,
171-
tablespace_name as `tablespace_name`
166+
data_length as `data_length`
172167
FROM INFORMATION_SCHEMA.PARTITIONS
173168
WHERE
174169
table_schema = %s AND table_name in ({}) AND partition_name IS NOT NULL

0 commit comments

Comments
 (0)