]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
monitoring/grafana: doctest util regex
authorPere Diaz Bou <pdiazbou@redhat.com>
Tue, 14 Dec 2021 08:25:49 +0000 (09:25 +0100)
committerPere Diaz Bou <pdiazbou@redhat.com>
Tue, 26 Apr 2022 07:24:07 +0000 (09:24 +0200)
Signed-off-by: Pere Diaz Bou <pdiazbou@redhat.com>
monitoring/ceph-mixin/tests_dashboards/__init__.py
monitoring/ceph-mixin/tests_dashboards/util.py

index 204a5f0d721b2c4b1d6b46c9372331bed675e2dd..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,7 +173,7 @@ class PromqlTest:
 
         for variable, value in self.variables.items():
             expr = self.promql_expr_test.expr
-            new_expr = re.sub(r'\${0}'.format(variable), 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 b5872deafc99013e26d69195db8e74bd0fd68ebf..1d8b57325eb05c42b05cda7c3b3fefeb1ac998f4 100644 (file)
@@ -1,4 +1,5 @@
 import json
+import re
 from pathlib import Path
 from typing import Any, Dict, Tuple, Union
 
@@ -67,3 +68,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