Skip to content

Commit

Permalink
Fix the duration validator so it matches what prom actually uses (#272)
Browse files Browse the repository at this point in the history
* Fix the duration validator so it matches what prom actually uses

* Add comments referencing the regexp definition and validation
  • Loading branch information
rbarry82 authored May 20, 2022
1 parent 155f990 commit a3d80a0
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,15 @@ def _is_valid_timespec(self, timeval) -> bool:
Returns:
True if time specification is valid and False otherwise.
"""
if not (matched := re.match(r"[1-9][0-9]*[ymwdhs]", timeval)):
# Prometheus checks here:
# https://github.com/prometheus/common/blob/627089d3a7af73be778847aa577192b937b8d89a/model/time.go#L186
# Which is where this regex is sourced from. The validation is done
# when parsing flags as part of binary invocation here:
# https://github.com/prometheus/prometheus/blob/c40e269c3e514953299e9ba1f6265e067ab43e64/cmd/prometheus/main.go#L302
timespec_re = re.compile(
r"^((([0-9]+)y)?(([0-9]+)w)?(([0-9]+)d)?(([0-9]+)h)?(([0-9]+)m)?(([0-9]+)s)?(([0-9]+)ms)?|0)$"
)
if not (matched := timespec_re.search(timeval)):
self.unit.status = BlockedStatus(f"Invalid time spec : {timeval}")

return bool(matched)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def test_invalid_metrics_retention_times_can_not_be_set(self):
self.assertEqual(cli_arg(plan, "--storage.tsdb.retention.time"), None)

# invalid time value
retention_time = "0d"
retention_time = "5m1y2d"
retention_time_config["metrics_retention_time"] = retention_time

self.harness.update_config(retention_time_config)
Expand Down

0 comments on commit a3d80a0

Please sign in to comment.