diff --git a/promgen/prometheus.py b/promgen/prometheus.py index 5fd5b1984..40944aacf 100644 --- a/promgen/prometheus.py +++ b/promgen/prometheus.py @@ -8,7 +8,6 @@ import tempfile from urllib.parse import urljoin -import pytz import yaml from dateutil import parser from django.core.exceptions import ValidationError @@ -264,6 +263,9 @@ def silence(*, labels, duration=None, **kwargs): Post a silence message to Alert Manager Duration should be sent in a format like 1m 2h 1d etc """ + # We active a timezone here, because the frontend (browser) + # will be POSTing date times without timezones + timezone.activate(util.setting("timezone", "UTC")) if duration: start = timezone.now() @@ -278,9 +280,11 @@ def silence(*, labels, duration=None, **kwargs): kwargs["startsAt"] = start.isoformat() kwargs["endsAt"] = end.isoformat() else: - local_timezone = pytz.timezone(util.setting("timezone", "UTC")) for key in ["startsAt", "endsAt"]: - kwargs[key] = local_timezone.localize(parser.parse(kwargs[key])).isoformat() + dt = parser.parse(kwargs[key]) + if timezone.is_naive(dt): + dt = timezone.make_aware(dt) + kwargs[key] = dt.isoformat() kwargs["matchers"] = [ { diff --git a/promgen/templatetags/promgen.py b/promgen/templatetags/promgen.py index f4f7cc960..3a1c2c157 100644 --- a/promgen/templatetags/promgen.py +++ b/promgen/templatetags/promgen.py @@ -10,12 +10,10 @@ import yaml from django import template from django.urls import reverse +from django.utils import timezone from django.utils.html import format_html from django.utils.safestring import mark_safe from django.utils.translation import gettext as _ -from pytz import timezone - -from promgen import util register = template.Library() @@ -98,9 +96,16 @@ def pretty_yaml(data): @register.filter() def strftime(timestamp, fmt): - tz = util.setting("timezone", "UTC") + """ + Convert a timestamp to a specific format. + + Normally we would use the built in django `time` filters, but sometimes we + get a numeric value as a timestamp (from Prometheus results) so we want a + single place where we can convert any type of value to a timestamp. + """ if isinstance(timestamp, int) or isinstance(timestamp, float): - return timezone(tz).localize(datetime.fromtimestamp(timestamp)).strftime(fmt) + dt = datetime.fromtimestamp(timestamp, timezone.utc) + return timezone.localtime(dt).strftime(fmt) return timestamp