Skip to content

Commit 15da096

Browse files
committed
implement updated rules
1 parent b34ce4b commit 15da096

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

lib/carbon/tests/test_util.py

+19-6
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,41 @@ def test_sanitizing_name_as_tag_value(self):
2626
test_cases = [
2727
{
2828
'original': "my~.test.abc",
29-
'expected': "my.test.abc",
29+
'expected': "my~.test.abc",
3030
}, {
3131
'original': "a.b.c",
3232
'expected': "a.b.c",
3333
}, {
3434
'original': "~~a~~.~~~b~~~.~~~c~~~",
35-
'expected': "a.b.c",
35+
'expected': "a~~.~~~b~~~.~~~c~~~",
3636
}, {
3737
'original': "a.b.c~",
38-
'expected': "a.b.c",
38+
'expected': "a.b.c~",
3939
}, {
4040
'original': "~a.b.c",
4141
'expected': "a.b.c",
4242
}, {
4343
'original': "~a~",
44-
'expected': "a",
44+
'expected': "a~",
45+
}, {
46+
'original': "~~~",
47+
'raises': True,
48+
}, {
49+
'original': "~",
50+
'raises': True,
4551
},
4652
]
4753

4854
for test_case in test_cases:
49-
result = TaggedSeries.sanitize_name_as_tag_value(test_case['original'])
50-
self.assertEquals(result, test_case['expected'])
55+
if test_case.get('raises', False):
56+
self.assertRaises(
57+
Exception,
58+
TaggedSeries.sanitize_name_as_tag_value,
59+
test_case['original'],
60+
)
61+
else:
62+
result = TaggedSeries.sanitize_name_as_tag_value(test_case['original'])
63+
self.assertEquals(result, test_case['expected'])
5164

5265

5366
# Destinations have the form:

lib/carbon/util.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,12 @@ def parse_carbon(cls, path):
392392
@staticmethod
393393
def sanitize_name_as_tag_value(name):
394394
"""take a metric name and sanitize it so it is guaranteed to be a valid tag value"""
395-
return name.replace('~', '')
395+
sanitized = name.lstrip('~')
396+
397+
if len(sanitized) == 0:
398+
raise Exception('Cannot use metric name %s as tag value, results in emptry string' % (name))
399+
400+
return sanitized
396401

397402
@staticmethod
398403
def format(tags):

0 commit comments

Comments
 (0)