-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
trace.go
267 lines (235 loc) · 8.71 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
// 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-2020 Datadog, Inc.
package writer
import (
"compress/gzip"
"math"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/DataDog/datadog-agent/pkg/trace/config"
"github.com/DataDog/datadog-agent/pkg/trace/info"
"github.com/DataDog/datadog-agent/pkg/trace/logutil"
"github.com/DataDog/datadog-agent/pkg/trace/metrics"
"github.com/DataDog/datadog-agent/pkg/trace/metrics/timing"
"github.com/DataDog/datadog-agent/pkg/trace/pb"
"github.com/DataDog/datadog-agent/pkg/trace/traceutil"
"github.com/DataDog/datadog-agent/pkg/util/log"
"github.com/gogo/protobuf/proto"
)
// 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
// SampledSpans represents the result of a trace sampling operation.
type SampledSpans struct {
// Trace will contain a trace if it was sampled or be empty if it wasn't.
Trace pb.Trace
// Events contains all APM events extracted from a trace. If no events were extracted, it will be empty.
Events []*pb.Span
}
// Empty returns true if this TracePackage has no data.
func (ss *SampledSpans) Empty() bool {
return len(ss.Trace) == 0 && len(ss.Events) == 0
}
// size returns the estimated size of the package.
func (ss *SampledSpans) size() int {
// we use msgpack's Msgsize() heuristic because it is a good indication
// of the weight of a span and the msgpack size is relatively close to
// the protobuf size, which is expensive to compute.
return ss.Trace.Msgsize() + pb.Trace(ss.Events).Msgsize()
}
// TraceWriter buffers traces and APM events, flushing them to the Datadog API.
type TraceWriter struct {
in <-chan *SampledSpans
hostname string
env string
senders []*sender
stop chan struct{}
stats *info.TraceWriterInfo
wg sync.WaitGroup // waits for gzippers
tick time.Duration // flush frequency
traces []*pb.APITrace // traces buffered
events []*pb.Span // events buffered
bufferedSize int // estimated buffer size
easylog *logutil.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, in <-chan *SampledSpans) *TraceWriter {
tw := &TraceWriter{
in: in,
hostname: cfg.Hostname,
env: cfg.DefaultEnv,
stats: &info.TraceWriterInfo{},
stop: make(chan struct{}),
tick: 5 * time.Second,
easylog: logutil.NewThrottled(5, 10*time.Second), // no more than 5 messages every 10 seconds
}
climit := cfg.TraceWriter.ConnectionLimit
if climit == 0 {
// default to 10% of the connection limit to outgoing sends.
climit = int(math.Max(1, float64(cfg.ConnectionLimit)/10))
}
qsize := cfg.TraceWriter.QueueSize
if qsize == 0 {
// default to 50% of maximum memory.
qsize = int(math.Max(1, cfg.MaxMemory/2/float64(maxPayloadSize)))
}
if s := cfg.TraceWriter.FlushPeriodSeconds; s != 0 {
tw.tick = time.Duration(s*1000) * time.Millisecond
}
log.Debugf("Trace writer initialized (climit=%d qsize=%d)", climit, qsize)
tw.senders = newSenders(cfg, tw, pathTraces, climit, qsize)
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
w.wg.Wait()
stopSenders(w.senders)
}
// Run starts the TraceWriter.
func (w *TraceWriter) Run() {
t := time.NewTicker(w.tick)
defer t.Stop()
defer close(w.stop)
for {
select {
case pkg := <-w.in:
w.addSpans(pkg)
case <-w.stop:
// drain the input channel before stopping
outer:
for {
select {
case pkg := <-w.in:
w.addSpans(pkg)
default:
break outer
}
}
w.flush()
return
case <-t.C:
w.report()
w.flush()
}
}
}
func (w *TraceWriter) addSpans(pkg *SampledSpans) {
if pkg.Empty() {
return
}
atomic.AddInt64(&w.stats.Spans, int64(len(pkg.Trace)))
atomic.AddInt64(&w.stats.Traces, 1)
atomic.AddInt64(&w.stats.Events, int64(len(pkg.Events)))
size := pkg.size()
if size+w.bufferedSize > maxPayloadSize {
// reached maximum allowed buffered size
w.flush()
}
if len(pkg.Trace) > 0 {
log.Tracef("Handling new trace with %d spans: %v", len(pkg.Trace), pkg.Trace)
w.traces = append(w.traces, traceutil.APITrace(pkg.Trace))
}
if len(pkg.Events) > 0 {
log.Tracef("Handling new analyzed spans: %v", pkg.Events)
w.events = append(w.events, pkg.Events...)
}
w.bufferedSize += size
}
func (w *TraceWriter) resetBuffer() {
w.bufferedSize = 0
w.traces = w.traces[:0]
w.events = w.events[:0]
}
const headerLanguages = "X-Datadog-Reported-Languages"
func (w *TraceWriter) flush() {
if len(w.traces) == 0 && len(w.events) == 0 {
// nothing to do
return
}
defer timing.Since("datadog.trace_agent.trace_writer.encode_ms", time.Now())
defer w.resetBuffer()
log.Debugf("Serializing %d traces and %d APM events.", len(w.traces), len(w.events))
tracePayload := pb.TracePayload{
HostName: w.hostname,
Env: w.env,
Traces: w.traces,
Transactions: w.events,
}
b, err := proto.Marshal(&tracePayload)
if err != nil {
log.Errorf("Failed to serialize payload, data dropped: %v", err)
return
}
atomic.AddInt64(&w.stats.BytesUncompressed, int64(len(b)))
atomic.AddInt64(&w.stats.BytesEstimated, int64(w.bufferedSize))
w.wg.Add(1)
go func() {
defer timing.Since("datadog.trace_agent.trace_writer.compress_ms", time.Now())
defer w.wg.Done()
p := newPayload(map[string]string{
"Content-Type": "application/x-protobuf",
"Content-Encoding": "gzip",
headerLanguages: strings.Join(info.Languages(), "|"),
})
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)
}()
}
func (w *TraceWriter) report() {
metrics.Count("datadog.trace_agent.trace_writer.payloads", atomic.SwapInt64(&w.stats.Payloads, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.bytes_uncompressed", atomic.SwapInt64(&w.stats.BytesUncompressed, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.retries", atomic.SwapInt64(&w.stats.Retries, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.bytes_estimated", atomic.SwapInt64(&w.stats.BytesEstimated, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.bytes", atomic.SwapInt64(&w.stats.Bytes, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.errors", atomic.SwapInt64(&w.stats.Errors, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.traces", atomic.SwapInt64(&w.stats.Traces, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.events", atomic.SwapInt64(&w.stats.Events, 0), nil, 1)
metrics.Count("datadog.trace_agent.trace_writer.spans", atomic.SwapInt64(&w.stats.Spans, 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)
atomic.AddInt64(&w.stats.Retries, 1)
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))
atomic.AddInt64(&w.stats.Bytes, int64(data.bytes))
atomic.AddInt64(&w.stats.Payloads, 1)
case eventTypeRejected:
log.Warnf("Trace writer payload rejected by edge: %v", data.err)
atomic.AddInt64(&w.stats.Errors, 1)
case eventTypeDropped:
w.easylog.Warn("Trace writer queue full. 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)
}
}