Skip to content

Commit 85ee2c9

Browse files
committed
Fix Prometheus UTC timestamp formatting
Signed-off-by: Miro <200482516+Mirochill@users.noreply.github.com>
1 parent bd5a653 commit 85ee2c9

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## [Unreleased][]
44

5+
### Fixed
6+
7+
- Avoid the deprecated `datetime.utcfromtimestamp()` call when formatting
8+
Prometheus chart labels.
9+
510
[Unreleased]: https://github.com/chaostoolkit/chaostoolkit-reporting/compare/0.18.0...HEAD
611

712
## [0.18.0][] - 2024-12-02
@@ -270,4 +275,4 @@
270275
271276
### Added
272277
273-
- Initial release
278+
- Initial release

chaosreport/__init__.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import subprocess
1111
import tempfile
1212
from base64 import b64encode
13-
from datetime import datetime, timedelta
13+
from datetime import datetime, timedelta, timezone
1414
from importlib.metadata import version, PackageNotFoundError
1515
from typing import Any, Dict, List
1616

@@ -470,9 +470,11 @@ def generate_chart_from_prometheus(run: Run, export_format: str):
470470
# now we have our range of abscissa, let's map those
471471
# timestamps to formatted strings
472472
x = sorted(list(x))
473-
fromts = datetime.utcfromtimestamp
474473
chart.x_labels = [
475-
fromts(v).strftime("%Y-%m-%d\n %H:%M:%S") for v in x
474+
datetime.fromtimestamp(v, timezone.utc).strftime(
475+
"%Y-%m-%d\n %H:%M:%S"
476+
)
477+
for v in x
476478
]
477479
chart.x_labels_major = chart.x_labels[::10]
478480
chart.title = "Query - {}".format(

tests/test_prometheus_charts.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import chaosreport
2+
3+
4+
class LineChart:
5+
instances = []
6+
7+
def __init__(self, **kwargs):
8+
self.x_labels = []
9+
self.x_labels_major = []
10+
self.series = []
11+
self.title = None
12+
LineChart.instances.append(self)
13+
14+
def add(self, label, values, allow_interruptions=False):
15+
self.series.append((label, values, allow_interruptions))
16+
17+
def render(self, **kwargs):
18+
return b"<svg />"
19+
20+
21+
def test_generate_chart_from_prometheus_formats_labels_in_utc(monkeypatch):
22+
LineChart.instances = []
23+
monkeypatch.setattr(chaosreport.pygal, "Line", LineChart)
24+
25+
run = {
26+
"activity": {
27+
"provider": {
28+
"arguments": {"query": "up"},
29+
},
30+
},
31+
"output": {
32+
"data": {
33+
"resultType": "matrix",
34+
"result": [
35+
{
36+
"metric": {"pod": "frontend"},
37+
"values": [
38+
(0, "1"),
39+
(3600, "2"),
40+
],
41+
}
42+
],
43+
},
44+
},
45+
}
46+
47+
chaosreport.generate_chart_from_prometheus(run, "html")
48+
49+
chart = LineChart.instances[0]
50+
assert chart.x_labels == [
51+
"1970-01-01\n 00:00:00",
52+
"1970-01-01\n 01:00:00",
53+
]
54+
assert chart.x_labels_major == ["1970-01-01\n 00:00:00"]

0 commit comments

Comments
 (0)