Skip to content

Commit

Permalink
Merge pull request #24 from dabealu/resolve-every-request
Browse files Browse the repository at this point in the history
Resolve every request
  • Loading branch information
dabealu authored Mar 29, 2020
2 parents 523980f + 0e84896 commit b6bcbb4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 65 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ jobs:

- name: build
run: |
go build -v -o zookeeper-exporter
tar -czvf zookeeper-exporter-${{ steps.v.outputs.tag }}-${{ runner.os }}.tar.gz --transform 's,^,zookeeper-exporter-${{ steps.v.outputs.tag }}-${{ runner.os }}/,' zookeeper-exporter
for OS in linux darwin; do
echo "building binary for ${OS}"
GOOS=${OS} GOARCH=amd64 go build -v -o zookeeper-exporter
tar -czvf zookeeper-exporter-${{ steps.v.outputs.tag }}-${OS}.tar.gz --transform 's,^,zookeeper-exporter-${{ steps.v.outputs.tag }}-${OS}/,' zookeeper-exporter
done
ls -lh
- name: create release
id: create-release
Expand Down
112 changes: 49 additions & 63 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,58 @@ import (
"regexp"
)

type zkHost struct {
Unresolved string
*net.TCPAddr
func main() {
location := flag.String("location", "/metrics", "metrics location")
listen := flag.String("listen", "0.0.0.0:8080", "address to listen on")
timeout := flag.Int64("timeout", 30, "timeout for connection to zk servers, in seconds")
host := flag.String("zk-host", "127.0.0.1", "zookeeper host")
port := flag.String("zk-port", "2181", "zookeeper port")
list := flag.String("zk-list", "",
"comma separated list of zk servers, i.e. '10.0.0.1:2181,10.0.0.2:2181,10.0.0.3:2181', this flag overrides --zk-host/port")
flag.Parse()

hosts := []string{}
if *list == "" {
hosts = []string{fmt.Sprintf("%s:%s", *host, *port)}
} else {
hosts = strings.Split(*list, ",")
}

log.Printf("info: zookeeper addresses %v", hosts)

options := Options{
Timeout: *timeout,
Hosts: hosts,
}

serveMetrics(*location, *listen, &options)
}

type zkOptions struct {
type Options struct {
Timeout int64
Hosts []zkHost
Hosts []string
}

// open tcp connections to zk nodes, send 'mntr' and return result as a map
func getMetrics(zkOpts zkOptions) map[string]string {
func getMetrics(options *Options) map[string]string {
metrics := map[string]string{}

for _, h := range zkOpts.Hosts {
hostLabel := fmt.Sprintf("zk_host=%q", h.Unresolved)
for _, h := range options.Hosts {
tcpaddr, err := net.ResolveTCPAddr("tcp", h)
if err != nil {
log.Printf("warning: cannot resolve zk hostname '%s': %s", h, err)
continue
}

hostLabel := fmt.Sprintf("zk_host=%q", h)

// open connection
timeout := time.Duration(zkOpts.Timeout) * time.Second
d := net.Dialer{Timeout: timeout}
conn, err := d.Dial("tcp", h.TCPAddr.String())
timeout := time.Duration(options.Timeout) * time.Second
dialer := net.Dialer{Timeout: timeout}

conn, err := dialer.Dial("tcp", tcpaddr.String())
if err != nil {
log.Printf("warning: cannot connect to %s: %v", h.Unresolved, err)
log.Printf("warning: cannot connect to %s: %v", h, err)
metrics[fmt.Sprintf("zk_up{%s}", hostLabel)] = "0"
continue
}
Expand All @@ -43,13 +72,13 @@ func getMetrics(zkOpts zkOptions) map[string]string {

_, err = conn.Write([]byte("mntr"))
if err != nil {
log.Printf("warning: failed to send 'mntr' to '%s': %s", h.Unresolved, err)
log.Printf("warning: failed to send 'mntr' to '%s': %s", h, err)
}

// read response
res, err := ioutil.ReadAll(conn)
if err != nil {
log.Printf("warning: failed read 'mntr' response from '%s': %s", h.Unresolved, err)
log.Printf("warning: failed read 'mntr' response from '%s': %s", h, err)
}

// get slice of strings from response, like 'zk_avg_latency 0'
Expand Down Expand Up @@ -82,7 +111,7 @@ func getMetrics(zkOpts zkOptions) map[string]string {
} else {
metrics[zkLeader] = "0"
}

case "zk_version":
re := regexp.MustCompile(`^([0-9]+\.[0-9]+\.[0-9]+).*$`)
version := re.ReplaceAllString(kv[1], "$1")
Expand All @@ -91,7 +120,7 @@ func getMetrics(zkOpts zkOptions) map[string]string {

case "zk_peer_state":
metrics[fmt.Sprintf("zk_peer_state{%s,state=%q}", hostLabel, kv[1])] = "1"

case "": // noop on empty string

default:
Expand All @@ -106,59 +135,16 @@ func getMetrics(zkOpts zkOptions) map[string]string {
}

// serve zk metrics at chosen address and url
func serveMetrics(location, listen string, zkOpts zkOptions) {
h := func(w http.ResponseWriter, r *http.Request) {
for k, v := range getMetrics(zkOpts) {
func serveMetrics(location, listen string, options *Options) {
handler := func(w http.ResponseWriter, r *http.Request) {
for k, v := range getMetrics(options) {
fmt.Fprintf(w, "%s %s\n", k, v)
}
}

http.HandleFunc(location, h)
http.HandleFunc(location, handler)
log.Printf("info: serving metrics at %s%s", listen, location)
if err := http.ListenAndServe(listen, nil); err != nil {
log.Fatalf("fatal: shutting down exporter: %s", err)
}
}

func main() {
location := flag.String("location", "/metrics", "metrics location")
listen := flag.String("listen", "0.0.0.0:8080", "address to listen on")
timeout := flag.Int64("timeout", 120, "timeout for connection to zk servers, in seconds")
host := flag.String("zk-host", "127.0.0.1", "zookeeper host")
port := flag.String("zk-port", "2181", "zookeeper port")
list := flag.String("zk-list", "",
"comma separated list of zk servers, i.e. '10.0.0.1:2181,10.0.0.2:2181,10.0.0.3:2181', this flag overrides --zk-host/port")
flag.Parse()

Hosts := []zkHost{}

if *list == "" {
// single zk server
h := *host + ":" + *port
tcp, err := net.ResolveTCPAddr("tcp", h)
if err != nil {
log.Fatalf("fatal: cannot resolve zk hostname '%s': %s", h, err)
}

Hosts = append(Hosts, zkHost{Unresolved: h, TCPAddr: tcp})
} else {
// list of zk servers,
for _, h := range strings.Split(*list, ",") {
tcp, err := net.ResolveTCPAddr("tcp", h)
if err != nil {
log.Printf("warning: cannot resolve zk hostname '%s': %s", h, err)
continue
}

Hosts = append(Hosts, zkHost{Unresolved: h, TCPAddr: tcp})
}
}

log.Printf("info: zookeeper addresses %v", Hosts)

zkOpts := zkOptions{
Timeout: *timeout,
Hosts: Hosts,
}
serveMetrics(*location, *listen, zkOpts)
}

0 comments on commit b6bcbb4

Please sign in to comment.