alerting icon indicating copy to clipboard operation
alerting copied to clipboard

[BUG][QUESTION] How to work around JSON templating for Webhooks?

Open adityaj1107 opened this issue 3 years ago • 10 comments

Issue by DandyDeveloper Friday Apr 03, 2020 at 08:32 GMT Originally opened as https://github.com/opendistro-for-elasticsearch/alerting/issues/196


I can't see a previous discussion on this, and I don't know how to workaround it. I don't consider this a bug in Open ES, but because of the inherent expectations for the recipients to receive JSON objects, how do we deal with this?

What do we do when dealing with JSON object on webhooks? Current templating with aggregations basically makes it impossible due to trailing commas.

For example, using any kind of templating requires iterations over an object in the ES response object. However, every json iteration that will be separated by a comma will always have a trailing comma like so:

{
  "obj1": "val1",
  "obj2": "val2",
  "obj3": "val3",
  "obj4": "val4", <- Sadface
}

The vast majority of JSON compliant webhooks will reject this / fail to decode the JSON object on the other end.

Any suggestions on how to get around this?

Discussion here: https://discuss.opendistrocommunity.dev/t/mustache-template-function-removing-trailing-comma/2528

adityaj1107 avatar Jun 02 '21 21:06 adityaj1107

Comment by DandyDeveloper Friday Apr 03, 2020 at 08:34 GMT


@dbbaughe You've been super helpful recently. Wonder if you've got a suggestion for this? Could we have a trim function in ES that might check for this.

Say if the header is present in webhook, run it through a validate function that will trim the object?

adityaj1107 avatar Jun 02 '21 21:06 adityaj1107

Comment by DandyDeveloper Wednesday May 20, 2020 at 03:53 GMT


https://discuss.opendistrocommunity.dev/t/mustache-template-function-removing-trailing-comma/2528/4

Discussion is ongoing here.

adityaj1107 avatar Jun 02 '21 21:06 adityaj1107

Comment by samidhtalsania Thursday Jun 11, 2020 at 22:09 GMT


+1 Ran into the same issue

adityaj1107 avatar Jun 02 '21 21:06 adityaj1107

Comment by NicoP-S Thursday Jul 16, 2020 at 10:28 GMT


I have the same problem. At the moment I add an empty object after the iteration. On the Webhook side the empty object will be ignored. A clean json would be better.

adityaj1107 avatar Jun 02 '21 21:06 adityaj1107

Hi @DandyDeveloper @samidhtalsania @NicoP-S

In our backend code we convert the response from the mustache templates to a string and then send that string as a payload to the Webhooks. The web-hooks (chime / slack) currently don’t have the response payload in the json format.

Code Link for the template compiling in the String Format

Code Link for the Chime Message

Code Link for the Slack Message

Can you share the exact queries and the exact templates which you are using the build the response for the web-hook? It ll help me deep dive into this issue, if any and debug it further.

adityaj1107 avatar Jun 27 '21 22:06 adityaj1107

@aditjind I replied to you on the forum post.

DandyDeveloper avatar Jun 28 '21 00:06 DandyDeveloper

A possible solution for this problem: We extract all json objects from the string and then validate those json objects by removing the trailing commas etc. and then add them back to the string. This is a workaround but I agree this would be error prone and is not a clean solution.

Extracting json objects from a string would mean matching the json with a regex from that string. The regex should be such that it should recursively match the json. There would be cases where there are curly braces present in the string. We should get around those cases in the regex match.

A completely cleaner solution would be to provide users specific and a separate support for providing json strings. Those strings can be then validated and sanitized as needed to prevent breaking the readers of the web-hooks.

adityaj1107 avatar Jun 30 '21 03:06 adityaj1107

@aditjind I would much prefer a solution for JSON objects specifically. Mustache just isn't designed for this.

Mustache should be defined as templating notifications for alerts.

Webhooks should probably be assumed to expect common object patterns like JSON / YAML Etc.

DandyDeveloper avatar Jun 30 '21 03:06 DandyDeveloper

Having looked up some alternatives here, I've seen mentions to JSON templating libraries such as:

Having looked at these only superficially, I imagine that if we go for a JSON specific templating, it will excel in the JSON formatting part but not necessarily be able to cover general text templating. However, if we can offer the choice to toggle between multiple possible offerings, the user may be able to cater to their own situation/use case.

Does the community have any preferences or suggestions on templating mechanisms that they'd like to see here?

qreshi avatar Nov 09 '22 19:11 qreshi

Stumbled upon this issue, and I'm interested where this is going. Any chances to elevate this to be considered?