我已经设置了一个 Kibana 观察者,它应该在满足条件时向通道发送一条松弛消息。Watcher 似乎工作正常,它能够查询日志、获取命中、准备有效负载以将消息发布到 Slack 频道。但是对 slack 的调用会导致 400 错误(无效的有效负载),而当通过邮递员尝试相同的有效负载时,似乎没问题。(唯一的区别是在 Kibana watcher 中,用于松弛 POST 调用的 body json 被视为字符串,而在尝试通过邮递员时,我必须使用已解析的 json 对象)。
观察者动作:
"condition": {
"compare": {
"ctx.payload.hits.total": {
"gt": 0
}
}
},
"actions": {
"send_trigger": {
"throttle_period_in_millis": 60000,
"transform": {
"script": {
"source": "def payload = ctx.payload; def msgData = ctx.payload.hits.hits.0._source.message.replace('\"', ''); msgData = msgData.replace('\\', ''); payload.msg = msgData; return payload;",
"lang": "painless"
}
},
"webhook": {
"scheme": "https",
"host": "hooks.slack.com",
"port": 443,
"method": "post",
"path": "/services/********/*********/******************",
"params": {},
"headers": {
"Content-type": "application/json"
},
"body": "{\"channel\": \"alert-channel-name\",\"username\": \"slackKibanaUser\", \"attachments\": [ { \"color\": \"danger\", \"title\": \" Alert Title For Event Occurance \", \"title_link\" : \"https://kibana.whatever.biz.com/app/kibana\", \"text\": \" *Detail:* \n ``` {{ ctx.payload.msg}} \n ``` \", \"fields\": [ {\"title\": \"Traceid\", \"value\": \"`{{ ctx.payload.hits.hits.0._source.traceId}}`\", \"short\": true}, { \"title\": \"Environment\", \"value\": \"{{ctx.payload.hits.hits.0._source.fields.environment}}\", \"short\": false }], \"footer\": \"{{ctx.payload.hits.hits.0.fields.environment}}\", \"footer_icon\": \"https://platform.slack-edge.com/img/default_application_icon.png\" }]} "
}
}
}
watcher error 中记录的响应:
"reason": "received [400] status code",
"webhook": {
"request": {
"host": "hooks.slack.com",
"port": 443,
"scheme": "https",
"method": "post",
"path": "/services/********/*********/******************",
"headers": {
"Content-type": "application/json"
},
"body": "{\"channel\": \"alert-channel-name\",\"username\": \"slackKibanaUser\", \"attachments\": [ { \"color\": \"danger\", \"title\": \" Alert Title For Event Occurance \", \"title_link\" : \"https://kibana.whatever.biz.com/app/kibana\", \"text\": \" *Detail:* \n ``` 2020-05-28T00:00:00.000Z\tsome-random-numbers \tERROR\tInvoke Error\t{errorType:MyCustomError,errorMessage:{\\errorMessage\\:\\Something went wrong while processing : More error details goes here.\\,\\payload\\:\\[id=undefined, key1=value1, key2=value2, timestampRecorded=12/10/2019, 02:00:00, timestampActual=12/10/2019, 01:59:00, key3=value3, key4=v:a:l:u:e:4, key5=v:a:l:u:e:5]\\},name:MyCustomError,stack:[MyCustomError: {\\errorMessage\\:\\Something went wrong while processing : More error details goes here.\\,\\payload\\:\\[id=undefined, key1=value1, key2=value2, timestampRecorded=12/10/2019, 02:00:00, timestampActual=12/10/2019, 01:59:00, key3=value3, key4=v:a:l:u:e:4, key5=v:a:l:u:e:5]\\}, at Runtime.exports.functionOne (/var/task/index.js:49:9)]} \n ``` \", \"fields\": [ {\"title\": \"Traceid\", \"value\": \"``\", \"short\": true}, { \"title\": \"Environment\", \"value\": \"test-env\", \"short\": false }], \"footer\": \"\", \"footer_icon\": \"https://platform.slack-edge.com/img/default_application_icon.png\" }]} "
},
"response": {
"status": 400,
"headers": {
...
"referrer-policy": [
"no-referrer"
],
"connection": [
"keep-alive"
],
"content-type": [
"text/html"
]
},
"body": "invalid_payload"
}
}
如果我使用上面相同的正文字段,将其解析为 json,并使用邮递员进行 post 调用,我会得到 200 Ok 响应,并且消息会发布到 slack 频道。不知道我错过了什么。