This repository has been archived by the owner on Aug 30, 2019. It is now read-only.
/
api.go
425 lines (357 loc) · 12.3 KB
/
api.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
package api
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"sort"
"strings"
"sync/atomic"
"time"
log "github.com/cihub/seelog"
"github.com/tinylib/msgp/msgp"
"github.com/DataDog/datadog-trace-agent/internal/agent"
"github.com/DataDog/datadog-trace-agent/internal/config"
"github.com/DataDog/datadog-trace-agent/internal/info"
"github.com/DataDog/datadog-trace-agent/internal/metrics"
"github.com/DataDog/datadog-trace-agent/internal/osutil"
"github.com/DataDog/datadog-trace-agent/internal/pb"
"github.com/DataDog/datadog-trace-agent/internal/sampler"
"github.com/DataDog/datadog-trace-agent/internal/watchdog"
)
const (
maxRequestBodyLength = 10 * 1024 * 1024
tagTraceHandler = "handler:traces"
tagServiceHandler = "handler:services"
)
// Version is a dumb way to version our collector handlers
type Version string
const (
// v01 DEPRECATED, FIXME[1.x]
// Traces: JSON, slice of spans
// Services: JSON, map[string]map[string][string]
v01 Version = "v0.1"
// v02 DEPRECATED, FIXME[1.x]
// Traces: JSON, slice of traces
// Services: JSON, map[string]map[string][string]
v02 Version = "v0.2"
// v03
// Traces: msgpack/JSON (Content-Type) slice of traces
// Services: msgpack/JSON, map[string]map[string][string]
v03 Version = "v0.3"
// v04
// Traces: msgpack/JSON (Content-Type) slice of traces + returns service sampling ratios
// Services: msgpack/JSON, map[string]map[string][string]
v04 Version = "v0.4"
)
// HTTPReceiver is a collector that uses HTTP protocol and just holds
// a chan where the spans received are sent one by one
type HTTPReceiver struct {
Stats *info.ReceiverStats
PreSampler *sampler.PreSampler
Out chan pb.Trace
services chan pb.ServicesMetadata
conf *config.AgentConfig
dynConf *sampler.DynamicConfig
server *http.Server
maxRequestBodyLength int64
debug bool
}
// NewHTTPReceiver returns a pointer to a new HTTPReceiver
func NewHTTPReceiver(
conf *config.AgentConfig, dynConf *sampler.DynamicConfig, out chan pb.Trace, services chan pb.ServicesMetadata,
) *HTTPReceiver {
// use buffered channels so that handlers are not waiting on downstream processing
return &HTTPReceiver{
Stats: info.NewReceiverStats(),
PreSampler: sampler.NewPreSampler(),
Out: out,
conf: conf,
dynConf: dynConf,
services: services,
maxRequestBodyLength: maxRequestBodyLength,
debug: strings.ToLower(conf.LogLevel) == "debug",
}
}
// Run starts doing the HTTP server and is ready to receive traces
func (r *HTTPReceiver) Run() {
// FIXME[1.x]: remove all those legacy endpoints + code that goes with it
http.HandleFunc("/spans", r.httpHandleWithVersion(v01, r.handleTraces))
http.HandleFunc("/services", r.httpHandleWithVersion(v01, r.handleServices))
http.HandleFunc("/v0.1/spans", r.httpHandleWithVersion(v01, r.handleTraces))
http.HandleFunc("/v0.1/services", r.httpHandleWithVersion(v01, r.handleServices))
http.HandleFunc("/v0.2/traces", r.httpHandleWithVersion(v02, r.handleTraces))
http.HandleFunc("/v0.2/services", r.httpHandleWithVersion(v02, r.handleServices))
http.HandleFunc("/v0.3/traces", r.httpHandleWithVersion(v03, r.handleTraces))
http.HandleFunc("/v0.3/services", r.httpHandleWithVersion(v03, r.handleServices))
// current collector API
http.HandleFunc("/v0.4/traces", r.httpHandleWithVersion(v04, r.handleTraces))
http.HandleFunc("/v0.4/services", r.httpHandleWithVersion(v04, r.handleServices))
// expvar implicitely publishes "/debug/vars" on the same port
addr := fmt.Sprintf("%s:%d", r.conf.ReceiverHost, r.conf.ReceiverPort)
if err := r.Listen(addr, ""); err != nil {
osutil.Exitf("%v", err)
}
go r.PreSampler.Run()
go func() {
defer watchdog.LogOnPanic()
r.logStats()
}()
}
// Listen creates a new HTTP server listening on the provided address.
func (r *HTTPReceiver) Listen(addr, logExtra string) error {
listener, err := net.Listen("tcp", addr)
if err != nil {
return fmt.Errorf("cannot listen on %s: %v", addr, err)
}
ln, err := newRateLimitedListener(listener, r.conf.ConnectionLimit)
if err != nil {
return fmt.Errorf("cannot create listener: %v", err)
}
timeout := 5 * time.Second
if r.conf.ReceiverTimeout > 0 {
timeout = time.Duration(r.conf.ReceiverTimeout) * time.Second
}
r.server = &http.Server{
ReadTimeout: timeout,
WriteTimeout: timeout,
}
log.Infof("listening for traces at http://%s%s", addr, logExtra)
go func() {
defer watchdog.LogOnPanic()
ln.Refresh(r.conf.ConnectionLimit)
}()
go func() {
defer watchdog.LogOnPanic()
r.server.Serve(ln)
}()
return nil
}
// Stop stops the receiver and shuts down the HTTP server.
func (r *HTTPReceiver) Stop() error {
expiry := time.Now().Add(20 * time.Second) // give it 20 seconds
ctx, _ := context.WithDeadline(context.Background(), expiry)
return r.server.Shutdown(ctx)
}
func (r *HTTPReceiver) httpHandle(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, req *http.Request) {
req.Body = NewLimitedReader(req.Body, r.maxRequestBodyLength)
defer req.Body.Close()
fn(w, req)
}
}
func (r *HTTPReceiver) httpHandleWithVersion(v Version, f func(Version, http.ResponseWriter, *http.Request)) http.HandlerFunc {
return r.httpHandle(func(w http.ResponseWriter, req *http.Request) {
contentType := req.Header.Get("Content-Type")
if contentType == "application/msgpack" && (v == v01 || v == v02) {
// msgpack is only supported for versions 0.3
log.Errorf("rejecting client request, unsupported media type %q", contentType)
HTTPFormatError([]string{tagTraceHandler, fmt.Sprintf("v:%s", v)}, w)
return
}
f(v, w, req)
})
}
func (r *HTTPReceiver) replyTraces(v Version, w http.ResponseWriter) {
switch v {
case v01:
fallthrough
case v02:
fallthrough
case v03:
// Simple response, simply acknowledge with "OK"
HTTPOK(w)
case v04:
// Return the recommended sampling rate for each service as a JSON.
HTTPRateByService(w, r.dynConf)
}
}
// handleTraces knows how to handle a bunch of traces
func (r *HTTPReceiver) handleTraces(v Version, w http.ResponseWriter, req *http.Request) {
if !r.PreSampler.Sample(req) {
io.Copy(ioutil.Discard, req.Body)
HTTPOK(w)
return
}
traces, ok := getTraces(v, w, req)
if !ok {
return
}
// We successfuly decoded the payload
r.replyTraces(v, w)
// We parse the tags from the header
tags := info.Tags{
Lang: req.Header.Get("Datadog-Meta-Lang"),
LangVersion: req.Header.Get("Datadog-Meta-Lang-Version"),
Interpreter: req.Header.Get("Datadog-Meta-Lang-Interpreter"),
TracerVersion: req.Header.Get("Datadog-Meta-Tracer-Version"),
}
// We get the address of the struct holding the stats associated to the tags
ts := r.Stats.GetTagStats(tags)
bytesRead := req.Body.(*LimitedReader).Count
if bytesRead > 0 {
atomic.AddInt64(&ts.TracesBytes, int64(bytesRead))
}
// normalize data
for _, trace := range traces {
spans := len(trace)
atomic.AddInt64(&ts.TracesReceived, 1)
atomic.AddInt64(&ts.SpansReceived, int64(spans))
err := agent.NormalizeTrace(trace)
if err != nil {
atomic.AddInt64(&ts.TracesDropped, 1)
atomic.AddInt64(&ts.SpansDropped, int64(spans))
errorMsg := fmt.Sprintf("dropping trace reason: %s (debug for more info), %v", err, trace)
// avoid truncation in DEBUG mode
if len(errorMsg) > 150 && !r.debug {
errorMsg = errorMsg[:150] + "..."
}
log.Errorf(errorMsg)
} else {
select {
case r.Out <- trace:
// if our downstream consumer is slow, we drop the trace on the floor
// this is a safety net against us using too much memory
// when clients flood us
default:
atomic.AddInt64(&ts.TracesDropped, 1)
atomic.AddInt64(&ts.SpansDropped, int64(spans))
log.Errorf("dropping trace reason: rate-limited")
}
}
}
}
// handleServices handle a request with a list of several services
func (r *HTTPReceiver) handleServices(v Version, w http.ResponseWriter, req *http.Request) {
var servicesMeta pb.ServicesMetadata
contentType := req.Header.Get("Content-Type")
if err := decodeReceiverPayload(req.Body, &servicesMeta, v, contentType); err != nil {
log.Errorf("cannot decode %s services payload: %v", v, err)
HTTPDecodingError(err, []string{tagServiceHandler, fmt.Sprintf("v:%s", v)}, w)
return
}
HTTPOK(w)
// We parse the tags from the header
tags := info.Tags{
Lang: req.Header.Get("Datadog-Meta-Lang"),
LangVersion: req.Header.Get("Datadog-Meta-Lang-Version"),
Interpreter: req.Header.Get("Datadog-Meta-Lang-Interpreter"),
TracerVersion: req.Header.Get("Datadog-Meta-Tracer-Version"),
}
// We get the address of the struct holding the stats associated to the tags
ts := r.Stats.GetTagStats(tags)
atomic.AddInt64(&ts.ServicesReceived, int64(len(servicesMeta)))
bytesRead := req.Body.(*LimitedReader).Count
if bytesRead > 0 {
atomic.AddInt64(&ts.ServicesBytes, int64(bytesRead))
}
r.services <- servicesMeta
}
// logStats periodically submits stats about the receiver to statsd
func (r *HTTPReceiver) logStats() {
var lastLog time.Time
accStats := info.NewReceiverStats()
for now := range time.Tick(10 * time.Second) {
metrics.Gauge("datadog.trace_agent.heartbeat", 1, nil, 1)
// We update accStats with the new stats we collected
accStats.Acc(r.Stats)
// Publish the stats accumulated during the last flush
r.Stats.Publish()
// We reset the stats accumulated during the last 10s.
r.Stats.Reset()
if now.Sub(lastLog) >= time.Minute {
// We expose the stats accumulated to expvar
info.UpdateReceiverStats(accStats)
for _, logStr := range accStats.Strings() {
log.Info(logStr)
}
// We reset the stats accumulated during the last minute
accStats.Reset()
lastLog = now
// Also publish rates by service (they are updated by receiver)
rates := r.dynConf.RateByService.GetAll()
info.UpdateRateByService(rates)
}
}
}
// Languages returns the list of the languages used in the traces the agent receives.
func (r *HTTPReceiver) Languages() string {
// We need to use this map because we can have several tags for a same language.
langs := make(map[string]bool)
str := []string{}
r.Stats.RLock()
for tags := range r.Stats.Stats {
if _, ok := langs[tags.Lang]; !ok {
str = append(str, tags.Lang)
langs[tags.Lang] = true
}
}
r.Stats.RUnlock()
sort.Strings(str)
return strings.Join(str, "|")
}
func getTraces(v Version, w http.ResponseWriter, req *http.Request) (pb.Traces, bool) {
var traces pb.Traces
contentType := req.Header.Get("Content-Type")
switch v {
case v01:
// We cannot use decodeReceiverPayload because []model.Span does not
// implement msgp.Decodable. This hack can be removed once we
// drop v01 support.
if contentType != "application/json" && contentType != "text/json" && contentType != "" {
log.Errorf("rejecting client request, unsupported media type %q", contentType)
HTTPFormatError([]string{tagTraceHandler, fmt.Sprintf("v:%s", v)}, w)
return nil, false
}
// in v01 we actually get spans that we have to transform in traces
var spans []pb.Span
if err := json.NewDecoder(req.Body).Decode(&spans); err != nil {
log.Errorf("cannot decode %s traces payload: %v", v, err)
HTTPDecodingError(err, []string{tagTraceHandler, fmt.Sprintf("v:%s", v)}, w)
return nil, false
}
traces = tracesFromSpans(spans)
case v02:
fallthrough
case v03:
fallthrough
case v04:
if err := decodeReceiverPayload(req.Body, &traces, v, contentType); err != nil {
log.Errorf("cannot decode %s traces payload: %v", v, err)
HTTPDecodingError(err, []string{tagTraceHandler, fmt.Sprintf("v:%s", v)}, w)
return nil, false
}
default:
HTTPEndpointNotSupported([]string{tagTraceHandler, fmt.Sprintf("v:%s", v)}, w)
return nil, false
}
return traces, true
}
func decodeReceiverPayload(r io.Reader, dest msgp.Decodable, v Version, contentType string) error {
switch contentType {
case "application/msgpack":
return msgp.Decode(r, dest)
case "application/json":
fallthrough
case "text/json":
fallthrough
case "":
return json.NewDecoder(r).Decode(dest)
default:
panic(fmt.Sprintf("unhandled content type %q", contentType))
}
}
func tracesFromSpans(spans []pb.Span) pb.Traces {
traces := pb.Traces{}
byID := make(map[uint64][]*pb.Span)
for _, s := range spans {
byID[s.TraceID] = append(byID[s.TraceID], &s)
}
for _, t := range byID {
traces = append(traces, t)
}
return traces
}