Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValidationError for startsAt and endsAt fields #122

Closed
1 task done
orgito opened this issue Jun 6, 2019 · 3 comments
Closed
1 task done

ValidationError for startsAt and endsAt fields #122

orgito opened this issue Jun 6, 2019 · 3 comments
Assignees
Labels

Comments

@orgito
Copy link

orgito commented Jun 6, 2019

Prerequisites

Description

promt2teams is trowing the exception bellow after receiving a valid alert from alertmanager:

2019-06-06 13:53:17,619 - flask.app - ERROR - Exception on /v2/ToolsSquadTestes [POST]
Traceback (most recent call last):
...
  File "/usr/local/lib/python3.6/site-packages/prom2teams/app/versions/v2/namespace.py", line 26, in post
    alerts = self.schema.load(request.get_json())
  File "/usr/local/lib/python3.6/site-packages/marshmallow/schema.py", line 707, in load
    postprocess=True,
  File "/usr/local/lib/python3.6/site-packages/marshmallow/schema.py", line 867, in _do_load
    raise exc
marshmallow.exceptions.ValidationError: {'alerts': {0: {'endsAt': ['Not a valid date.'], 'startsAt': ['Not a valid date.']}}}

Steps to Reproduce

1 - Alertmanager sends an alert including valid RFC3339 dates. But the format does not conform with the validation message_schema.py:

Excerpt of a sample alert:

{
...
      "startsAt": "2019-06-05T16:22:28.29468897Z",
      "endsAt": "0001-01-01T00:00:00Z",
...
}

message_schema.py defines those fields as:

startsAt = fields.Date('%Y-%m-%dT%H:%M:%S.%fZ')
endsAt = fields.Date('%Y-%m-%dT%H:%M:%S.%fZ')

The problem here is the use of %f for the second fraction. This limits the fraction to microsseconds. But higher precision is valid in RFC3339. It also rejects valid dates that do not have the fractions.

A quickfix is to change the fields to:

startsAt = fields.DateTime()
endsAt = fields.DateTime()

But this will accept other date formats too.

Expected behavior: prom2teams shoud accept any RFC3339 valid format for startsAt and endsAt

Actual behavior: prom2teams is trowing an exception when the second fraction is higher precision than microseconds or when there is no second fraction.

Reproduces how often: Every time one of startsAt and endsAt contains a higher precision fractions for seconds or does not contains the fractions.

Versions

python 3.6.8
alertmanager 0.17.0
prometheus 2.10.0
prom2teams 2.3.1 (installed via pip)
marshmallow 3.0.0rc6 (installed via pip as prom2teams dependecy)

@miguel-chacon
Copy link
Collaborator

We'll take a look ASAP, thanks @orgito

@orgito
Copy link
Author

orgito commented Jun 18, 2019

Thank you!

@orgito orgito closed this as completed Jun 18, 2019
@prsshini
Copy link

Is this issue resolved? I have the latest prom2teams 3.2.2 which throws the same error..

When I post the message without startsAt inthe payload Json, I get the error
curl -X POST $url -d '[{"status": "firing","labels": {"alertname": "subha-1021pm","service": "curl","severity": "critical","instance": "0"},"annotations": {"summary": "This is a summary","description": "This is a description."},"generatorURL": "http://prometheus.int.example.net/<generating_expression>"}]'

Error Message:
marshmallow.exceptions.ValidationError: {'alerts': {0: {'startsAt': ['Not a valid datetime.']}}}
Jul 23 05:24:17 ip-172-18-0-114 prom2teams[11629]: 2021-07-23 05:24:17,464 - werkzeug - INFO - 127.0.0.1 - - [23/Jul/2021 05:24:17] "POST /v2/Connector HTTP/1.1" 500 -

When I post it with the startsAt in the payload, I can send the message successfully.
curl -X POST $url -d '[{"status": "firing","labels": {"alertname": "subha-955pm","service": "curl","severity": "critical","instance": "0"},"annotations": {"summary": "This is a summary","description": "This is a description."},"generatorURL": "http://prometheus.int.example.net/<generating_expression>","startsAt": "2020-07-23T01:05:36+00:00"}]'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants