Skip to content

Commit

Permalink
log error when meet invalid json (#473)
Browse files Browse the repository at this point in the history
  • Loading branch information
suchen-sci authored Jan 12, 2024
1 parent 707e216 commit e742fca
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 12 deletions.
4 changes: 4 additions & 0 deletions notify/ringcentral/ringcentral.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package ringcentral

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -75,6 +76,9 @@ func (c *NotifyConfig) SendRingCentral(title, msg string) error {
]
}
`, report.JSONEscape(title), report.JSONEscape(msg))
if !json.Valid([]byte(msgContent)) {
log.Errorf("[%s / %s ] - %v, err: invalid json", c.Kind(), c.Name(), msgContent)
}

req, err := http.NewRequest(http.MethodPost, c.WebhookURL, bytes.NewBuffer([]byte(msgContent)))
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions notify/slack/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,5 @@ func (c *NotifyConfig) SendSlackNotification(msg string) error {
log.Debugf(msg)
return fmt.Errorf("Error response from Slack - code [%d] - msg [%s]", resp.StatusCode, string(buf))
}
// if buf.String() != "ok" {
// return errors.New("Non-ok response returned from Slack " + buf.String())
// }
return nil
}
5 changes: 5 additions & 0 deletions notify/wecom/wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package wecom

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -64,6 +65,10 @@ func (c *NotifyConfig) SendWecomNotification(msg string) error {
}
}
`, report.JSONEscape(msg))
if !json.Valid([]byte(msgContent)) {
log.Errorf("[%s / %s ] - %v, err: invalid json", c.Kind(), c.Name(), msgContent)
}

req, err := http.NewRequest(http.MethodPost, c.WebhookURL, bytes.NewBuffer([]byte(msgContent)))
if err != nil {
return err
Expand Down
17 changes: 12 additions & 5 deletions report/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ func markdown(r probe.Result, f Format) string {
// ToSlack convert the object to ToSlack notification
// Go to https://app.slack.com/block-kit-builder to build the notification block
func ToSlack(r probe.Result) string {

json := `
jsonMsg := `
{
"text": "%s",
"blocks": [
Expand Down Expand Up @@ -193,13 +192,17 @@ func ToSlack(r probe.Result) string {
r.Title(), r.Status.Emoji(), r.Endpoint, rtt, JSONEscape(r.Message))
context := SlackTimeFormation(r.StartTime, " probed at ", global.GetTimeFormat())
summary := fmt.Sprintf("%s %s - %s", r.Title(), r.Status.Emoji(), JSONEscape(r.Message))
return fmt.Sprintf(json, summary, body, context)
output := fmt.Sprintf(jsonMsg, summary, body, context)
if !json.Valid([]byte(output)) {
log.Errorf("ToSlack() for %s: Invalid JSON: %s", r.Name, output)
}
return output
}

// ToLark convert the object to Lark notification
// Go to https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN#4996824a to build the notification block
func ToLark(r probe.Result) string {
json := `
jsonMsg := `
{
"msg_type": "interactive",
"card": {
Expand Down Expand Up @@ -253,7 +256,11 @@ func ToLark(r probe.Result) string {
rtt := r.RoundTripTime.Round(time.Millisecond)
content := fmt.Sprintf("%s - ⏱ %s\\n%s", r.Endpoint, rtt, JSONEscape(r.Message))
footer := global.FooterString() + " probed at " + FormatTime(r.StartTime)
return fmt.Sprintf(json, headerColor, title, content, footer)
output := fmt.Sprintf(jsonMsg, headerColor, title, content, footer)
if !json.Valid([]byte(output)) {
log.Errorf("ToLark() for %s: Invalid JSON: %s", r.Name, output)
}
return output
}

// ToCSV convert the object to CSV
Expand Down
16 changes: 12 additions & 4 deletions report/sla.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func SLAHTMLFilter(probers []probe.Prober, filter *SLAFilter) string {
// SLASlackSection return the slack json format string to stat
func SLASlackSection(r *probe.Result) string {

json := `
jsonMsg := `
{
"type": "mrkdwn",
"text": "*%s* - %s` +
Expand All @@ -268,10 +268,14 @@ func SLASlackSection(r *probe.Result) string {
message = "`" + message + "`"
}

return fmt.Sprintf(json, r.Name, JSONEscape(r.Endpoint),
output := fmt.Sprintf(jsonMsg, r.Name, JSONEscape(r.Endpoint),
DurationStr(r.Stat.UpTime), DurationStr(r.Stat.DownTime), r.SLAPercent(),
r.Stat.Total, SLAStatusText(r.Stat, MarkdownSocial),
t, r.Status.Emoji()+" "+r.Status.String(), message)
if !json.Valid([]byte(output)) {
log.Errorf("SLASlackSection() for %s: invalid json: %s", r.Name, output)
}
return output
}

// SLASlack generate all probes stat message to slack block string
Expand Down Expand Up @@ -391,7 +395,7 @@ func SLALarkSection(r *probe.Result) string {

// SLALark return a full stat report
func SLALark(probers []probe.Prober) string {
json := `
jsonMsg := `
{
"msg_type": "interactive",
"card": {
Expand Down Expand Up @@ -429,7 +433,11 @@ func SLALark(probers []probe.Prober) string {
}

elements := strings.Join(sections, "")
s := fmt.Sprintf(json, title, elements)
s := fmt.Sprintf(jsonMsg, title, elements)
if !json.Valid([]byte(s)) {
log.Errorf("SLALark(): invalid json: %s", s)
}

fmt.Printf("SLA: %s\n", s)
return s
}
Expand Down

0 comments on commit e742fca

Please sign in to comment.