Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复解析nsqd/stats 接口json被截断问题 #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
build/
vendor/
.idea/
9 changes: 8 additions & 1 deletion Makefile → cmd/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BUILD_DIR = build
BUILD_DIR = ../build
BINARY_NAME=nsq_exporter

GO = go
GOX = gox
Expand All @@ -8,6 +9,12 @@ GOX_ARGS = -output="$(BUILD_DIR)/{{.Dir}}_{{.OS}}_{{.Arch}}" -osarch="linux/amd6
build:
$(GO) build -o $(BUILD_DIR)/nsq_exporter .

debug:
GOOS=linux GOARCH=amd64 go build -gcflags="all=-N -l" -o ${BINARY_NAME}_dlv main.go

dlv:
dlv --listen=:10499 --headless=true --api-version=2 --accept-multiclient --log exec ./${BINARY_NAME}_dlv -- -nsqd.addr=http://10.16.48.65:4151/stats

.PHONY: deps-init deps-get
deps-init:
@go get -u github.com/kardianos/govendor
Expand Down
9 changes: 4 additions & 5 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package main
import (
"flag"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/seiferli/nsq_exporter/collector"
"log"
"net/http"
"net/url"
"strings"

"github.com/lovoo/nsq_exporter/collector"

"github.com/prometheus/client_golang/prometheus"
)

// Version of nsq_exporter. Set at build time.
Expand Down Expand Up @@ -42,7 +41,7 @@ func main() {
}
prometheus.MustRegister(ex)

http.Handle(*metricsPath, prometheus.Handler())
http.Handle(*metricsPath, promhttp.Handler())
if *metricsPath != "" && *metricsPath != "/" {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`<html>
Expand Down
24 changes: 21 additions & 3 deletions collector/stats.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package collector

import (
"bufio"
"encoding/json"
"io"
"log"
"net/http"
)

Expand Down Expand Up @@ -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
}
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/seiferli/nsq_exporter

go 1.18

require github.com/prometheus/client_golang v1.14.0

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
478 changes: 478 additions & 0 deletions go.sum

Large diffs are not rendered by default.

20 changes: 0 additions & 20 deletions vendor/github.com/beorn7/perks/LICENSE

This file was deleted.

Loading