go.mod 管理项目,修复nsqd stats接口json解析失败问题

This commit is contained in:
liby71
2023-02-21 14:54:10 +08:00
parent 67bacbcfce
commit 3d86e6efe6
6 changed files with 530 additions and 9 deletions

View File

@ -1,7 +1,10 @@
package collector
import (
"bufio"
"encoding/json"
"io"
"log"
"net/http"
)
@ -85,15 +88,30 @@ func getPercentile(t *topic, percentile int) float64 {
}
func getNsqdStats(client *http.Client, nsqdURL string) (*stats, error) {
log.Print("Get nsqdStats addr ", nsqdURL)
resp, err := client.Get(nsqdURL)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var sr statsResponse
if err = json.NewDecoder(resp.Body).Decode(&sr); err != nil {
var sr stats // statsResponse
buf := bufio.NewReader(resp.Body)
fullBody := []byte{}
for {
line, _, err := buf.ReadLine()
if err == io.EOF {
break
}
fullBody = append(fullBody, line...)
}
err = json.Unmarshal(fullBody, &sr)
if err != nil {
return nil, err
}
return &sr.Data, nil
// body超过一定长度会出现json字符被截断的问题
//if err = json.NewDecoder(resp.Body).Decode(&sr); err != nil {
// return nil, err
//}
return &sr, nil
}