forked from moznion/resque_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (57 loc) · 1.22 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package resqueExporter
import (
"fmt"
"log"
"net/http"
"os"
"github.com/mkideal/cli"
"github.com/prometheus/client_golang/prometheus"
)
var (
rev string
ver string
)
type opt struct {
Help bool `cli:"h,help" usage:"display help"`
Version bool `cli:"v,version" usage:"display version and revision"`
Port int `cli:"p,port" usage:"set port number" dft:"9102"`
}
func Run(args []string) {
var port int
cli.Run(&opt{}, func(ctx *cli.Context) error {
argv := ctx.Argv().(*opt)
if argv.Help {
ctx.String(ctx.Usage())
os.Exit(0)
}
if argv.Version {
ctx.String(fmt.Sprintf("%s/%s\n", ver, rev))
os.Exit(0)
}
port = argv.Port
return nil
})
config, err := loadConfig()
if err != nil {
log.Fatal(err)
}
exporter, err := newExporter(config)
if err != nil {
log.Fatal(err)
}
prometheus.MustRegister(exporter)
http.Handle("/metrics", prometheus.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`
<html>
<body>
<h3>Resque Exporter v` + ver + `/` + rev + `</h3>
<p><a href='/metrics'>Metrics</a></p>
</body>
</html>
`))
})
addr := fmt.Sprintf(":%d", port)
log.Print("Listening 0.0.0.0", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}