2 Star 0 Fork 339

hxchjm / go-zero

forked from kevwan / go-zero 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
remotewriter.go 842 Bytes
一键复制 编辑 原始数据 按行查看 历史
kevwan 提交于 2020-08-08 16:40 . update package reference
package stat
import (
"bytes"
"encoding/json"
"errors"
"net/http"
"time"
"github.com/tal-tech/go-zero/core/logx"
)
const httpTimeout = time.Second * 5
var ErrWriteFailed = errors.New("submit failed")
type RemoteWriter struct {
endpoint string
}
func NewRemoteWriter(endpoint string) Writer {
return &RemoteWriter{
endpoint: endpoint,
}
}
func (rw *RemoteWriter) Write(report *StatReport) error {
bs, err := json.Marshal(report)
if err != nil {
return err
}
client := &http.Client{
Timeout: httpTimeout,
}
resp, err := client.Post(rw.endpoint, "application/json", bytes.NewBuffer(bs))
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logx.Errorf("write report failed, code: %d, reason: %s", resp.StatusCode, resp.Status)
return ErrWriteFailed
}
return nil
}
Go
1
https://gitee.com/hxchjm/go-zero.git
git@gitee.com:hxchjm/go-zero.git
hxchjm
go-zero
go-zero
3eb88c4e4bf1

搜索帮助

53164aa7 5694891 3bd8fe86 5694891