Hi Craig,

Thanks for the bug report.

On Fri, Sep 09, 2022 at 08:58:55AM +1000, Craig Small wrote:
> prometheus-xmpp-alerts is expecting its datetime to be in UTC, but that
> is not what the prometheus alertmanager uses.
> 
> This means basically prometheus-xmpp-alerts does not work with
> prometheus-alermanager which is its main use-case.
> 
> I suspect that one or both of the programming teams assumes the servers
> run UTC which is a pretty bad assuption, not sure if it is xmpp-alerts
> fault for hard-coding the timezone or alermanagers fault for not setting
> it. In reality its both.
> 
> In my opinion, the xmpp-alerts should accept both a UTC timezone and a
> different timezone.
> 
> The file in question is 
> /usr/lib/python3/dist-packages/prometheus_xmpp/__init__.py
> with the function parse_timestring
> 
>     ts = re.sub('\\.([0-9]{6})([0-9]{0,3})Z$', r'.\1Z', ts)
>     return datetime.strptime(ts, '%Y-%m-%dT%H:%M:%S.%fZ')
> 
> The error message is:
> Sep 08 21:26:39 constantine prometheus-xmpp-alerts[2111702]: ValueError: time 
> data '2022-09-08T21:26:09.706263468+10:00' does not match format 
> '%Y-%m-%dT%H:%M:%S.%fZ'
> 
> which is true, "+10:00" doesn't match "Z", but it does match "%z". Also
> %z understands "Z" too. Oddly enough it doesn't understand any other
> sort of timezone, e.g. K is UTC+10h but that's strptime for you.
> 
> You'll also notice that the nanoseconds have not been stripped, again
> because of the assumption of the timezone for the regex.
> 
> So the answer is, do timezones properly.
> 1) Make the regex understand both sorts of timezones
> 2) Use %z because that's what its for.
> 
> The regex is we either get "Z" or +/-hh:mm so the solution on a single
> line is:
> 
> >>> datetime.strptime(re.sub('\\.([0-9]{6})([0-9]{0,3})(Z|[-+][0-9]{1,2}:[0-9]{2})$',
> >>>  r'.\1\3',"2022-09-08T21:26:09.706263468+10:00"), 
> >>> '%Y-%m-%dT%H:%M:%S.%f%z')
> datetime.datetime(2022, 9, 8, 21, 26, 9, 706263, 
> tzinfo=datetime.timezone(datetime.timedelta(seconds=36000)))
> >>> datetime.strptime(re.sub('\\.([0-9]{6})([0-9]{0,3})(Z|[-+][0-9]{1,2}:[0-9]{2})$',
> >>>  r'.\1\3',"2022-09-08T21:26:09.706263468Z"), '%Y-%m-%dT%H:%M:%S.%f%z')
> datetime.datetime(2022, 9, 8, 21, 26, 9, 706263, tzinfo=datetime.timezone.utc)
> >>> datetime.strptime(re.sub('\\.([0-9]{6})([0-9]{0,3})(Z|[-+][0-9]{1,2}:[0-9]{2})$',
> >>>  r'.\1\3',"2022-09-08T21:26:09.706263468-10:00"), 
> >>> '%Y-%m-%dT%H:%M:%S.%f%z')
> datetime.datetime(2022, 9, 8, 21, 26, 9, 706263, 
> tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=50400)))
> 
> I can provide a patch if required.

I believe this is already fixed upstream. See 
https://github.com/jelmer/prometheus-xmpp-alerts/blob/master/prometheus_xmpp/__init__.py#L23

Jelmer

Reply via email to