API error Bad data when sending alerts via another application

16 views
Skip to first unread message

Adarsh Kumar Pandey

unread,
Apr 15, 2020, 4:55:36 AM4/15/20
to Prometheus Users
Hie all,
I was trying to generate alerts from a custom application which will do a POST at the alertmanager endpoint /api/v1/alerts ....
When sending alerts as a json I'm getting an error saying:

level=error ts=2020-04-15T08:48:28.224Z caller=api.go:780 component=api version=v1 msg="API error" err="bad_data: json: cannot unmarshal string into Go value of type []*types.Alert"

my alert content is (I have censored sensitive contents as ***):

alert='''[{"status": "firing", "labels": {"alertname": "InstanceDown", "instance": "localhost:9100", "job": "node", "severity": "warning"}, "annotations": {}, "startsAt": "2020-04-06T19:57:38.628106884+05:30", "endsAt": "0001-01-01T00:00:00Z", "generatorURL": "http://adarsh-***", "fingerprint": "****"}]'''

Please help me out guys I'm not able to understand what i'm doing wrong ... as far as i've read this seems to me in correct format (without the json like representation)
thanks

Łukasz Mierzwa

unread,
Apr 15, 2020, 5:37:16 AM4/15/20
to Prometheus Users
This is not valid json, paste it to https://jsonlint.com/ to verify.
Valid JSON body example would be:

[{
"status": "firing",
"labels": {
"alertname": "InstanceDown",
"instance": "localhost:9100",
"job": "node",
"severity": "warning"
},
"annotations": {},
"startsAt": "2020-04-06T19:57:38.628106884+05:30",
"endsAt": "0001-01-01T00:00:00Z",
"generatorURL": "http://adarsh-***",
"fingerprint": "****"
}]

You seem to be sending a some internal attributes
status - there's no "firing" status in alertmanager, and it handles status internally so you should skip it
fingerprint - AFAIR that's calculated internally from alert body, try skipping it
endsAt "0001-01-01T00:00:00Z" value acts as "empty value" so if you're not trying to set a custom endsAt then try skipping it

You're using v1 API, which will go away soon, you're better of switching to v2, which has openapi spec so you can generate a client using https://github.com/prometheus/alertmanager/blob/master/api/v2/openapi.yaml

Adarsh Kumar Pandey

unread,
Apr 15, 2020, 5:47:01 AM4/15/20
to Prometheus Users
yes sure i'll look into that too..Thanks so much for such a detailed answer..I really appreciate that..

Adarsh Kumar Pandey

unread,
Apr 15, 2020, 5:55:38 AM4/15/20
to Prometheus Users
Could you please send a simple(bare minimum) alert sample that should be sent...
and do i have to make a major change in moving from v1 to v2 or just the url will change?
thanks

Adarsh Kumar Pandey

unread,
Apr 15, 2020, 6:03:16 AM4/15/20
to Prometheus Users
Yeah I eliminated startsAt endsAt and fingerprint and its working now ... but i read in other answers that to resolve an alert when generating an alert we can use endsAt and mention some past time to disable that alert...
Thanks once again for the help...
Reply all
Reply to author
Forward
0 new messages