added metrics url

This commit is contained in:
Stephen McQuay 2016-09-27 21:37:17 -07:00
parent b02af78e21
commit 1e14fe566c
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D
3 changed files with 91 additions and 0 deletions

View File

@ -5,9 +5,12 @@ import (
"fmt"
"log"
"math/rand"
"os"
"regexp"
"time"
"mcquay.me/exmo"
irc "github.com/fluffle/goirc/client"
)
@ -15,6 +18,16 @@ var host = flag.String("host", "localhost:6668", "irc server hostname")
var channel = flag.String("channel", "#exmormon", "irc channel")
func main() {
maddr := os.Getenv("ADDR")
if maddr == "" {
fmt.Fprintf(os.Stderr, "failed to set ADDR\n")
os.Exit(1)
}
if err := exmo.Metrics(maddr); err != nil {
fmt.Fprintf(os.Stderr, "can't http: %v\n", err)
os.Exit(1)
}
rand.Seed(time.Now().UnixNano())
flag.Parse()
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

View File

@ -7,9 +7,12 @@ import (
"log"
"math/rand"
"net/http"
"os"
"regexp"
"time"
"mcquay.me/exmo"
irc "github.com/fluffle/goirc/client"
)
@ -18,6 +21,16 @@ var channel = flag.String("channel", "#exmormon", "irc channel")
var top = flag.Int("top", 5, "number of top posts to use")
func main() {
maddr := os.Getenv("ADDR")
if maddr == "" {
fmt.Fprintf(os.Stderr, "failed to set ADDR\n")
os.Exit(1)
}
if err := exmo.Metrics(maddr); err != nil {
fmt.Fprintf(os.Stderr, "can't http: %v\n", err)
os.Exit(1)
}
rand.Seed(time.Now().UnixNano())
flag.Parse()
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile)

65
exmo.go Normal file
View File

@ -0,0 +1,65 @@
package exmo
import (
"errors"
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
)
func Metrics(addr string) error {
http.Handle("/metrics", prometheus.Handler())
h, p, err := net.SplitHostPort(addr)
if err != nil {
return err
}
var u string
if h != "" {
u = fmt.Sprintf("http://%v:%v/metrics", h, p)
} else {
u = fmt.Sprintf("http://localhost:%v/metrics", p)
}
e := make(chan error)
go func() {
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Printf("%+v", err)
}
e <- err
}()
go func() {
t := 100 * time.Millisecond
for {
time.Sleep(t)
if t < time.Minute {
t = t * 2
}
if t > 5*time.Second {
e <- errors.New("timeout waiting for metrics server to start")
}
resp, err := http.Get(u)
if err != nil {
log.Printf("%+v", err)
continue
}
io.Copy(ioutil.Discard, resp.Body)
if resp.StatusCode != http.StatusOK {
log.Printf("%+v", resp.Status)
} else {
e <- nil
return
}
}
}()
return <-e
}