From 85ee2c9fc0a2232ccee164619c85e35ac336b070 Mon Sep 17 00:00:00 2001 From: Miro <200482516+Mirochill@users.noreply.github.com> Date: Sat, 23 May 2026 23:02:20 +0200 Subject: [PATCH] Fix Prometheus UTC timestamp formatting Signed-off-by: Miro <200482516+Mirochill@users.noreply.github.com> --- CHANGELOG.md | 7 ++++- chaosreport/__init__.py | 8 +++-- tests/test_prometheus_charts.py | 54 +++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 tests/test_prometheus_charts.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 1edb712..c0d56da 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,11 @@ ## [Unreleased][] +### Fixed + +- Avoid the deprecated `datetime.utcfromtimestamp()` call when formatting + Prometheus chart labels. + [Unreleased]: https://github.com/chaostoolkit/chaostoolkit-reporting/compare/0.18.0...HEAD ## [0.18.0][] - 2024-12-02 @@ -270,4 +275,4 @@ ### Added -- Initial release \ No newline at end of file +- Initial release diff --git a/chaosreport/__init__.py b/chaosreport/__init__.py index 887b92e..c3b9c0a 100644 --- a/chaosreport/__init__.py +++ b/chaosreport/__init__.py @@ -10,7 +10,7 @@ import subprocess import tempfile from base64 import b64encode -from datetime import datetime, timedelta +from datetime import datetime, timedelta, timezone from importlib.metadata import version, PackageNotFoundError from typing import Any, Dict, List @@ -470,9 +470,11 @@ def generate_chart_from_prometheus(run: Run, export_format: str): # now we have our range of abscissa, let's map those # timestamps to formatted strings x = sorted(list(x)) - fromts = datetime.utcfromtimestamp chart.x_labels = [ - fromts(v).strftime("%Y-%m-%d\n %H:%M:%S") for v in x + datetime.fromtimestamp(v, timezone.utc).strftime( + "%Y-%m-%d\n %H:%M:%S" + ) + for v in x ] chart.x_labels_major = chart.x_labels[::10] chart.title = "Query - {}".format( diff --git a/tests/test_prometheus_charts.py b/tests/test_prometheus_charts.py new file mode 100644 index 0000000..9edcd4b --- /dev/null +++ b/tests/test_prometheus_charts.py @@ -0,0 +1,54 @@ +import chaosreport + + +class LineChart: + instances = [] + + def __init__(self, **kwargs): + self.x_labels = [] + self.x_labels_major = [] + self.series = [] + self.title = None + LineChart.instances.append(self) + + def add(self, label, values, allow_interruptions=False): + self.series.append((label, values, allow_interruptions)) + + def render(self, **kwargs): + return b"" + + +def test_generate_chart_from_prometheus_formats_labels_in_utc(monkeypatch): + LineChart.instances = [] + monkeypatch.setattr(chaosreport.pygal, "Line", LineChart) + + run = { + "activity": { + "provider": { + "arguments": {"query": "up"}, + }, + }, + "output": { + "data": { + "resultType": "matrix", + "result": [ + { + "metric": {"pod": "frontend"}, + "values": [ + (0, "1"), + (3600, "2"), + ], + } + ], + }, + }, + } + + chaosreport.generate_chart_from_prometheus(run, "html") + + chart = LineChart.instances[0] + assert chart.x_labels == [ + "1970-01-01\n 00:00:00", + "1970-01-01\n 01:00:00", + ] + assert chart.x_labels_major == ["1970-01-01\n 00:00:00"]