Skip to content

Commit 65a56d7

Browse files
authored
Don't emit metrics for tables not in the config file (#78)
This fixes #67 , which causes unnecessary alerts (and silences) for tables we're not trying to keep track of.
1 parent 965110f commit 65a56d7

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

partitionmanager/cli.py

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ def from_yaml_file(self, file):
119119
self.dbcmd = partitionmanager.sql.SubprocessDatabaseCommand(
120120
data["mariadb"]
121121
)
122-
if not self.tables: # Only load tables froml YAML if not supplied via args
122+
if not self.tables: # Only load tables from YAML if not supplied via args
123123
for key in data["tables"]:
124124
tab = partitionmanager.types.Table(key)
125125
tabledata = data["tables"][key]
@@ -179,14 +179,6 @@ def _extract_single_column(row):
179179
return row[columns[0]]
180180

181181

182-
def list_tables(conf):
183-
"""List all tables for the current database."""
184-
rows = conf.dbcmd.run("SHOW TABLES;")
185-
table_names = (_extract_single_column(row) for row in rows)
186-
table_objects = (partitionmanager.types.Table(name) for name in table_names)
187-
return list(table_objects)
188-
189-
190182
def partition_cmd(args):
191183
"""Runs do_partition on the config that results from the CLI arguments.
192184
@@ -379,13 +371,16 @@ def do_partition(conf):
379371
return all_results
380372

381373

382-
def do_stats(conf, metrics=partitionmanager.stats.PrometheusMetrics()):
374+
def do_stats(conf, metrics=None):
383375
"""Populates a metrics object from the tables in the configuration."""
384376

385377
log = logging.getLogger("do_stats")
386378

379+
if not metrics:
380+
metrics = partitionmanager.stats.PrometheusMetrics()
381+
387382
all_results = {}
388-
for table in list_tables(conf):
383+
for table in conf.tables:
389384
table_problems = pm_tap.get_table_compatibility_problems(conf.dbcmd, table)
390385
if table_problems:
391386
log.debug(f"Cannot gather statistics for {table}: {table_problems}")

partitionmanager/cli_test.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,13 +279,17 @@ def assert_stats_results(self, results):
279279
results["partitioned_yesterday"]["max_partition_delta"].days, 2
280280
)
281281

282-
def assert_stats_prometheus_outfile(self, prom_file):
282+
def parse_prometheus_outfile(self, prom_file):
283283
lines = prom_file.split("\n")
284284
metrics = {}
285285
for line in lines:
286286
if not line.startswith("#") and len(line) > 0:
287287
key, value = line.split(" ")
288288
metrics[key] = value
289+
return metrics
290+
291+
def assert_stats_prometheus_outfile(self, prom_file):
292+
metrics = self.parse_prometheus_outfile(prom_file)
289293

290294
for table in ["partitioned_last_week", "partitioned_yesterday", "other"]:
291295
self.assertIn(f'partition_total{{table="{table}"}}', metrics)
@@ -303,7 +307,7 @@ def assert_stats_prometheus_outfile(self, prom_file):
303307
def test_stats_cli_flag(self):
304308
args = PARSER.parse_args(["--mariadb", str(fake_exec), "stats"])
305309
results = stats_cmd(args)
306-
self.assert_stats_results(results)
310+
assert results == {}
307311

308312
def test_stats_yaml(self):
309313
with tempfile.NamedTemporaryFile(
@@ -314,7 +318,9 @@ def test_stats_yaml(self):
314318
mariadb: {str(fake_exec)}
315319
prometheus_stats: {stats_outfile.name}
316320
tables:
317-
unused:
321+
other:
322+
partitioned_last_week:
323+
partitioned_yesterday:
318324
"""
319325
insert_into_file(tmpfile, yaml)
320326
args = PARSER.parse_args(["--config", tmpfile.name, "stats"])
@@ -324,6 +330,36 @@ def test_stats_yaml(self):
324330
self.assert_stats_results(results)
325331
self.assert_stats_prometheus_outfile(stats_outfile.read())
326332

333+
def test_stats_yaml_ignore_unconfigured_tables(self):
334+
with tempfile.NamedTemporaryFile(
335+
mode="w+", encoding="UTF-8"
336+
) as stats_outfile, tempfile.NamedTemporaryFile() as tmpfile:
337+
yaml = f"""
338+
partitionmanager:
339+
mariadb: {str(fake_exec)}
340+
prometheus_stats: {stats_outfile.name}
341+
tables:
342+
other:
343+
"""
344+
insert_into_file(tmpfile, yaml)
345+
args = PARSER.parse_args(["--config", tmpfile.name, "stats"])
346+
347+
results = stats_cmd(args)
348+
349+
assert list(results.keys()) == ["other"]
350+
351+
out_data = stats_outfile.read()
352+
353+
metrics = self.parse_prometheus_outfile(out_data)
354+
assert list(metrics.keys()) == [
355+
'partition_total{table="other"}',
356+
'partition_time_remaining_until_partition_overrun{table="other"}',
357+
'partition_age_of_retained_partitions{table="other"}',
358+
'partition_mean_delta_seconds{table="other"}',
359+
'partition_max_delta_seconds{table="other"}',
360+
"partition_last_run_timestamp{}",
361+
]
362+
327363

328364
class TestConfig(unittest.TestCase):
329365
def test_cli_tables_override_yaml(self):

wheel2deb.yml

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)