base-golang/pkg/httpclient/alarm.go

30 lines
649 B
Go
Raw Normal View History

2024-07-23 10:23:43 +08:00
package httpclient
import (
"bufio"
"bytes"
"go.uber.org/zap"
)
// Verify parse the body and verify that it is correct
type AlarmVerify func(body []byte) (shouldAlarm bool)
type AlarmObject interface {
Send(subject, body string) error
}
func onFailedAlarm(title string, raw []byte, logger *zap.Logger, alarmObject AlarmObject) {
buf := bytes.NewBuffer(nil)
scanner := bufio.NewScanner(bytes.NewReader(raw))
for scanner.Scan() {
buf.WriteString(scanner.Text())
buf.WriteString(" <br/>")
}
if err := alarmObject.Send(title, buf.String()); err != nil && logger != nil {
logger.Error("calls failed alarm err", zap.Error(err))
}
}