forked from heroku/lumbermill
/
main.go
147 lines (125 loc) · 3.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"crypto/tls"
"fmt"
"log"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/heroku/slog"
influx "github.com/influxdb/influxdb-go"
)
const (
PointChannelCapacity = 100000
HashRingReplication = 20 // TODO: Needs to be determined
PostersPerHost = 6
)
const (
Router = iota
EventsRouter
DynoMem
DynoLoad
EventsDyno
numSeries
)
var (
connectionCloser = make(chan struct{})
posters = make([]*Poster, 0)
chanGroups = make([]*ChanGroup, 0)
seriesNames = []string{"router", "events.router", "dyno.mem", "dyno.load", "events.dyno"}
seriesColumns = [][]string{
[]string{"time", "id", "status", "service"}, // Router
[]string{"time", "id", "code"}, // EventsRouter
[]string{"time", "id", "source", "memory_cache", "memory_pgpgin", "memory_pgpgout", "memory_rss", "memory_swap", "memory_total", "dynoType"}, // DynoMem
[]string{"time", "id", "source", "load_avg_1m", "load_avg_5m", "load_avg_15m", "dynoType"}, // DynoLoad
[]string{"time", "id", "what", "type", "code", "message", "dynoType"}, // DynoEvents
}
hashRing = NewHashRing(HashRingReplication, nil)
Debug = os.Getenv("DEBUG") == "true"
User = os.Getenv("USER")
Password = os.Getenv("PASSWORD")
)
func LogWithContext(ctx slog.Context) {
ctx.Add("app", "lumbermill")
log.Println(ctx)
}
func createInfluxDBClient(host string) influx.ClientConfig {
return influx.ClientConfig{
Host: host, //"influxor.ssl.edward.herokudev.com:8086",
Username: os.Getenv("INFLUXDB_USER"), //"test",
Password: os.Getenv("INFLUXDB_PWD"), //"tester",
Database: os.Getenv("INFLUXDB_NAME"), //"ingress",
IsSecure: true,
HttpClient: &http.Client{
Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: os.Getenv("INFLUXDB_SKIP_VERIFY") == "true"},
ResponseHeaderTimeout: 5 * time.Second,
Dial: func(network, address string) (net.Conn, error) {
return net.DialTimeout(network, address, 5*time.Second)
},
},
},
}
}
func createClients(hostlist string) []influx.ClientConfig {
clients := make([]influx.ClientConfig, 0)
for _, host := range strings.Split(hostlist, ",") {
host = strings.Trim(host, "\t ")
if host != "" {
clients = append(clients, createInfluxDBClient(host))
}
}
return clients
}
// Health Checks, so just say 200 - OK
// TODO: Actual healthcheck
func serveHealth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func main() {
port := os.Getenv("PORT")
influxClients := createClients(os.Getenv("INFLUXDB_HOSTS"))
if len(influxClients) == 0 {
//No backends, so blackhole things
group := NewChanGroup("null", PointChannelCapacity)
chanGroups = append(chanGroups, group)
poster := NewNullPoster(group)
go poster.Run()
} else {
for i, client := range influxClients {
// TODO: make this the hostname, when we are actually sharding.
name := fmt.Sprintf("ringnode.%d", i)
group := NewChanGroup(name, PointChannelCapacity)
chanGroups = append(chanGroups, group)
for p := 0; p < PostersPerHost; p++ {
poster := NewPoster(client, name, group)
posters = append(posters, poster)
go poster.Run()
}
}
}
hashRing.Add(chanGroups...)
// Some statistics about the channels this way we can see how full they are getting
go func() {
for {
ctx := slog.Context{}
time.Sleep(10 * time.Second)
for _, group := range chanGroups {
group.Sample(ctx)
}
LogWithContext(ctx)
}
}()
// Every 5 minutes, signal that the connection should be closed
// This should allow for a slow balancing of connections.
go func() {
for {
time.Sleep(5 * time.Minute)
connectionCloser <- struct{}{}
}
}()
http.HandleFunc("/drain", serveDrain)
http.HandleFunc("/health", serveHealth)
log.Fatal(http.ListenAndServe(":"+port, nil))
}