-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathprofiled.go
85 lines (70 loc) · 2.36 KB
/
profiled.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
// Copyright (c) The EfficientGo Authors.
// Licensed under the Apache License 2.0.
package e2eprof
import (
"github.com/efficientgo/core/errors"
"github.com/efficientgo/e2e"
"github.com/efficientgo/e2e/profiling/parcaconfig"
)
type Target struct {
Name string // Represents runnable job and will be used for scrape job name.
InternalEndpoint string
Scheme string // "http" by default.
Config *parcaconfig.ProfilingConfig
}
type Profiled interface {
ProfileTargets() []Target
}
// ProfiledRunnable represents runnable with pprof HTTP handlers exposed.
type ProfiledRunnable struct {
e2e.Runnable
pprofPort string
scheme string
config *parcaconfig.ProfilingConfig
}
type rOpt struct {
config *parcaconfig.ProfilingConfig
scheme string
}
// WithProfiledConfig sets a custom parca ProfilingConfig entry about this runnable. Empty by default (Parca defaults apply).
func WithProfiledConfig(config parcaconfig.ProfilingConfig) ProfiledOption {
return func(o *rOpt) {
o.config = &config
}
}
// WithProfiledScheme allows adding customized scheme. "http" or "https" values allowed. "http" by default.
// If "https" is specified, insecure TLS will be performed.
func WithProfiledScheme(scheme string) ProfiledOption {
return func(o *rOpt) {
o.scheme = scheme
}
}
type ProfiledOption func(*rOpt)
// AsProfiled wraps e2e.Runnable with ProfiledRunnable.
// If runnable is running during invocation AsProfiled panics.
// NOTE(bwplotka): Caller is expected to discard passed `r` runnable and use returned ProfiledRunnable.Runnable instead.
func AsProfiled(r e2e.Runnable, pprofPortName string, opts ...ProfiledOption) *ProfiledRunnable {
if r.IsRunning() {
panic("can't use AsProfiled with running runnable")
}
opt := rOpt{
scheme: "http",
}
for _, o := range opts {
o(&opt)
}
if r.InternalEndpoint(pprofPortName) == "" {
return &ProfiledRunnable{Runnable: e2e.NewFailedRunnable(r.Name(), errors.Newf("pprof port name %v does not exist in given runnable ports", pprofPortName))}
}
instr := &ProfiledRunnable{
Runnable: r,
pprofPort: pprofPortName,
scheme: opt.scheme,
config: opt.config,
}
r.SetMetadata(metaKey, Profiled(instr))
return instr
}
func (r *ProfiledRunnable) ProfileTargets() []Target {
return []Target{{Name: r.Name(), Scheme: r.scheme, Config: r.config, InternalEndpoint: r.InternalEndpoint(r.pprofPort)}}
}