-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
45 lines (38 loc) · 961 Bytes
/
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
package main
import (
"fmt"
"io"
"log"
"net/http"
"os"
"strconv"
"time"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
appId := os.Getenv("APP_ID")
if len(appId) != 32 {
log.Fatal("APP_ID was not set or not 32 characters")
}
port, err := strconv.ParseUint(os.Getenv("PORT"), 10, 32)
if err != nil {
log.Printf("PORT defaulting to %d\n", 8080)
port = 8080
}
rootHandler := func(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "See /metrics for the prometheus metrics.\n")
}
http.HandleFunc("/", rootHandler)
http.Handle("/metrics", promhttp.Handler())
// TODO: make duration configurable.
errs := runCollector(appId, NewMetrics(), 1*time.Hour)
go func() {
for err := range errs {
log.Println(errors.Wrap(err, "error collecting"))
}
}()
addr := fmt.Sprintf(":%d", port)
log.Printf("Listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, nil))
}