-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
128 lines (105 loc) · 2.87 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"context"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
nlogrus "github.com/meatballhat/negroni-logrus"
"github.com/phyber/negroni-gzip/gzip"
prom "github.com/prometheus/client_golang/api"
promapi "github.com/prometheus/client_golang/api/prometheus/v1"
"github.com/sirupsen/logrus"
"github.com/unrolled/render"
nsecure "github.com/unrolled/secure"
"github.com/urfave/negroni"
)
func main() {
logger := logrus.New()
logger.SetFormatter(&logrus.JSONFormatter{})
logger.SetOutput(os.Stdout)
logger.SetLevel(logrus.InfoLevel)
promURL := flag.String("prometheus-url", "http://localhost:9090", "Promtheus URL")
promBasicAuth := flag.String("prometheus-basic-auth", "", "Optional basic auth used for prometheus")
promReadPath := flag.String("prometheus-read-path", "", "Optional path to modify URL for reading data")
flag.Parse()
if promURL == nil || *promURL == "" {
logger.Fatal("-prometheus-url flag is required")
}
httpc := NewHTTPClient()
promConfig := prom.Config{
Address: *promURL,
RoundTripper: &promRoundTripper{
client: httpc,
basicAuth: *promBasicAuth,
path: *promReadPath,
},
}
promClient, err := prom.NewClient(promConfig)
if err != nil {
logger.Fatalf("Could not create Prometheus API client: %s", err)
}
promAPI := promapi.NewAPI(promClient)
updatedAt := time.Now()
stats := []Stat{}
statsMu := sync.RWMutex{}
go func() {
for {
logger.Info("get-stats")
latestStats, err := chartAll(promAPI)
if err != nil {
logger.Errorf("Could not query Prometheus: %s", err)
time.Sleep(30 * time.Second)
continue
}
logger.Info("got-stats")
statsMu.Lock()
stats = latestStats
updatedAt = time.Now()
statsMu.Unlock()
time.Sleep(3 * time.Minute)
}
}()
renderer := render.New(render.Options{
Directory: "templates",
})
mux := http.NewServeMux()
mux.HandleFunc("GET /health", func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "healthy")
})
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
statsMu.RLock()
defer statsMu.RUnlock()
_ = renderer.HTML(
w, http.StatusOK,
"stats",
StatPage{
SecondsAgo: int64(time.Since(updatedAt).Seconds()),
Stats: stats,
},
)
})
level := logrus.InfoLevel
n := negroni.New()
n.Use(negroni.NewRecovery())
n.Use(nlogrus.NewCustomMiddleware(level, &logrus.JSONFormatter{}, "web"))
n.Use(gzip.Gzip(gzip.DefaultCompression))
n.Use(negroni.HandlerFunc(nsecure.New().HandlerFuncWithNext))
n.UseHandler(mux)
ctx, cancel := signal.NotifyContext(context.Background(), syscall.SIGTERM)
defer cancel()
server := &http.Server{Addr: ":8080", Handler: n}
go func() {
logger.Info("listening")
_ = server.ListenAndServe()
}()
<-ctx.Done()
ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
_ = server.Shutdown(ctx)
os.Exit(0)
}