forked from DataDog/dd-trace-go
/
upload.go
197 lines (178 loc) · 5.67 KB
/
upload.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
// 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 Datadog, Inc.
package profiler
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"math/rand"
"mime/multipart"
"net/http"
"net/textproto"
"strings"
"time"
"github.com/lsgndln/dd-trace-go/internal/log"
)
// maxRetries specifies the maximum number of retries to have when an error occurs.
const maxRetries = 2
var errOldAgent = errors.New("Datadog Agent is not accepting profiles. Agent-based profiling deployments " +
"require Datadog Agent >= 7.20")
// upload tries to upload a batch of profiles. It has retry and backoff mechanisms.
func (p *profiler) upload(bat batch) error {
statsd := p.cfg.statsd
var err error
for i := 0; i < maxRetries; i++ {
select {
case <-p.exit:
return nil
default:
}
err = p.doRequest(bat)
if rerr, ok := err.(*retriableError); ok {
statsd.Count("datadog.profiling.go.upload_retry", 1, nil, 1)
wait := time.Duration(rand.Int63n(p.cfg.period.Nanoseconds())) * time.Nanosecond
log.Error("Uploading profile failed: %v. Trying again in %s...", rerr, wait)
p.interruptibleSleep(wait)
continue
}
if err != nil {
statsd.Count("datadog.profiling.go.upload_error", 1, nil, 1)
} else {
statsd.Count("datadog.profiling.go.upload_success", 1, nil, 1)
var b int64
for _, p := range bat.profiles {
b += int64(len(p.data))
}
statsd.Count("datadog.profiling.go.uploaded_profile_bytes", b, nil, 1)
}
return err
}
return fmt.Errorf("failed after %d retries, last error was: %v", maxRetries, err)
}
// retriableError is an error returned by the server which may be retried at a later time.
type retriableError struct{ err error }
// Error implements error.
func (e retriableError) Error() string { return e.err.Error() }
// doRequest makes an HTTP POST request to the Datadog Profiling API with the
// given profile.
func (p *profiler) doRequest(bat batch) error {
tags := append(p.cfg.tags.Slice(),
fmt.Sprintf("service:%s", p.cfg.service),
// The profile_seq tag can be used to identify the first profile
// uploaded by a given runtime-id, identify missing profiles, etc.. See
// PROF-5612 (internal) for more details.
fmt.Sprintf("profile_seq:%d", bat.seq),
)
// If the user did not configure an "env" in the client, we should omit
// the tag so that the agent has a chance to supply a default tag.
// Otherwise, the tag supplied by the client will have priority.
if p.cfg.env != "" {
tags = append(tags, fmt.Sprintf("env:%s", p.cfg.env))
}
// If the profile batch includes a runtime execution trace, add a tag so
// that the uploads are more easily discoverable in the UI.
for _, b := range bat.profiles {
if b.pt == executionTrace {
tags = append(tags, "go_execution_traced:yes")
}
}
contentType, body, err := encode(bat, tags)
if err != nil {
return err
}
funcExit := make(chan struct{})
defer close(funcExit)
// uploadTimeout is guaranteed to be >= 0, see newProfiler.
ctx, cancel := context.WithTimeout(context.Background(), p.cfg.uploadTimeout)
go func() {
select {
case <-p.exit:
case <-funcExit:
}
cancel()
}()
req, err := http.NewRequestWithContext(ctx, "POST", p.cfg.targetURL, body)
if err != nil {
return err
}
if p.cfg.apiKey != "" {
req.Header.Set("DD-API-KEY", p.cfg.apiKey)
}
if containerID != "" {
req.Header.Set("Datadog-Container-ID", containerID)
}
req.Header.Set("Content-Type", contentType)
resp, err := p.cfg.httpClient.Do(req)
if err != nil {
return &retriableError{err}
}
defer resp.Body.Close()
if resp.StatusCode/100 == 5 {
// 5xx can be retried
return &retriableError{errors.New(resp.Status)}
}
if resp.StatusCode == 404 && p.cfg.targetURL == p.cfg.agentURL {
// 404 from the agent means we have an old agent version without profiling endpoint
return errOldAgent
}
if resp.StatusCode >= 200 && resp.StatusCode <= 299 {
// Success!
return nil
}
return errors.New(resp.Status)
}
type uploadEvent struct {
Start string `json:"start"`
End string `json:"end"`
Attachments []string `json:"attachments"`
Tags string `json:"tags_profiler"`
Family string `json:"family"`
Version string `json:"version"`
EndpointCounts map[string]uint64 `json:"endpoint_counts,omitempty"`
}
// encode encodes the profile as a multipart mime request.
func encode(bat batch, tags []string) (contentType string, body io.Reader, err error) {
var buf bytes.Buffer
mw := multipart.NewWriter(&buf)
if bat.host != "" {
tags = append(tags, fmt.Sprintf("host:%s", bat.host))
}
tags = append(tags, "runtime:go")
event := &uploadEvent{
Version: "4",
Family: "go",
Start: bat.start.Format(time.RFC3339Nano),
End: bat.end.Format(time.RFC3339Nano),
Tags: strings.Join(tags, ","),
EndpointCounts: bat.endpointCounts,
}
for _, p := range bat.profiles {
event.Attachments = append(event.Attachments, p.name)
f, err := mw.CreateFormFile(p.name, p.name)
if err != nil {
return "", nil, err
}
if _, err := f.Write(p.data); err != nil {
return "", nil, err
}
}
f, err := mw.CreatePart(textproto.MIMEHeader{
"Content-Disposition": []string{`form-data; name="event"; filename="event.json"`},
"Content-Type": []string{"application/json"},
})
if err != nil {
return "", nil, err
}
if err := json.NewEncoder(f).Encode(event); err != nil {
return "", nil, err
}
if err := mw.Close(); err != nil {
return "", nil, err
}
return mw.FormDataContentType(), &buf, nil
}