go.mod 管理项目,修复nsqd stats接口json解析失败问题
This commit is contained in:
37
cmd/Makefile
Normal file
37
cmd/Makefile
Normal file
@ -0,0 +1,37 @@
|
||||
BUILD_DIR = ../build
|
||||
BINARY_NAME=nsq_exporter
|
||||
|
||||
GO = go
|
||||
GOX = gox
|
||||
GOX_ARGS = -output="$(BUILD_DIR)/{{.Dir}}_{{.OS}}_{{.Arch}}" -osarch="linux/amd64 linux/386 linux/arm linux/arm64 darwin/amd64 freebsd/amd64 freebsd/386 windows/386 windows/amd64"
|
||||
|
||||
.PHONY: build
|
||||
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
|
||||
$(GOPATH)/bin/govendor init
|
||||
|
||||
deps-get: deps-init
|
||||
@$(GOPATH)/bin/govendor get github.com/lovoo/nsq_exporter
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -R $(BUILD_DIR)/* || true
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
$(GO) test ./...
|
||||
|
||||
.PHONY: release-build
|
||||
release-build:
|
||||
@go get -u github.com/mitchellh/gox
|
||||
@$(GOX) $(GOX_ARGS) github.com/lovoo/nsq_exporter
|
109
cmd/main.go
Normal file
109
cmd/main.go
Normal file
@ -0,0 +1,109 @@
|
||||
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"
|
||||
)
|
||||
|
||||
// Version of nsq_exporter. Set at build time.
|
||||
const Version = "0.0.0.dev"
|
||||
|
||||
var (
|
||||
listenAddress = flag.String("web.listen", ":9117", "Address on which to expose metrics and web interface.")
|
||||
metricsPath = flag.String("web.path", "/metrics", "Path under which to expose metrics.")
|
||||
nsqdURL = flag.String("nsqd.addr", "http://localhost:4151/stats", "Address of the nsqd node.")
|
||||
enabledCollectors = flag.String("collect", "stats.topics,stats.channels", "Comma-separated list of collectors to use.")
|
||||
namespace = flag.String("namespace", "nsq", "Namespace for the NSQ metrics.")
|
||||
tlsCACert = flag.String("tls.ca_cert", "", "CA certificate file to be used for nsqd connections.")
|
||||
tlsCert = flag.String("tls.cert", "", "TLS certificate file to be used for client connections to nsqd.")
|
||||
tlsKey = flag.String("tls.key", "", "TLS key file to be used for TLS client connections to nsqd.")
|
||||
|
||||
statsRegistry = map[string]func(namespace string) collector.StatsCollector{
|
||||
"topics": collector.TopicStats,
|
||||
"channels": collector.ChannelStats,
|
||||
"clients": collector.ClientStats,
|
||||
}
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
|
||||
ex, err := createNsqExecutor()
|
||||
if err != nil {
|
||||
log.Fatalf("error creating nsq executor: %v", err)
|
||||
}
|
||||
prometheus.MustRegister(ex)
|
||||
|
||||
http.Handle(*metricsPath, promhttp.Handler())
|
||||
if *metricsPath != "" && *metricsPath != "/" {
|
||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Write([]byte(`<html>
|
||||
<head><title>NSQ Exporter</title></head>
|
||||
<body>
|
||||
<h1>NSQ Exporter</h1>
|
||||
<p><a href="` + *metricsPath + `">Metrics</a></p>
|
||||
</body>
|
||||
</html>`))
|
||||
})
|
||||
}
|
||||
|
||||
log.Print("listening to ", *listenAddress)
|
||||
err = http.ListenAndServe(*listenAddress, nil)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func createNsqExecutor() (*collector.NsqExecutor, error) {
|
||||
nsqdURL, err := normalizeURL(*nsqdURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
ex, err := collector.NewNsqExecutor(*namespace, nsqdURL, *tlsCACert, *tlsCert, *tlsKey)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
for _, param := range strings.Split(*enabledCollectors, ",") {
|
||||
param = strings.TrimSpace(param)
|
||||
parts := strings.SplitN(param, ".", 2)
|
||||
if len(parts) != 2 {
|
||||
return nil, fmt.Errorf("invalid collector name: %s", param)
|
||||
}
|
||||
if parts[0] != "stats" {
|
||||
return nil, fmt.Errorf("invalid collector prefix: %s", parts[0])
|
||||
}
|
||||
|
||||
name := parts[1]
|
||||
c, has := statsRegistry[name]
|
||||
if !has {
|
||||
return nil, fmt.Errorf("unknown stats collector: %s", name)
|
||||
}
|
||||
ex.Use(c(*namespace))
|
||||
}
|
||||
return ex, nil
|
||||
}
|
||||
|
||||
func normalizeURL(ustr string) (string, error) {
|
||||
ustr = strings.ToLower(ustr)
|
||||
if !strings.HasPrefix(ustr, "https://") && !strings.HasPrefix(ustr, "http://") {
|
||||
ustr = "http://" + ustr
|
||||
}
|
||||
|
||||
u, err := url.Parse(ustr)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if u.Path == "" {
|
||||
u.Path = "/stats"
|
||||
}
|
||||
u.RawQuery = "format=json"
|
||||
return u.String(), nil
|
||||
}
|
Reference in New Issue
Block a user