8000 Add support for parsing timestamps. (#483) · rwky/client_python@f11b6d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit f11b6d9

Browse files
asherfbrian-brazil
authored andcommitted
Add support for parsing timestamps. (prometheus#483)
* Add support for parsing timestamps. Signed-off-by: Asher Foa <asher@asherfoa.com>
1 parent db04b10 commit f11b6d9

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

prometheus_client/parser.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -103,15 +103,17 @@ def _parse_labels(labels_string):
103103

104104

105105
# If we have multiple values only consider the first
106-
def _parse_value(s):
106+
def _parse_value_and_timestamp(s):
107107
s = s.lstrip()
108108
separator = " "
109109
if separator not in s:
110110
separator = "\t"
111-
i = s.find(separator)
112-
if i == -1:
113-
return s
114-
return s[:i]
111+
values = [value.strip() for value in s.split(separator) if value.strip()]
112+
if not values:
113+
return float(s), None
114+
value = float(values[0])
115+
timestamp = (float(values[-1])/1000) if len(values) > 1 else None
116+
return value, timestamp
115117

116118

117119
def _parse_sample(text):
@@ -123,8 +125,8 @@ def _parse_sample(text):
123125
# We ignore the starting curly brace
124126
label = text[label_start + 1:label_end]
125127
# The value is after the label end (ignoring curly brace and space)
126-
value = float(_parse_value(text[label_end + 2:]))
127-
return Sample(name, _parse_labels(label), value)
128+
value, timestamp = _parse_value_and_timestamp(text[label_end + 2:])
129+
return Sample(name, _parse_labels(label), value, timestamp)
128130

129131
# We don't have labels
130132
except ValueError:
@@ -135,8 +137,8 @@ def _parse_sample(text):
135137
name_end = text.index(separator)
136138
name = text[:name_end]
137139
# The value is after the name
138-
value = float(_parse_value(text[name_end:]))
139-
return Sample(name, {}, value)
140+
value, timestamp = _parse_value_and_timestamp(text[name_end:])
141+
return Sample(name, {}, value, timestamp)
140142

141143

142144
def text_fd_to_metric_families(fd):

tests/test_parser.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,17 +273,20 @@ def test_escaping(self):
273273
metric_family.add_metric(["b\\a\\z"], 2)
274274
self.assertEqualMetrics([metric_family], list(families))
275275

276-
def test_timestamps_discarded(self):
276+
def test_timestamps(self):
277277
families = text_string_to_metric_families("""# TYPE a counter
278278
# HELP a help
279279
a{foo="bar"} 1\t000
280280
# TYPE b counter
281281
# HELP b help
282282
b 2 1234567890
283+
b 88 1234566000
283284
""")
284285
a = CounterMetricFamily("a", "help", labels=["foo"])
285-
a.add_metric(["bar"], 1)
286-
b = CounterMetricFamily("b", "help", value=2)
286+
a.add_metric(["bar"], 1, timestamp=0)
287+
b = CounterMetricFamily("b", "help")
288+
b.add_metric([], 2, timestamp=1234567.89)
289+
b.add_metric([], 88, timestamp=1234566)
287290
self.assertEqualMetrics([a, b], list(families))
288291

289292
@unittest.skipIf(sys.version_info < (2, 7), "Test requires Python 2.7+.")

0 commit comments

Comments
 (0)
0