-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathtrace.go
339 lines (299 loc) · 10.3 KB
/
trace.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package writer
import (
"compress/gzip"
"errors"
"runtime"
"strings"
"sync"
"time"
pb "github.com/DataDog/datadog-agent/pkg/proto/pbgo/trace"
"github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/trace/info"
"github.com/DataDog/datadog-agent/pkg/trace/log"
"github.com/DataDog/datadog-agent/pkg/trace/metrics"
"github.com/DataDog/datadog-agent/pkg/trace/metrics/timing"
"github.com/DataDog/datadog-agent/pkg/trace/telemetry"
)
// pathTraces is the target host API path for delivering traces.
const pathTraces = "/api/v0.2/traces"
// MaxPayloadSize specifies the maximum accumulated payload size that is allowed before
// a flush is triggered; replaced in tests.
var MaxPayloadSize = 3200000 // 3.2MB is the maximum allowed by the Datadog API
type samplerTPSReader interface {
GetTargetTPS() float64
}
type samplerEnabledReader interface {
IsEnabled() bool
}
// SampledChunks represents the result of a trace sampling operation.
type SampledChunks struct {
// TracerPayload contains all the chunks that were sampled as part of processing a payload.
TracerPayload *pb.TracerPayload
// Size represents the approximated message size in bytes.
Size int
// SpanCount specifies the number of spans that were sampled as part of a trace inside the TracerPayload.
SpanCount int64
// EventCount specifies the total number of events found in Traces.
EventCount int64
}
// TraceWriter buffers traces and APM events, flushing them to the Datadog API.
type TraceWriter struct {
// In receives sampled spans to be processed by the trace writer.
// Channel should only be received from when testing.
In chan *SampledChunks
Serialize chan *pb.AgentPayload
// used to keep track of payloads currently being flushed
// only useful for tests
swg sync.WaitGroup
prioritySampler samplerTPSReader
errorsSampler samplerTPSReader
rareSampler samplerEnabledReader
hostname string
env string
senders []*sender
stop chan struct{}
stats *info.TraceWriterInfo
wg sync.WaitGroup // waits for gzippers
tick time.Duration // flush frequency
agentVersion string
tracerPayloads []*pb.TracerPayload // tracer payloads buffered
bufferedSize int // estimated buffer size
// syncMode reports whether the writer should flush on its own or only when FlushSync is called
syncMode bool
flushChan chan chan struct{}
telemetryCollector telemetry.TelemetryCollector
easylog *log.ThrottledLogger
}
// NewTraceWriter returns a new TraceWriter. It is created for the given agent configuration and
// will accept incoming spans via the in channel.
func NewTraceWriter(cfg *config.AgentConfig, prioritySampler samplerTPSReader, errorsSampler samplerTPSReader, rareSampler samplerEnabledReader, telemetryCollector telemetry.TelemetryCollector) *TraceWriter {
tw := &TraceWriter{
In: make(chan *SampledChunks, 1),
Serialize: make(chan *pb.AgentPayload, 1),
prioritySampler: prioritySampler,
errorsSampler: errorsSampler,
rareSampler: rareSampler,
hostname: cfg.Hostname,
env: cfg.DefaultEnv,
stats: &info.TraceWriterInfo{},
stop: make(chan struct{}),
flushChan: make(chan chan struct{}),
syncMode: cfg.SynchronousFlushing,
tick: 5 * time.Second,
agentVersion: cfg.AgentVersion,
easylog: log.NewThrottled(5, 10*time.Second), // no more than 5 messages every 10 seconds
telemetryCollector: telemetryCollector,
}
climit := cfg.TraceWriter.ConnectionLimit
if climit == 0 {
climit = 100
}
if cfg.TraceWriter.QueueSize > 0 {
log.Warnf("apm_config.trace_writer.queue_size is deprecated and will not be respected.")
}
if s := cfg.TraceWriter.FlushPeriodSeconds; s != 0 {
tw.tick = time.Duration(s*1000) * time.Millisecond
}
qsize := 1
log.Warnf("Trace writer initialized (climit=%d qsize=%d)", climit, qsize)
tw.senders = newSenders(cfg, tw, pathTraces, climit, qsize, telemetryCollector)
for i := 0; i < runtime.GOMAXPROCS(0); i++ {
tw.wg.Add(1)
go tw.serializer()
}
return tw
}
// Stop stops the TraceWriter and attempts to flush whatever is left in the senders buffers.
func (w *TraceWriter) Stop() {
log.Debug("Exiting trace writer. Trying to flush whatever is left...")
w.stop <- struct{}{}
<-w.stop
// Wait for encoding/compression to complete on each payload,
// and submission to senders
w.wg.Wait()
stopSenders(w.senders)
}
// Run starts the TraceWriter.
func (w *TraceWriter) Run() {
if w.syncMode {
w.runSync()
} else {
w.runAsync()
}
}
func (w *TraceWriter) runAsync() {
t := time.NewTicker(w.tick)
defer t.Stop()
defer close(w.Serialize)
defer close(w.stop)
for {
select {
case pkg := <-w.In:
w.addSpans(pkg)
case <-w.stop:
w.drainAndFlush()
return
case <-t.C:
w.report()
w.flush()
}
}
}
func (w *TraceWriter) runSync() {
defer close(w.Serialize)
defer close(w.stop)
defer close(w.flushChan)
for {
select {
case pkg := <-w.In:
w.addSpans(pkg)
case notify := <-w.flushChan:
w.drainAndFlush()
notify <- struct{}{}
case <-w.stop:
w.drainAndFlush()
return
}
}
}
// FlushSync blocks and sends pending payloads when syncMode is true
func (w *TraceWriter) FlushSync() error {
if !w.syncMode {
return errors.New("not flushing; sync mode not enabled")
}
defer w.report()
notify := make(chan struct{}, 1)
w.flushChan <- notify
<-notify
return nil
}
func (w *TraceWriter) addSpans(pkg *SampledChunks) {
w.stats.Spans.Add(pkg.SpanCount)
w.stats.Traces.Add(int64(len(pkg.TracerPayload.Chunks)))
w.stats.Events.Add(pkg.EventCount)
size := pkg.Size
if size+w.bufferedSize > MaxPayloadSize {
// reached maximum allowed buffered size
w.flush()
}
if len(pkg.TracerPayload.Chunks) > 0 {
log.Tracef("Writer: handling new tracer payload with %d spans: %v", pkg.SpanCount, pkg.TracerPayload)
w.tracerPayloads = append(w.tracerPayloads, pkg.TracerPayload)
}
w.bufferedSize += size
}
func (w *TraceWriter) drainAndFlush() {
outer:
for {
select {
case pkg := <-w.In:
w.addSpans(pkg)
default:
break outer
}
}
w.flush()
w.swg.Wait()
}
func (w *TraceWriter) resetBuffer() {
w.bufferedSize = 0
w.tracerPayloads = make([]*pb.TracerPayload, 0, len(w.tracerPayloads))
}
const headerLanguages = "X-Datadog-Reported-Languages"
func (w *TraceWriter) flush() {
if len(w.tracerPayloads) == 0 {
// nothing to do
return
}
defer timing.Since("datadog.trace_agent.trace_writer.encode_ms", time.Now())
defer w.resetBuffer()
log.Debugf("Serializing %d tracer payloads.", len(w.tracerPayloads))
p := pb.AgentPayload{
AgentVersion: w.agentVersion,
HostName: w.hostname,
Env: w.env,
TargetTPS: w.prioritySampler.GetTargetTPS(),
ErrorTPS: w.errorsSampler.GetTargetTPS(),
RareSamplerEnabled: w.rareSampler.IsEnabled(),
TracerPayloads: w.tracerPayloads,
}
log.Debugf("Reported agent rates: target_tps=%v errors_tps=%v rare_sampling=%v", p.TargetTPS, p.ErrorTPS, p.RareSamplerEnabled)
w.swg.Add(1)
w.Serialize <- &p
}
func (w *TraceWriter) serializer() {
defer w.wg.Done()
for pl := range w.Serialize {
func() {
defer w.swg.Done()
b, err := pl.MarshalVT()
if err != nil {
log.Errorf("Failed to serialize payload, data dropped: %v", err)
return
}
w.stats.BytesUncompressed.Add(int64(len(b)))
p := newPayload(map[string]string{
"Content-Type": "application/x-protobuf",
"Content-Encoding": "gzip",
headerLanguages: strings.Join(info.Languages(), "|"),
})
p.body.Grow(len(b) / 2)
gzipw, err := gzip.NewWriterLevel(p.body, gzip.BestSpeed)
if err != nil {
// it will never happen, unless an invalid compression is chosen;
// we know gzip.BestSpeed is valid.
log.Errorf("gzip.NewWriterLevel: %d", err)
return
}
if _, err := gzipw.Write(b); err != nil {
log.Errorf("Error gzipping trace payload: %v", err)
}
if err := gzipw.Close(); err != nil {
log.Errorf("Error closing gzip stream when writing trace payload: %v", err)
}
sendPayloads(w.senders, p, w.syncMode)
}()
}
}
func (w *TraceWriter) report() {
metrics.Count("datadog.trace_agent.trace_writer.payloads", w.stats.Payloads.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.bytes_uncompressed", w.stats.BytesUncompressed.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.retries", w.stats.Retries.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.bytes", w.stats.Bytes.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.errors", w.stats.Errors.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.traces", w.stats.Traces.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.events", w.stats.Events.Swap(0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.spans", w.stats.Spans.Swap(0), nil, 1)
}
var _ eventRecorder = (*TraceWriter)(nil)
// recordEvent implements eventRecorder.
func (w *TraceWriter) recordEvent(t eventType, data *eventData) {
if data != nil {
metrics.Histogram("datadog.trace_agent.trace_writer.connection_fill", data.connectionFill, nil, 1)
metrics.Histogram("datadog.trace_agent.trace_writer.queue_fill", data.queueFill, nil, 1)
}
switch t {
case eventTypeRetry:
log.Debugf("Retrying to flush trace payload; error: %s", data.err)
w.stats.Retries.Inc()
case eventTypeSent:
log.Debugf("Flushed traces to the API; time: %s, bytes: %d", data.duration, data.bytes)
timing.Since("datadog.trace_agent.trace_writer.flush_duration", time.Now().Add(-data.duration))
w.stats.Bytes.Add(int64(data.bytes))
w.stats.Payloads.Inc()
if !w.telemetryCollector.SentFirstTrace() {
go w.telemetryCollector.SendFirstTrace()
}
case eventTypeRejected:
log.Warnf("Trace writer payload rejected by edge: %v", data.err)
w.stats.Errors.Inc()
case eventTypeDropped:
w.easylog.Warn("Trace Payload dropped (%.2fKB).", float64(data.bytes)/1024)
metrics.Count("datadog.trace_agent.trace_writer.dropped", 1, nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.dropped_bytes", int64(data.bytes), nil, 1)
}
}