Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for parsing timestamps. #483

Merged
merged 2 commits into from
Oct 29, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 11 additions & 9 deletions prometheus_client/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ def _parse_labels(labels_string):


# If we have multiple values only consider the first
def _parse_value(s):
def _parse_value_and_timestamp(s):
s = s.lstrip()
separator = " "
if separator not in s:
separator = "\t"
i = s.find(separator)
if i == -1:
return s
return s[:i]
values = [value.strip() for value in s.split(separator) if value.strip()]
if not values:
return float(s), None
value = float(values[0])
timestamp = float(values[-1]) if len(values) > 1 else None
return value, timestamp


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

# We don't have labels
except ValueError:
Expand All @@ -135,8 +137,8 @@ def _parse_sample(text):
name_end = text.index(separator)
name = text[:name_end]
# The value is after the name
value = float(_parse_value(text[name_end:]))
return Sample(name, {}, value)
value, timestamp = _parse_value_and_timestamp(text[name_end:])
return Sample(name, {}, value, timestamp)


def text_fd_to_metric_families(fd):
Expand Down
7 changes: 4 additions & 3 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def test_escaping(self):
metric_family.add_metric(["b\\a\\z"], 2)
self.assertEqualMetrics([metric_family], list(families))

def test_timestamps_discarded(self):
def test_timestamps(self):
families = text_string_to_metric_families("""# TYPE a counter
# HELP a help
a{foo="bar"} 1\t000
Expand All @@ -282,8 +282,9 @@ def test_timestamps_discarded(self):
b 2 1234567890
""")
a = CounterMetricFamily("a", "help", labels=["foo"])
a.add_metric(["bar"], 1)
b = CounterMetricFamily("b", "help", value=2)
a.add_metric(["bar"], 1, timestamp=0)
b = CounterMetricFamily("b", "help")
b.add_metric([], 2, timestamp=1234567890)
Copy link
Contributor

Choose a reason for hiding this comment

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

This isn't right, Prometheus timestamp are in milliseconds whereas the Python client is using the OpenMetrics data model so is working in seconds.

self.assertEqualMetrics([a, b], list(families))

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