-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwelcome_runner.go
63 lines (53 loc) · 1.32 KB
/
welcome_runner.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
"net/http"
"time"
log "github.com/sirupsen/logrus"
)
func runWelcomePage() {
var (
baseURL = cfg.BaseURL
welcomeAPIToken = cfg.APIToken
)
for tick := time.NewTicker(10 * time.Minute); ; <-tick.C {
postWelcomeMetric(baseURL, welcomeAPIToken)
}
}
func postWelcomeMetric(baseURL, welcomeAPIToken string) {
beers := rand.Intn(24)
status := "OK"
switch {
case beers < 6:
status = "Critical"
case beers < 12:
status = "Warning"
}
beer := dashboardMetric{
Title: "Amount of beer in the fridge",
Description: fmt.Sprintf("Currently there are %d bottles of beer in the fridge", beers),
IgnoreMAD: true,
Status: status,
Expires: 86400,
Freshness: 900,
Value: float64(beers),
}
body := new(bytes.Buffer)
if err := json.NewEncoder(body).Encode(beer); err != nil {
log.WithError(err).Error("Unable to marshal dashboard metric")
return
}
url := fmt.Sprintf("%s/welcome/beer_available", baseURL)
req, _ := http.NewRequest(http.MethodPut, url, body)
req.Header.Add("Authorization", welcomeAPIToken)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.WithError(err).Error("Unable to put welcome-runner metric")
return
}
resp.Body.Close()
log.Debug("Successfully put welcome-runner metric")
}