forked from krak3n/opentelemetry-go-datadog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trace.go
358 lines (294 loc) · 8.02 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package datadog
import (
"bytes"
"context"
"encoding/binary"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"net"
"net/http"
"net/url"
"path"
"runtime"
"strconv"
"strings"
"time"
"github.com/tinylib/msgp/msgp"
"go.opentelemetry.io/otel/sdk/export/trace"
export "go.opentelemetry.io/otel/sdk/export/trace"
"google.golang.org/api/support/bundler"
)
// DataDog agent constants.
const (
PacketLimit = int(1e7) // 10MB
FlushThreshold = PacketLimit / 2
)
// Default HTTP Client Timeouts and Settings.
const (
OneSecondTimeout = time.Second * 1
TenSecondTimeout = time.Second * 10
ThirtySecondTimeout = time.Second * 30
NintySecondTimeout = time.Second * 90
DefaultMaxIdleConns = 100
)
// MessagePack constants.
const (
MsgPackMaxLength = uint(math.MaxUint32)
MsgPackArrayFix byte = 144 // up to 15 items
MsgPackArray16 = 0xdc // up to 2^16-1 items, followed by size in 2 bytes
MsgPackArray32 = 0xdd // up to 2^32-1 items, followed by size in 4 bytes
)
// Ensure TraceExporter satisfies the export.SpanSyncer interface.
var _ export.SpanSyncer = (*TraceExporter)(nil)
// An Uploader uploads trace data to the DataDog trace agent.
type Uploader interface {
Upload(data io.Reader, count int) (TraceAgentResponse, error)
}
// TraceExporter exports traces to DataDog.
type TraceExporter struct {
bundler *bundler.Bundler
}
// NewTraceExporter constructs a new TraceExporter.
func NewTraceExporter() (*TraceExporter, error) {
uploader := NewTraceAgent()
bundler := bundler.NewBundler((*Span)(nil), func(bundle interface{}) {
spans := bundle.([]*Span)
req := NewTraceAgentRequest()
for _, span := range spans {
if err := req.Add(span); err != nil {
log.Println(err)
}
if req.Size() > FlushThreshold {
_, err := uploader.Upload(req.Buffer(), 1)
if err != nil {
log.Println(err)
return
}
req.Reset()
}
}
_, err := uploader.Upload(req.Buffer(), 1)
if err != nil {
log.Println(err)
return
}
req.Reset()
})
return &TraceExporter{
bundler: bundler,
}, nil
}
// ExportSpan receives a single span and exports it to DataDog
func (e *TraceExporter) ExportSpan(ctx context.Context, span *trace.SpanData) {
if err := e.bundler.Add(ConvertSpan(span), 1); err != nil {
log.Println(err)
}
}
// ExportSpans receives a multiple spans and exports them to DataDog
func (e *TraceExporter) ExportSpans(ctx context.Context, spans []*trace.SpanData) {
for _, span := range spans {
e.ExportSpan(ctx, span)
}
}
// Flush exports spans to DataDog.
func (e *TraceExporter) Flush() {
e.bundler.Flush()
}
// SpanPackets holds the spans encoded in msgpack format. Spans are added squentially whilst
// keeping track of the count.
type SpanPackets struct {
count uint64
data bytes.Buffer
}
// Add adds a span to SpanPackets.
func (s *SpanPackets) Add(span *Span) error {
if uint(s.count) >= MsgPackMaxLength {
return ErrMsgPackOverflow
}
if err := msgp.Encode(&s.data, span); err != nil {
return err
}
s.count++
return nil
}
// Size returns the size of the packets including the msgpack headers.
func (s *SpanPackets) Size() int {
return s.data.Len() + msgpHeaderSize(s.count)
}
// Bytes returns msgpack encoded spans without the headers.
func (s *SpanPackets) Bytes() []byte {
var header [8]byte
off := msgpHeader(&header, s.count)
var buf bytes.Buffer
buf.Write(header[off:])
buf.Write(s.data.Bytes())
return buf.Bytes()
}
// Reset clears spans and sets the count to 0.
func (s *SpanPackets) Reset() {
s.count = 0
s.data.Reset()
}
// TraceAgentRequest holds traces waiting to be sent the DataDog agent.
type TraceAgentRequest struct {
packets map[uint64]*SpanPackets
size int
}
// NewTraceAgentRequest constructs anew TraceAgentRequest
func NewTraceAgentRequest() *TraceAgentRequest {
return &TraceAgentRequest{
packets: make(map[uint64]*SpanPackets),
}
}
// Add adds a span to the TraceAgentRequest packets.
func (r TraceAgentRequest) Add(span *Span) error {
if uint(len(r.packets)) >= MsgPackMaxLength {
return ErrMsgPackOverflow
}
tid := span.TraceID
if _, ok := r.packets[tid]; !ok {
r.packets[tid] = new(SpanPackets)
}
old := r.packets[tid].Size()
if err := r.packets[tid].Add(span); err != nil {
return err
}
size := r.packets[tid].Size()
r.size += size - old
return nil
}
// Size returns the size of the request including the headers.
func (r *TraceAgentRequest) Size() int {
return r.size + msgpHeaderSize(uint64(len(r.packets)))
}
// Buffer returns a copy of the msgpack encoded packets.
func (r *TraceAgentRequest) Buffer() *bytes.Buffer {
var header [8]byte
var buffer bytes.Buffer
off := msgpHeader(&header, uint64(len(r.packets)))
buffer.Write(header[off:])
for _, packet := range r.packets {
buffer.Write(packet.Bytes())
}
return &buffer
}
// Reset resets the trace request.
func (r *TraceAgentRequest) Reset() {
r.packets = make(map[uint64]*SpanPackets)
r.size = 0
}
// TraceAgentResponse is the response from the DataDog trace agent after successful upload.
type TraceAgentResponse struct {
Rates map[string]float64 `json:"rate_by_service"`
}
// TraceAgent uploads traces to the DataDog trace agent.
type TraceAgent struct {
url *url.URL
client *http.Client
headers map[string]string
}
// NewTraceAgent constructs a new TraceAgent.
func NewTraceAgent() *TraceAgent {
client := &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: ThirtySecondTimeout,
KeepAlive: ThirtySecondTimeout,
DualStack: true,
}).DialContext,
MaxIdleConns: DefaultMaxIdleConns,
IdleConnTimeout: NintySecondTimeout,
TLSHandshakeTimeout: TenSecondTimeout,
ExpectContinueTimeout: OneSecondTimeout,
},
Timeout: OneSecondTimeout,
}
url := &url.URL{
Scheme: "http",
Host: "localhost:8126",
Path: path.Join("v0.4", "traces"),
}
headers := map[string]string{
"Datadog-Meta-Lang": "go",
"Datadog-Meta-Lang-Version": strings.TrimPrefix(runtime.Version(), "go"),
"Datadog-Meta-Lang-Interpreter": runtime.Compiler + "-" + runtime.GOARCH + "-" + runtime.GOOS,
"Datadog-Meta-TraceAgent-Version": "OTEL/v0.4.2",
"Content-Type": "application/msgpack",
}
return &TraceAgent{
url: url,
client: client,
headers: headers,
}
}
// Upload uploads a trace to the DataDog trace agent.
func (t *TraceAgent) Upload(data io.Reader, count int) (TraceAgentResponse, error) {
var tar TraceAgentResponse
req, err := http.NewRequest(http.MethodPost, t.url.String(), data)
if err != nil {
return tar, err
}
req.Header.Set("X-DataDog-Trace-Count", strconv.Itoa(count))
for k, v := range t.headers {
req.Header.Set(k, v)
}
rsp, err := t.client.Do(req)
if err != nil {
return tar, err
}
defer Close(rsp.Body)
body, err := ioutil.ReadAll(rsp.Body)
if err != nil {
return tar, err
}
if rsp.StatusCode >= http.StatusBadRequest {
return tar, fmt.Errorf("[%d %s] %s",
rsp.StatusCode,
http.StatusText(rsp.StatusCode),
string(body))
}
if err := json.Unmarshal(body, &tar); err != nil {
return tar, err
}
return tar, nil
}
// msgpHeaderSize returns the size in bytes of a header for a msgpack array of length n.
func msgpHeaderSize(n uint64) int {
switch {
case n == 0:
return 0
case n <= 15:
return 1
case n <= math.MaxUint16:
return 3
case n <= math.MaxUint32:
fallthrough
default:
return 5
}
}
// msgpHeader writes the msgpack array header for a slice of length n into out.
// It returns the offset at which to begin reading from out. For more information,
// see the msgpack spec:
// https://github.com/msgpack/msgpack/blob/master/spec.md#array-format-family
func msgpHeader(out *[8]byte, n uint64) (off int) {
off = 8 - msgpHeaderSize(n)
switch {
case n <= 15:
out[off] = MsgPackArrayFix + byte(n)
case n <= math.MaxUint16:
binary.BigEndian.PutUint64(out[:], n) // writes 2 bytes
out[off] = MsgPackArray16
case n <= math.MaxUint32:
fallthrough
default:
binary.BigEndian.PutUint64(out[:], n) // writes 4 bytes
out[off] = MsgPackArray32
}
return off
}