Skip to content
Snippets Groups Projects
Commit 00fd91e1 authored by Piotr Maślanka's avatar Piotr Maślanka
Browse files

restricted naming metrics

parent 61230a41
No related branches found
No related tags found
No related merge requests found
......@@ -28,7 +28,8 @@ instrument that are set to at least RUNTIME. If a user wants to
dig deeper, it can switch the instrument to
DEBUG, which will cause more data to be registered. If a metric
is in state INHERIT, it will inherit the metric level from it's
parent, traversing the tree if required.
parent, traversing the tree if required. The tree node separator is a dot in the
metric's name, eg. `satella.metrics.my_metric`.
INHERIT is the default state for all other metrics than root,
for root the default is RUNTIME. Root metric cannot be set to INHERIT,
......@@ -50,6 +51,14 @@ You obtain metrics using ``getMetric()`` as follows:
``metric = getMetric(__name__+'.StringMetric', 'string', MetricLevel.RUNTIME, **kwargs)``
Please note that metric name cannot contain following characters:
* -
* {
* }
* '
* "
**internal** is for those cases where the application is the consumer of
the metrics, and you don't want them exposed to outside.
Take care to examine this field of ``MetricData`` if you
......
......@@ -27,6 +27,9 @@ def adjust_metric_level_for_root(metric_level: tp.Optional[MetricLevel],
return metric_level_to_set_for_children
FORBIDDEN_CHARACTERS = '-"{}\''
# noinspection PyPep8Naming
def getMetric(metric_name: str = '',
metric_type: str = 'base',
......@@ -39,7 +42,12 @@ def getMetric(metric_name: str = '',
:param metric_type: metric type
:param metric_level: a metric level to set this metric to.
:raise MetricAlreadyExists: a metric having this name already exists, but with a different type
:raise ValueError: metric name contains a forbidden character
"""
for forbidden_char in FORBIDDEN_CHARACTERS:
if forbidden_char in metric_name:
raise ValueError('Metric name contains a forbidden character')
metric_level_to_set_for_children = metric_level or MetricLevel.INHERIT
name = metric_name.split('.')
with metrics_lock:
......
......@@ -18,6 +18,9 @@ def choose(postfix: str, mdc: MetricDataCollection, labels=None) -> MetricData:
class TestMetric(unittest.TestCase):
def test_forbidden_characters(self):
self.assertRaises(ValueError, lambda: getMetric('forbidden-characters', 'int'))
def test_labeled_metric_with_callable(self):
m1 = getMetric('lmc1', 'callable')
lm = LabeledMetric(m1, key='value')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment