forked from getlantern/lumbermill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (141 loc) · 4.44 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package main
import (
"crypto/tls"
"io"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
auth "github.com/heroku/lumbermill/Godeps/_workspace/src/github.com/heroku/authenticater"
influx "github.com/heroku/lumbermill/Godeps/_workspace/src/github.com/influxdb/influxdb-go"
metrics "github.com/heroku/lumbermill/Godeps/_workspace/src/github.com/rcrowley/go-metrics"
librato "github.com/heroku/lumbermill/Godeps/_workspace/src/github.com/rcrowley/go-metrics/librato"
)
type shutdownChan chan struct{}
type clientFunc func() *http.Client
const (
defaultClientTimeout = 20 * time.Second
pointChannelCapacity = 500000
hashRingReplication = 46
postersPerHost = 6
)
var (
connectionCloser = make(chan struct{})
debug = os.Getenv("DEBUG") == "true"
)
func (s shutdownChan) Close() error {
s <- struct{}{}
return nil
}
func createInfluxDBClient(host string, f clientFunc) 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: f(),
}
}
// Creates clients which deliver to InfluxDB
func createClients(hostlist string, f clientFunc) []influx.ClientConfig {
var clients []influx.ClientConfig
for _, host := range strings.Split(hostlist, ",") {
host = strings.Trim(host, "\t ")
if host != "" {
clients = append(clients, createInfluxDBClient(host, f))
}
}
return clients
}
// Creates destinations and attaches them to posters, which deliver to InfluxDB
func createMessageRoutes(hostlist string, f clientFunc) (*hashRing, []*destination, *sync.WaitGroup) {
var destinations []*destination
posterGroup := new(sync.WaitGroup)
hashRing := newHashRing(hashRingReplication, nil)
influxClients := createClients(hostlist, f)
if len(influxClients) == 0 {
//No backends, so blackhole things
destination := newDestination("null", pointChannelCapacity)
hashRing.Add(destination)
destinations = append(destinations, destination)
poster := newNullPoster(destination)
go poster.Run()
} else {
for _, client := range influxClients {
name := client.Host
destination := newDestination(name, pointChannelCapacity)
hashRing.Add(destination)
destinations = append(destinations, destination)
for p := 0; p < postersPerHost; p++ {
poster := newPoster(client, name, destination, posterGroup)
posterGroup.Add(1)
go func() {
poster.Run()
posterGroup.Done()
}()
}
}
}
return hashRing, destinations, posterGroup
}
func awaitSignals(ss ...io.Closer) {
sigCh := make(chan os.Signal)
signal.Notify(sigCh, syscall.SIGTERM, syscall.SIGINT)
sig := <-sigCh
log.Printf("Got signal: %q", sig)
for _, s := range ss {
s.Close()
}
}
func awaitShutdown(shutdownChan shutdownChan, server *server, posterGroup *sync.WaitGroup) {
<-shutdownChan
log.Printf("waiting for inflight requests to finish.")
server.Wait()
posterGroup.Wait()
log.Printf("Shutdown complete.")
}
func newClientFunc() *http.Client {
return &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: os.Getenv("INFLUXDB_SKIP_VERIFY") == "true"},
},
Timeout: defaultClientTimeout,
}
}
func main() {
hashRing, destinations, posterGroup := createMessageRoutes(os.Getenv("INFLUXDB_HOSTS"), newClientFunc)
if os.Getenv("LIBRATO_TOKEN") != "" {
go librato.Librato(
metrics.DefaultRegistry,
20*time.Second,
os.Getenv("LIBRATO_OWNER"),
os.Getenv("LIBRATO_TOKEN"),
os.Getenv("LIBRATO_SOURCE"),
[]float64{0.50, 0.95, 0.99},
time.Millisecond,
)
} else if os.Getenv("DEBUG") == "true" {
go metrics.Log(metrics.DefaultRegistry, 20e9, log.New(os.Stderr, "metrics: ", log.Lmicroseconds))
}
basicAuther, err := auth.NewBasicAuthFromString(os.Getenv("CRED_STORE"))
if err != nil {
log.Fatalf("Unable to parse credentials from CRED_STORE=%q: err=%q", os.Getenv("CRED_STORE"), err)
}
shutdownChan := make(shutdownChan)
server := newServer(&http.Server{Addr: ":" + os.Getenv("PORT")}, basicAuther, hashRing)
log.Printf("Starting up")
go server.Run(5 * time.Minute)
var closers []io.Closer
closers = append(closers, server)
closers = append(closers, shutdownChan)
for _, cls := range destinations {
closers = append(closers, cls)
}
go awaitSignals(closers...)
awaitShutdown(shutdownChan, server, posterGroup)
}