-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdebug.go
More file actions
56 lines (48 loc) · 1.58 KB
/
debug.go
File metadata and controls
56 lines (48 loc) · 1.58 KB
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
package codersdk
import (
"context"
"io"
"net/http"
"net/url"
"strings"
"time"
"golang.org/x/xerrors"
)
// DebugProfileDurationMax is the maximum duration the server will accept
// for a profile collection. Callers should ensure their context deadline
// exceeds this to avoid premature cancellation.
const DebugProfileDurationMax = 60 * time.Second
// DebugProfileOptions are options for collecting debug profiles from the
// server via the consolidated /debug/profile endpoint.
type DebugProfileOptions struct {
// Duration controls how long time-based profiles (cpu, trace) run.
// Zero uses the server default (10s).
Duration time.Duration
// Profiles is the list of profile types to collect. Nil or empty uses
// the server default (cpu, heap, allocs, block, mutex, goroutine).
Profiles []string
}
// DebugCollectProfile fetches a tar.gz archive of pprof profiles from the
// server. The caller is responsible for closing the returned ReadCloser.
func (c *Client) DebugCollectProfile(ctx context.Context, opts DebugProfileOptions) (io.ReadCloser, error) {
qp := url.Values{}
if opts.Duration > 0 {
qp.Set("duration", opts.Duration.String())
}
if len(opts.Profiles) > 0 {
qp.Set("profiles", strings.Join(opts.Profiles, ","))
}
reqPath := "/api/v2/debug/profile"
if len(qp) > 0 {
reqPath += "?" + qp.Encode()
}
resp, err := c.Request(ctx, http.MethodPost, reqPath, nil)
if err != nil {
return nil, xerrors.Errorf("request debug profile: %w", err)
}
if resp.StatusCode != http.StatusOK {
defer resp.Body.Close()
return nil, ReadBodyAsError(resp)
}
return resp.Body, nil
}