Skip to content

Commit

Permalink
add tagged series decode method
Browse files Browse the repository at this point in the history
  • Loading branch information
DanCech committed Nov 28, 2017
1 parent fc01b08 commit 0209a3d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
11 changes: 11 additions & 0 deletions webapp/graphite/tags/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ def encode(metric, sep='.'):
# 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
8 changes: 7 additions & 1 deletion webapp/tests/test_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ def test_taggedseries(self):
# test encoding
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')
self.assertIsInstance(parsed, TaggedSeries)
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 0209a3d

Please sign in to comment.