Skip to content
New issue

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

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

Already on GitHub? # to your account

Get servers from prometheus #7

Open
wants to merge 4 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
zookeeper_exporter
*.tar.gz
*-stamp
.idea/
41 changes: 35 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ import (

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

type Servers struct {
Servers []string `json:"servers"`
Port int `json:"port"`
}

const concurrentFetch = 100

// Commandline flags.
var (
useExhibitor = flag.Bool("exporter.use_exhibitor", false, "Use Exhibitor to discover ZooKeeper servers")
Expand All @@ -33,6 +32,7 @@ var (

var (
variableLabels = []string{"server"}
versionLabels = []string{"server", "version"}
)

var httpClient = http.Client{
Expand Down Expand Up @@ -158,7 +158,12 @@ func (e *exporter) pollServer(server string, ch chan<- prometheus.Metric, wg *sy

conn, err := net.Dial("tcp", server)
if err != nil {
e.recordErr(err)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
"zk_up",
"zk_up",
variableLabels, nil,
), prometheus.GaugeValue, 0, server)
return
}

Expand All @@ -175,7 +180,18 @@ func (e *exporter) pollServer(server string, ch chan<- prometheus.Metric, wg *sy

switch key {
case "zk_version":
continue
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
"zk_version",
"zk_version",
versionLabels, nil,
), prometheus.GaugeValue, 1, server, value)
ch <- prometheus.MustNewConstMetric(
prometheus.NewDesc(
"zk_up",
"zk_up",
variableLabels, nil,
), prometheus.GaugeValue, 1, server)
case "zk_server_state":
log.Debugf("%s: %d", key+"_"+value, 1)
ch <- prometheus.MustNewConstMetric(
Expand All @@ -197,12 +213,25 @@ func (e *exporter) pollServer(server string, ch chan<- prometheus.Metric, wg *sy
}
}

func exporterHandler(w http.ResponseWriter, r *http.Request, exporter *exporter) {
params := r.URL.Query()
if len(params["servers"]) != 0 {
exporter.addrs = params["servers"]
}
registry := prometheus.NewRegistry()
registry.MustRegister(exporter)
h := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
h.ServeHTTP(w, r)
}

func main() {
flag.Parse()
exporter := newZooKeeperExporter(flag.Args(), *useExhibitor)
prometheus.MustRegister(exporter)

http.Handle(*metricPath, prometheus.Handler())
http.HandleFunc(*metricPath, func(w http.ResponseWriter, r *http.Request) {
exporterHandler(w, r, exporter)
})

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, *metricPath, http.StatusMovedPermanently)
})
Expand Down