Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Escape label values in prometheus metrics #3175

Merged
merged 2 commits into from
May 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions synapse/metrics/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

from itertools import chain
import logging
import re

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -56,8 +57,7 @@ def is_scalar(self):
return not len(self.labels)

def _render_labelvalue(self, value):
# TODO: escape backslashes, quotes and newlines
return '"%s"' % (value)
return '"%s"' % (_escape_label_value(value),)

def _render_key(self, values):
if self.is_scalar():
Expand Down Expand Up @@ -299,3 +299,21 @@ def render(self):
"process_psutil_rss:total %d" % sum_rss,
"process_psutil_rss:count %d" % len_rss,
]


def _escape_character(c):
"""Replaces a single character with its escape sequence.
"""
if c == "\\":
return "\\\\"
elif c == "\"":
return "\\\""
elif c == "\n":
return "\\n"
return c


def _escape_label_value(value):
"""Takes a label value and escapes quotes, newlines and backslashes
"""
return re.sub(r"([\n\"\\])", lambda m: _escape_character(m.group(1)), value)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be inclined to make _escape_character take the match object, rather than create a new lambda for each label.

21 changes: 20 additions & 1 deletion tests/metrics/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
from tests import unittest

from synapse.metrics.metric import (
CounterMetric, CallbackMetric, DistributionMetric, CacheMetric
CounterMetric, CallbackMetric, DistributionMetric, CacheMetric,
_escape_label_value,
)


Expand Down Expand Up @@ -171,3 +172,21 @@ def test_cache(self):
'cache:size{name="cache_name"} 1',
'cache:evicted_size{name="cache_name"} 2',
])


class LabelValueEscapeTestCase(unittest.TestCase):
def test_simple(self):
string = "safjhsdlifhyskljfksdfh"
self.assertEqual(string, _escape_label_value(string))

def test_escape(self):
self.assertEqual(
"abc\\\"def\\nghi\\\\",
_escape_label_value("abc\"def\nghi\\"),
)

def test_sequence_of_escapes(self):
self.assertEqual(
"abc\\\"def\\nghi\\\\\\n",
_escape_label_value("abc\"def\nghi\\\n"),
)