8000 Check for non-real timestamps · danarwix/client_python@afe09a3 · GitHub
[go: up one dir, main page]

Skip to content

Commit afe09a3

Browse files
committed
Check for non-real timestamps
Signed-off-by: Brian Brazil <brian.brazil@robustperception.io>
1 parent 61480b2 commit afe09a3

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

prometheus_client/openmetrics/parser.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@ def _parse_timestamp(timestamp):
7575
return core.Timestamp(int(parts[0]), int(parts[1][:9].ljust(9, "0")))
7676
except ValueError:
7777
# Float.
78-
return float(timestamp)
78+
ts = float(timestamp)
79+
if math.isnan(ts) or math.isinf(ts):
80+
raise ValueError("Invalid timestamp: {0!r}".format(timestamp))
81+
return ts
7982

8083

8184
def _parse_labels(it, text):

tests/openmetrics/test_parser.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,10 @@ def test_invalid_input(self):
477477
('a 1 z\n# EOF\n'),
478478
('a 1 1z\n# EOF\n'),
479479
('a 1 1.1.1\n# EOF\n'),
480+
('a 1 NaN\n# EOF\n'),
481+
('a 1 Inf\n# EOF\n'),
482+
('a 1 +Inf\n# EOF\n'),
483+
('a 1 -Inf\n# EOF\n'),
480484
# Bad exemplars.
481485
('# TYPE a histogram\na_bucket{le="+Inf"} 1 #\n# EOF\n'),
482486
('# TYPE a histogram\na_bucket{le="+Inf"} 1# {} 1\n# EOF\n'),

0 commit comments

Comments
 (0)
0