forked from janeprather/xapi_exporter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
51 lines (38 loc) · 1.13 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/prometheus/client_golang/prometheus"
)
const appTitle = "XAPI Data Exporter"
const httpIndex = "<html><head><title>%s</title></head>" +
"<body><h1>%s</h1><a href=\"%s\">View Metrics</a>" +
"</body></html>"
const metricsPath = "/metrics"
func main() {
var err error
// parse commandline flags
flag.Parse()
// load configuration
initConfig()
// instantiate exporter object
exporter := newExporter()
// register the exporter with prom package
prometheus.MustRegister(exporter)
// register the prometheus handler with http service
http.Handle(metricsPath, promhttp.Handler())
// unless the metricsPath is /, offer a minimal page with a link at /
if len(metricsPath) > 0 && metricsPath != "/" {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(fmt.Sprintf(httpIndex, appTitle, appTitle, metricsPath)))
})
}
log.Printf("Starting HTTP service on %s\n", config.BindAddress)
err = http.ListenAndServe(config.BindAddress, nil)
if err != nil {
log.Fatal(err)
}
}