Skip to content

Commit f8c38c5

Browse files
committed
upstream breaking change: remvoe multipart-form sending
1 parent 8003d94 commit f8c38c5

File tree

6 files changed

+34
-134
lines changed

6 files changed

+34
-134
lines changed

collector.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ func (c *cpuProfileCollector) upload() {
240240
SampleRate: DefaultSampleRate,
241241
Units: "samples",
242242
AggregationType: "sum",
243-
Format: upstream.FormatPprof,
244243
Profile: copyBuf(buf),
245244
})
246245
c.buf.Reset()

sample_types.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

session.go

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,7 @@ func (ps *Session) uploadData(startTime, endTime time.Time) {
275275
SpyName: "gospy",
276276
Units: "goroutines",
277277
AggregationType: "average",
278-
Format: upstream.FormatPprof,
279278
Profile: copyBuf(ps.goroutinesBuf.Bytes()),
280-
SampleTypeConfig: map[string]*upstream.SampleType{
281-
"goroutine": {
282-
DisplayName: "goroutines",
283-
Units: "goroutines",
284-
Aggregation: "average",
285-
},
286-
},
287279
})
288280
ps.goroutinesBuf.Reset()
289281
}
@@ -324,14 +316,12 @@ func (ps *Session) dumpHeapProfile(startTime time.Time, endTime time.Time) {
324316
}
325317
curMemBytes := copyBuf(ps.memBuf.Bytes())
326318
job := &upstream.UploadJob{
327-
Name: ps.appName,
328-
StartTime: startTime,
329-
EndTime: endTime,
330-
SpyName: "gospy",
331-
SampleRate: 100,
332-
Format: upstream.FormatPprof,
333-
Profile: curMemBytes,
334-
SampleTypeConfig: sampleTypeConfigHeap,
319+
Name: ps.appName,
320+
StartTime: startTime,
321+
EndTime: endTime,
322+
SpyName: "gospy",
323+
SampleRate: 100,
324+
Profile: curMemBytes,
335325
}
336326
ps.upstream.Upload(job)
337327
ps.lastGCGeneration = currentGCGeneration
@@ -353,13 +343,11 @@ func (ps *Session) dumpMutexProfile(startTime time.Time, endTime time.Time) {
353343
}
354344
curMutexBuf := copyBuf(ps.mutexBuf.Bytes())
355345
job := &upstream.UploadJob{
356-
Name: ps.appName,
357-
StartTime: startTime,
358-
EndTime: endTime,
359-
SpyName: "gospy",
360-
Format: upstream.FormatPprof,
361-
Profile: curMutexBuf,
362-
SampleTypeConfig: sampleTypeConfigMutex,
346+
Name: ps.appName,
347+
StartTime: startTime,
348+
EndTime: endTime,
349+
SpyName: "gospy",
350+
Profile: curMutexBuf,
363351
}
364352
ps.upstream.Upload(job)
365353
}
@@ -379,13 +367,11 @@ func (ps *Session) dumpBlockProfile(startTime time.Time, endTime time.Time) {
379367
}
380368
curBlockBuf := copyBuf(ps.blockBuf.Bytes())
381369
job := &upstream.UploadJob{
382-
Name: ps.appName,
383-
StartTime: startTime,
384-
EndTime: endTime,
385-
SpyName: "gospy",
386-
Format: upstream.FormatPprof,
387-
Profile: curBlockBuf,
388-
SampleTypeConfig: sampleTypeConfigBlock,
370+
Name: ps.appName,
371+
StartTime: startTime,
372+
EndTime: endTime,
373+
SpyName: "gospy",
374+
Profile: curBlockBuf,
389375
}
390376
ps.upstream.Upload(job)
391377
}

upstream/remote/remote.go

