Skip to content

Commit

Permalink
Merge pull request #2129 from DanCech/DOT
Browse files Browse the repository at this point in the history
 use _DOT_ to replace literal . in tagged series filenames
  • Loading branch information
DanCech authored Nov 28, 2017
2 parents 25e77d3 + 0209a3d commit d4eb9cc
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
13 changes: 12 additions & 1 deletion webapp/graphite/tags/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,22 @@ def encode(metric, sep='.'):
"""
if ';' in metric:
metric_hash = sha256(metric.encode('utf8')).hexdigest()
return sep.join(['_tagged', metric_hash[0:3], metric_hash[3:6], metric.replace('.', '-')])
return sep.join(['_tagged', metric_hash[0:3], metric_hash[3:6], metric.replace('.', '_DOT_')])

# metric isn't tagged, just replace dots with the separator and trim any leading separator
return metric.replace('.', sep).lstrip(sep)

@staticmethod
def decode(path, sep='.'):
"""
Helper function to decode tagged series from storage in whisper etc
"""
if path.startswith('_tagged'):
return path.split(sep, 3)[-1].replace('_DOT_', '.')

# metric isn't tagged, just replace the separator with dots
return path.replace(sep, '.')

def __init__(self, metric, tags, series_id=None):
self.metric = metric
self.tags = tags
Expand Down
10 changes: 8 additions & 2 deletions webapp/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ def test_taggedseries(self):
self.assertEqual(parsed.path, 'test.a;blah=blah;hello=tiger')

# test encoding
self.assertEqual(TaggedSeries.encode(parsed.path), '_tagged.2b0.2af.test-a;blah=blah;hello=tiger')
self.assertEqual(TaggedSeries.encode(parsed.path), '_tagged.2b0.2af.test_DOT_a;blah=blah;hello=tiger')

# test decoding
self.assertEqual(TaggedSeries.decode('_tagged.2b0.2af.test_DOT_a;blah=blah;hello=tiger'), parsed.path)

# test path without tags
parsed = TaggedSeries.parse('test.a')
Expand All @@ -40,7 +43,10 @@ def test_taggedseries(self):
self.assertEqual(parsed.path, 'test.a')

# test encoding
self.assertEqual(TaggedSeries.encode(parsed.path), 'test.a')
self.assertEqual(TaggedSeries.encode('test.a', sep='/'), 'test/a')

# test encoding
self.assertEqual(TaggedSeries.decode('test/a', sep='/'), 'test.a')

# test parsing openmetrics
parsed = TaggedSeries.parse(r'test.a{hello="tiger",blah="bla\"h"}')
Expand Down

0 comments on commit d4eb9cc

Please sign in to comment.