bugfix: callback ibex

This commit is contained in:
Ulric Qin 2021-12-06 18:20:44 +08:00
parent 2185fbff65
commit 88063cd30e
1 changed files with 42 additions and 4 deletions

View File

@ -1,12 +1,15 @@
package engine
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
"github.com/toolkits/pkg/logger"
"github.com/toolkits/pkg/net/httplib"
"github.com/didi/nightingale/v5/src/models"
"github.com/didi/nightingale/v5/src/pkg/ibex"
@ -14,6 +17,39 @@ import (
"github.com/didi/nightingale/v5/src/server/memsto"
)
func PostJSON(url string, timeout time.Duration, v interface{}) (response []byte, code int, err error) {
var bs []byte
bs, err = json.Marshal(v)
if err != nil {
return
}
bf := bytes.NewBuffer(bs)
client := http.Client{
Timeout: timeout,
}
req, err := http.NewRequest("POST", url, bf)
req.Header.Set("Content-Type", "application/json")
var resp *http.Response
resp, err = client.Do(req)
if err != nil {
return
}
code = resp.StatusCode
if resp.Body != nil {
defer resp.Body.Close()
response, err = ioutil.ReadAll(resp.Body)
}
return
}
func callback(event *models.AlertCurEvent) {
urls := strings.Fields(event.Callbacks)
for _, url := range urls {
@ -21,8 +57,10 @@ func callback(event *models.AlertCurEvent) {
continue
}
if strings.HasPrefix(url, "${ibex}") && !event.IsRecovered {
handleIbex(url, event)
if strings.HasPrefix(url, "${ibex}") {
if !event.IsRecovered {
handleIbex(url, event)
}
continue
}
@ -30,7 +68,7 @@ func callback(event *models.AlertCurEvent) {
url = "http://" + url
}
resp, code, err := httplib.PostJSON(url, 5*time.Second, event, map[string]string{})
resp, code, err := PostJSON(url, 5*time.Second, event)
if err != nil {
logger.Errorf("event_callback(rule_id=%d url=%s) fail, resp: %s, err: %v, code: %d", event.RuleId, url, string(resp), err, code)
} else {