Lines changed: 13 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package remote
33
import (
44
"bytes"
55
"context"
6-
"encoding/json"
76
"errors"
87
"fmt"
98
"io"
10-
"mime/multipart"
119
"net/http"
1210
"net/url"
1311
"path"
@@ -42,6 +40,7 @@ type Remote struct {
4240
wg sync.WaitGroup
4341

4442
flushWG *sync.WaitGroup
43+
url *url.URL
4544
}
4645

4746
type HTTPClient interface {
@@ -100,6 +99,7 @@ func NewRemote(cfg Config) (*Remote, error) {
10099
if err != nil {
101100
return nil, err
102101
}
102+
r.url = u
103103

104104
// authorize the token first
105105
if cfg.AuthToken == "" && isOGPyroscopeCloud(u) {
@@ -150,35 +150,10 @@ func (r *Remote) Flush() {
150150
}
151151

152152
func (r *Remote) uploadProfile(j *upstream.UploadJob) error {
153-
u, err := url.Parse(r.cfg.Address)
154-
if err != nil {
155-
return fmt.Errorf("url parse: %w", err)
156-
}
157-
158-
body := &bytes.Buffer{}
159-
160-
writer := multipart.NewWriter(body)
161-
fw, err := writer.CreateFormFile("profile", "profile.pprof")
162-
if err != nil {
163-
return err
164-
}
165-
_, _ = fw.Write(j.Profile)
166-
if j.SampleTypeConfig != nil {
167-
fw, err = writer.CreateFormFile("sample_type_config", "sample_type_config.json")
168-
if err != nil {
169-
return err
170-
}
171-
b, err := json.Marshal(j.SampleTypeConfig)
172-
if err != nil {
173-
return err
174-
}
175-
_, _ = fw.Write(b)
176-
}
177-
if err = writer.Close(); err != nil {
178-
return err
179-
}
153+
u := *r.url
180154

181155
q := u.Query()
156+
q.Set("format", "pprof")
182157
q.Set("name", j.Name)
183158
q.Set("from", strconv.FormatInt(j.StartTime.UnixNano(), 10))
184159
q.Set("until", strconv.FormatInt(j.EndTime.UnixNano(), 10))
@@ -191,18 +166,20 @@ func (r *Remote) uploadProfile(j *upstream.UploadJob) error {
191166
u.RawQuery = q.Encode()
192167

193168
r.logger.Debugf("uploading at %s", u.String())
194-
// new a request for the job
195-
request, err := http.NewRequestWithContext(context.Background(), http.MethodPost, u.String(), body)
169+
170+
request, err := http.NewRequestWithContext(
171+
context.Background(), // todo: timeout
172+
http.MethodPost,
173+
u.String(),
174+
bytes.NewReader(j.Profile),
175+
)
196176
if err != nil {
197177
return fmt.Errorf("new http request: %w", err)
198178
}
199-
contentType := writer.FormDataContentType()
200-
r.logger.Debugf("content type: %s", contentType)
201-
request.Header.Set("Content-Type", contentType)
202-
// request.Header.Set("Content-Type", "binary/octet-stream+"+string(j.Format))
179+
request.Header.Set("Content-Type", "binary/octet-stream+pprof")
203180

204181
switch {
205-
case r.cfg.AuthToken != "" && isOGPyroscopeCloud(u):
182+
case r.cfg.AuthToken != "" && isOGPyroscopeCloud(&u):
206183
request.Header.Set("Authorization", "Bearer "+r.cfg.AuthToken)
207184
case r.cfg.BasicAuthUser != "" && r.cfg.BasicAuthPassword != "":
208185
request.SetBasicAuth(r.cfg.BasicAuthUser, r.cfg.BasicAuthPassword)

upstream/remote/remote_test.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,11 @@ func TestUploadProfile(t *testing.T) {
6666
Body: io.NopCloser(bytes.NewBufferString("OK")),
6767
}, nil)
6868

69-
r := &Remote{
70-
cfg: tt.cfg,
71-
client: mockClient,
72-
logger: logger,
73-
}
74-
75-
err := r.uploadProfile(&upstream.UploadJob{
69+
r, err := NewRemote(tt.cfg)
70+
r.client = mockClient
71+
r.logger = logger
72+
require.NoError(t, err)
73+
err = r.uploadProfile(&upstream.UploadJob{
7674
Name: "test-profile",
7775
StartTime: time.Now(),
7876
EndTime: time.Now().Add(time.Minute),

upstream/upstream.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ import (
44
"time"
55
)
66

7-
type Format string
8-
9-
const FormatPprof Format = "pprof"
10-
117
type Upstream interface {
128
Upload(job *UploadJob)
139
Flush()
@@ -29,9 +25,5 @@ type UploadJob struct {
2925
SampleRate uint32
3026
Units string
3127
AggregationType string
32-
Format Format
3328
Profile []byte
34-
// Deprecated
35-
PrevProfile []byte
36-
SampleTypeConfig map[string]*SampleType
3729
}

0 commit comments

Comments
 (0)