Skip to content

Commit 4668b13

Browse files
feat: parametrize pprof server
1 parent 5363459 commit 4668b13

File tree

3 files changed

+56
-28
lines changed

3 files changed

+56
-28
lines changed

boundary.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ type Config struct {
2121
Logger *slog.Logger
2222
Jailer jail.Jailer
2323
ProxyPort int
24+
PprofEnabled bool
25+
PprofPort int
2426
}
2527

2628
type Boundary struct {
@@ -35,11 +37,13 @@ type Boundary struct {
3537
func New(ctx context.Context, config Config) (*Boundary, error) {
3638
// Create proxy server
3739
proxyServer := proxy.NewProxyServer(proxy.Config{
38-
HTTPPort: config.ProxyPort,
39-
RuleEngine: config.RuleEngine,
40-
Auditor: config.Auditor,
41-
Logger: config.Logger,
42-
TLSConfig: config.TLSConfig,
40+
HTTPPort: config.ProxyPort,
41+
RuleEngine: config.RuleEngine,
42+
Auditor: config.Auditor,
43+
Logger: config.Logger,
44+
TLSConfig: config.TLSConfig,
45+
PprofEnabled: config.PprofEnabled,
46+
PprofPort: config.PprofPort,
4347
})
4448

4549
// Create cancellable context for boundary

cli/cli.go

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ type Config struct {
2929
LogDir string
3030
Unprivileged bool
3131
ProxyPort int64
32+
PprofEnabled bool
33+
PprofPort int64
3234
}
3335

3436
// NewCommand creates and returns the root serpent command
@@ -94,6 +96,19 @@ func BaseCommand() *serpent.Command {
9496
Default: "8080",
9597
Value: serpent.Int64Of(&config.ProxyPort),
9698
},
99+
{
100+
Flag: "pprof",
101+
Env: "BOUNDARY_PPROF",
102+
Description: "Enable pprof profiling server.",
103+
Value: serpent.BoolOf(&config.PprofEnabled),
104+
},
105+
{
106+
Flag: "pprof-port",
107+
Env: "BOUNDARY_PPROF_PORT",
108+
Description: "Set port for pprof profiling server.",
109+
Default: "6060",
110+
Value: serpent.Int64Of(&config.PprofPort),
111+
},
97112
},
98113
Handler: func(inv *serpent.Invocation) error {
99114
args := inv.Args
@@ -203,12 +218,14 @@ func Run(ctx context.Context, config Config, args []string) error {
203218

204219
// Create boundary instance
205220
boundaryInstance, err := boundary.New(ctx, boundary.Config{
206-
RuleEngine: ruleEngine,
207-
Auditor: auditor,
208-
TLSConfig: tlsConfig,
209-
Logger: logger,
210-
Jailer: jailer,
211-
ProxyPort: int(config.ProxyPort),
221+
RuleEngine: ruleEngine,
222+
Auditor: auditor,
223+
TLSConfig: tlsConfig,
224+
Logger: logger,
225+
Jailer: jailer,
226+
ProxyPort: int(config.ProxyPort),
227+
PprofEnabled: config.PprofEnabled,
228+
PprofPort: int(config.PprofPort),
212229
})
213230
if err != nil {
214231
return fmt.Errorf("failed to create boundary instance: %v", err)

proxy/proxy.go

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ type Server struct {
3333

3434
listener net.Listener
3535
pprofServer *http.Server
36+
pprofEnabled bool
37+
pprofPort int
3638
}
3739

3840
// Config holds configuration for the proxy server
3941
type Config struct {
40-
HTTPPort int
41-
RuleEngine rules.Evaluator
42-
Auditor audit.Auditor
43-
Logger *slog.Logger
44-
TLSConfig *tls.Config
42+
HTTPPort int
43+
RuleEngine rules.Evaluator
44+
Auditor audit.Auditor
45+
Logger *slog.Logger
46+
TLSConfig *tls.Config
47+
PprofEnabled bool
48+
PprofPort int
4549
}
4650

4751
// NewProxyServer creates a new proxy server instance
@@ -52,6 +56,8 @@ func NewProxyServer(config Config) *Server {
5256
logger: config.Logger,
5357
tlsConfig: config.TLSConfig,
5458
httpPort: config.HTTPPort,
59+
pprofEnabled: config.PprofEnabled,
60+
pprofPort: config.PprofPort,
5561
}
5662
}
5763

@@ -63,19 +69,20 @@ func (p *Server) Start() error {
6369

6470
p.logger.Info("Starting HTTP proxy with TLS termination", "port", p.httpPort)
6571

66-
p.pprofServer = &http.Server{
67-
Addr: ":6060", // pprof port
68-
Handler: http.DefaultServeMux,
69-
}
70-
71-
// Start pprof server on a different port
72-
go func() {
73-
p.logger.Info("Starting pprof server", "port", 6060)
74-
75-
if err := p.pprofServer.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
76-
p.logger.Error("pprof server error", "error", err)
72+
// Start pprof server if enabled
73+
if p.pprofEnabled {
74+
p.pprofServer = &http.Server{
75+
Addr: fmt.Sprintf(":%d", p.pprofPort),
76+
Handler: http.DefaultServeMux,
7777
}
78-
}()
78+
79+
go func() {
80+
p.logger.Info("Starting pprof server", "port", p.pprofPort)
81+
if err := p.pprofServer.ListenAndServe(); err != nil && errors.Is(err, http.ErrServerClosed) {
82+
p.logger.Error("pprof server error", "error", err)
83+
}
84+
}()
85+
}
7986

8087
var err error
8188
p.listener, err = net.Listen("tcp", fmt.Sprintf(":%d", p.httpPort))

0 commit comments

Comments
 (0)