forked from stripe/veneur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
343 lines (287 loc) · 11 KB
/
proxy.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package veneur
import (
"fmt"
"net/http"
"net/http/pprof"
"os"
"strconv"
"sync"
"syscall"
"time"
"golang.org/x/net/context"
"github.com/DataDog/datadog-go/statsd"
"github.com/Sirupsen/logrus"
raven "github.com/getsentry/raven-go"
"github.com/hashicorp/consul/api"
"github.com/pkg/profile"
"github.com/stripe/veneur/samplers"
"github.com/stripe/veneur/trace"
"github.com/zenazn/goji/bind"
"github.com/zenazn/goji/graceful"
"stathat.com/c/consistent"
"goji.io"
"goji.io/pat"
)
type Proxy struct {
Sentry *raven.Client
Hostname string
ForwardDestinations *consistent.Consistent
TraceDestinations *consistent.Consistent
Discoverer Discoverer
ConsulForwardService string
ConsulTraceService string
ConsulInterval time.Duration
ForwardDestinationsMtx sync.Mutex
TraceDestinationsMtx sync.Mutex
HTTPAddr string
HTTPClient *http.Client
Statsd *statsd.Client
enableProfiling bool
}
func NewProxyFromConfig(conf ProxyConfig) (p Proxy, err error) {
hostname, err := os.Hostname()
if err != nil {
log.WithError(err).Error("Error finding hostname")
return
}
p.Hostname = hostname
log.Hooks.Add(sentryHook{
c: p.Sentry,
hostname: hostname,
lv: []logrus.Level{
logrus.ErrorLevel,
logrus.FatalLevel,
logrus.PanicLevel,
},
})
p.HTTPAddr = conf.HTTPAddress
// TODO Timeout?
p.HTTPClient = &http.Client{}
p.Statsd, err = statsd.NewBuffered(conf.StatsAddress, 1024)
if err != nil {
log.WithError(err).Error("Failed to create stats instance")
return
}
p.Statsd.Namespace = "veneur_proxy."
p.enableProfiling = conf.EnableProfiling
p.ConsulForwardService = conf.ConsulForwardServiceName
p.ConsulTraceService = conf.ConsulTraceServiceName
p.ForwardDestinations = consistent.New()
p.TraceDestinations = consistent.New()
p.ConsulInterval, err = time.ParseDuration(conf.ConsulRefreshInterval)
if err != nil {
log.WithError(err).Error("Error parsing Consul refresh interval")
return
}
log.WithField("interval", conf.ConsulRefreshInterval).Info("Will use Consul for service discovery")
// TODO Size of replicas in config?
//ret.ForwardDestinations.NumberOfReplicas = ???
return
}
// Start fires up the various goroutines that run on behalf of the server.
// This is separated from the constructor for testing convenience.
func (p *Proxy) Start() {
log.WithField("version", VERSION).Info("Starting server")
config := api.DefaultConfig()
// Use the same HTTP Client we're using for other things, so we can leverage
// it for testing.
config.HttpClient = p.HTTPClient
disc, consulErr := NewConsul(config)
if consulErr != nil {
log.WithError(consulErr).Error("Error creating Consul discoverer")
return
}
p.Discoverer = disc
p.RefreshDestinations(p.ConsulForwardService, p.ForwardDestinations, &p.ForwardDestinationsMtx)
if len(p.ForwardDestinations.Members()) == 0 {
log.WithField("serviceName", p.ConsulForwardService).Error("Refusing to start with zero destinations for forwarding.")
}
// Else use Consul
p.RefreshDestinations(p.ConsulTraceService, p.TraceDestinations, &p.TraceDestinationsMtx)
if len(p.ForwardDestinations.Members()) == 0 {
log.WithField("serviceName", p.ConsulTraceService).Error("Refusing to start with zero destinations for tracing.")
}
go func() {
defer func() {
ConsumePanic(p.Sentry, p.Statsd, p.Hostname, recover())
}()
ticker := time.NewTicker(p.ConsulInterval)
for range ticker.C {
if p.ConsulForwardService != "" {
p.RefreshDestinations(p.ConsulForwardService, p.ForwardDestinations, &p.ForwardDestinationsMtx)
}
if p.ConsulTraceService != "" {
p.RefreshDestinations(p.ConsulTraceService, p.TraceDestinations, &p.TraceDestinationsMtx)
}
}
}()
}
// HTTPServe starts the HTTP server and listens perpetually until it encounters an unrecoverable error.
func (p *Proxy) HTTPServe() {
var prf interface {
Stop()
}
// We want to make sure the profile is stopped
// exactly once (and only once), even if the
// shutdown pre-hook does not run (which it may not)
profileStopOnce := sync.Once{}
if p.enableProfiling {
profileStartOnce.Do(func() {
prf = profile.Start()
})
defer func() {
profileStopOnce.Do(prf.Stop)
}()
}
httpSocket := bind.Socket(p.HTTPAddr)
graceful.Timeout(10 * time.Second)
graceful.PreHook(func() {
if prf != nil {
profileStopOnce.Do(prf.Stop)
}
log.Info("Terminating HTTP listener")
})
// Ensure that the server responds to SIGUSR2 even
// when *not* running under einhorn.
graceful.AddSignal(syscall.SIGUSR2, syscall.SIGHUP)
graceful.HandleSignals()
log.WithField("address", p.HTTPAddr).Info("HTTP server listening")
bind.Ready()
if err := graceful.Serve(httpSocket, p.Handler()); err != nil {
log.WithError(err).Error("HTTP server shut down due to error")
}
graceful.Shutdown()
}
// RefreshDestinations updates the server's list of valid destinations
// for flushing. This should be called periodically to ensure we have
// the latest data.
func (p *Proxy) RefreshDestinations(serviceName string, ring *consistent.Consistent, mtx *sync.Mutex) {
start := time.Now()
destinations, err := p.Discoverer.GetDestinationsForService(serviceName)
p.Statsd.TimeInMilliseconds("discoverer.update_duration_ns", float64(time.Since(start).Nanoseconds()), []string{fmt.Sprintf("service:%s", serviceName)}, 1.0)
if err != nil || len(destinations) == 0 {
log.WithError(err).Error("Discoverer returned an error, destinations may be stale!")
p.Statsd.Incr("discoverer.errors", []string{fmt.Sprintf("service:%s", serviceName)}, 1.0)
// Return since we got no hosts. We don't want to zero out the list. This
// should result in us leaving the "last good" values in the ring.
return
}
// At the last moment, lock the mutex and defer so we unlock after setting.
// We do this after we've fetched info so we don't hold the lock during long
// queries, timeouts or errors. The flusher can lock the mutex and prevent us
// from updating at the same time.
mtx.Lock()
defer mtx.Unlock()
ring.Set(destinations)
p.Statsd.Gauge("discoverer.destination_number", float64(len(destinations)), []string{fmt.Sprintf("service:%s", serviceName)}, 1.0)
}
// Handler returns the Handler responsible for routing request processing.
func (p *Proxy) Handler() http.Handler {
mux := goji.NewMux()
mux.HandleFuncC(pat.Get("/healthcheck"), func(c context.Context, w http.ResponseWriter, r *http.Request) {
w.Write([]byte("ok\n"))
})
mux.Handle(pat.Post("/import"), handleProxy(p))
mux.Handle(pat.Get("/debug/pprof/cmdline"), http.HandlerFunc(pprof.Cmdline))
mux.Handle(pat.Get("/debug/pprof/profile"), http.HandlerFunc(pprof.Profile))
mux.Handle(pat.Get("/debug/pprof/symbol"), http.HandlerFunc(pprof.Symbol))
mux.Handle(pat.Get("/debug/pprof/trace"), http.HandlerFunc(pprof.Trace))
// TODO match without trailing slash as well
mux.Handle(pat.Get("/debug/pprof/*"), http.HandlerFunc(pprof.Index))
return mux
}
func (p *Proxy) ProxyTraces(ctx context.Context, traces []DatadogTraceSpan) {
span, _ := trace.StartSpanFromContext(ctx, "veneur.opentracing.proxy.proxy_traces")
defer span.Finish()
tracesByDestination := make(map[string][]*DatadogTraceSpan)
for _, h := range p.TraceDestinations.Members() {
tracesByDestination[h] = make([]*DatadogTraceSpan, 0)
}
for _, t := range traces {
dest, _ := p.TraceDestinations.Get(strconv.FormatInt(t.TraceID, 10))
tracesByDestination[dest] = append(tracesByDestination[dest], &t)
}
for dest, batch := range tracesByDestination {
if len(batch) != 0 {
dnsStart := time.Now()
endpoint, err := resolveEndpoint(fmt.Sprintf("%s/spans", dest))
if err != nil {
// not a fatal error if we fail
// we'll just try to use the host as it was given to us
p.Statsd.Count("spans.error_total", 1, []string{"cause:dns"}, 1.0)
log.WithError(err).Warn("Could not re-resolve host for spans")
}
p.Statsd.TimeInMilliseconds("spans.duration_ns", float64(time.Since(dnsStart).Nanoseconds()), []string{"part:dns"}, 1.0)
// this endpoint is not documented to take an array... but it does
// another curious constraint of this endpoint is that it does not
// support "Content-Encoding: deflate"
err = postHelper(span.Attach(ctx), p.HTTPClient, p.Statsd, endpoint, batch, "flush_traces", false)
if err == nil {
log.WithFields(logrus.Fields{
"traces": len(batch),
"destination": dest,
}).Info("Completed flushing traces to Datadog")
} else {
log.WithFields(
logrus.Fields{
"traces": len(batch),
logrus.ErrorKey: err}).Error("Error flushing traces to Datadog")
}
} else {
log.WithField("destination", dest).Info("No traces to flush, skipping.")
}
}
}
// ProxyMetrics takes a sliceof JSONMetrics and breaks them up into multiple
// HTTP requests by MetricKey using the hash ring.
func (p *Proxy) ProxyMetrics(ctx context.Context, jsonMetrics []samplers.JSONMetric) {
span, _ := trace.StartSpanFromContext(ctx, "veneur.opentracing.proxy.proxy_metrics")
defer span.Finish()
jsonMetricsByDestination := make(map[string][]samplers.JSONMetric)
for _, h := range p.ForwardDestinations.Members() {
jsonMetricsByDestination[h] = make([]samplers.JSONMetric, 0)
}
for _, jm := range jsonMetrics {
dest, _ := p.ForwardDestinations.Get(jm.MetricKey.String())
jsonMetricsByDestination[dest] = append(jsonMetricsByDestination[dest], jm)
}
// nb The response has already been returned at this point, because we
wg := sync.WaitGroup{}
wg.Add(len(jsonMetricsByDestination)) // Make our waitgroup the size of our destinations
for dest, batch := range jsonMetricsByDestination {
go p.doPost(&wg, dest, batch)
}
wg.Wait() // Wait for all the above goroutines to complete
p.Statsd.TimeInMilliseconds("proxy.duration_ns", float64(time.Since(span.Start).Nanoseconds()), nil, 1.0)
}
func (p *Proxy) doPost(wg *sync.WaitGroup, destination string, batch []samplers.JSONMetric) {
defer wg.Done()
batchSize := len(batch)
if batchSize < 1 {
return
}
// always re-resolve the host to avoid dns caching
log.WithField("destination", destination).Debug("Beginning flush forward")
endpoint, err := resolveEndpoint(fmt.Sprintf("%s/import", destination))
if err != nil {
// not a fatal error if we fail
// we'll just try to use the host as it was given to us
p.Statsd.Count("forward.error_total", 1, []string{"cause:dns"}, 1.0)
log.WithError(err).Warn("Could not re-resolve host for forward")
return
}
err = postHelper(context.TODO(), p.HTTPClient, p.Statsd, endpoint, batch, "forward", true)
if err == nil {
log.WithField("metrics", batchSize).Info("Completed forward to upstream Veneur")
} else {
p.Statsd.Count("forward.error_total", 1, []string{"cause:post"}, 1.0)
log.WithError(err).Warn("Failed to POST metrics to destination")
}
p.Statsd.Gauge("metrics_by_destination", float64(batchSize), []string{fmt.Sprintf("destination:%s", destination)}, 1.0)
}
// Shutdown signals the server to shut down after closing all
// current connections.
func (p *Proxy) Shutdown() {
log.Info("Shutting down server gracefully")
graceful.Shutdown()
}