]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
monitoring/grafana: doctest util regex 44190/head
authorPere Diaz Bou <pdiazbou@redhat.com>
Tue, 14 Dec 2021 08:25:49 +0000 (09:25 +0100)
committerPere Diaz Bou <pdiazbou@redhat.com>
Wed, 15 Dec 2021 08:36:08 +0000 (09:36 +0100)
Signed-off-by: Pere Diaz Bou <pdiazbou@redhat.com>
monitoring/grafana/dashboards/tests/__init__.py
monitoring/grafana/dashboards/tests/util.py
monitoring/grafana/dashboards/tox.ini

index d758afabf526a129c0baa15862d4dbb78f3331a3..45147e5c32490f6d10cdfe5674dc190f5399ebdd 100644 (file)
@@ -7,6 +7,8 @@ from typing import Any, List
 
 import yaml
 
+from .util import replace_grafana_expr_variables
+
 
 @dataclass
 class InputSeries:
@@ -171,8 +173,7 @@ class PromqlTest:
 
         for variable, value in self.variables.items():
             expr = self.promql_expr_test.expr
-            regex = fr'\${variable}(?=\W)'
-            new_expr = re.sub(regex, fr'{str(value)}', expr)
+            new_expr = replace_grafana_expr_variables(expr, variable, value)
             self.set_expression(new_expr)
 
         test_as_dict = asdict(self.test_file)
index 4f09e9edd3b0fd23d3ee9132e28cc77525182425..7b6391a34ab67936b0d54c37a5db0030e64ab538 100644 (file)
@@ -1,4 +1,5 @@
 import json
+import re
 from pathlib import Path
 from typing import Any, Dict, Tuple, Union
 
@@ -66,3 +67,25 @@ def add_dashboard_variables(data: Dict[str, Any], dashboard_data: Dict[str, Any]
     for variable in dashboard_data['templating']['list']:
         if 'name' in variable:
             data['variables'][variable['name']] = 'UNSET VARIABLE'
+
+
+def replace_grafana_expr_variables(expr: str, variable: str, value: Any) -> str:
+    """ Replace grafana variables in expression with a value
+
+    It should match the whole word, 'osd' musn't match with the 'osd' prefix in 'osd_hosts'
+    >>> replace_grafana_expr_variables('metric{name~="$osd_hosts|$other|$osd"}', \
+        'osd', 'replacement')
+    'metric{name~="$osd_hosts|$other|replacement"}'
+
+    >>> replace_grafana_expr_variables('metric{name~="$osd_hosts|$other|$osd"}', \
+        'other', 'replacement')
+    'metric{name~="$osd_hosts|replacement|$osd"}'
+
+    It replaces words with dollar prefix
+    >>> replace_grafana_expr_variables('metric{name~="no_dollar|$other|$osd"}', \
+        'no_dollar', 'replacement')
+    'metric{name~="no_dollar|$other|$osd"}'
+    """
+    regex = fr'\${variable}(?=\W)'
+    new_expr = re.sub(regex, fr'{str(value)}', expr)
+    return new_expr
index 382952c5b1beeb2c84661783bbea5d563811dd7c..c489a7897113f3642557c58b154b640fcd67d1d3 100644 (file)
@@ -41,4 +41,5 @@ deps =
 depends = grafonnet-check
 setenv =
 commands =
+    python -m doctest tests/util.py
     behave tests/features