Skip to content

Commit 9ace714

Browse files
committed
sanitize names when using them as tag value
related: graphite-project/graphite-web#2458
1 parent a87b9c2 commit 9ace714

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/carbon/tests/test_util.py

+28
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from carbon.util import parseDestinations
66
from carbon.util import enableTcpKeepAlive
7+
from carbon.util import TaggedSeries
78

89

910
class UtilTest(TestCase):
@@ -21,6 +22,33 @@ def setTcpKeepAlive(self, value):
2122
enableTcpKeepAlive(_Transport(), True, None)
2223
self.assertEquals(s.getsockopt(socket.SOL_TCP, socket.SO_KEEPALIVE), 1)
2324

25+
def test_sanitizing_name_as_tag_value(self):
26+
test_cases = [
27+
{
28+
'original': "my~.test.abc",
29+
'expected': "my.test.abc",
30+
}, {
31+
'original': "a.b.c",
32+
'expected': "a.b.c",
33+
}, {
34+
'original': "~~a~~.~~~b~~~.~~~c~~~",
35+
'expected': "a.b.c",
36+
}, {
37+
'original': "a.b.c~",
38+
'expected': "a.b.c",
39+
}, {
40+
'original': "~a.b.c",
41+
'expected': "a.b.c",
42+
}, {
43+
'original': "~a~",
44+
'expected': "a",
45+
},
46+
]
47+
48+
for test_case in test_cases:
49+
result = TaggedSeries.sanitize_name_as_tag_value(test_case['original'])
50+
self.assertEquals(result, test_case['expected'])
51+
2452

2553
# Destinations have the form:
2654
# <host> ::= <string without colons> | "[" <string> "]"

lib/carbon/util.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ def parse_openmetrics(cls, path):
365365
tags[m.group(1)] = m.group(2).replace(r'\"', '"').replace(r'\\', '\\')
366366
rawtags = rawtags[len(m.group(0)):]
367367

368-
tags['name'] = metric
368+
tags['name'] = cls.sanitize_name_as_tag_value(metric)
369369
return cls(metric, tags)
370370

371371
@classmethod
@@ -386,9 +386,14 @@ def parse_carbon(cls, path):
386386

387387
tags[tag[0]] = tag[1]
388388

389-
tags['name'] = metric
389+
tags['name'] = cls.sanitize_name_as_tag_value(metric)
390390
return cls(metric, tags)
391391

392+
@staticmethod
393+
def sanitize_name_as_tag_value(name):
394+
"""take a metric name and sanitize it so it is guaranteed to be a valid tag value"""
395+
return name.replace('~', '')
396+
392397
@staticmethod
393398
def format(tags):
394399
return tags.get('name', '') + ''.join(sorted([

0 commit comments

Comments
 (0)