Skip to content

Commit 2e6028d

Browse files
authored
Improve pprof in api service (#727)
* Disable stripping binary symbols * use separate port for pprof
1 parent c473895 commit 2e6028d

File tree

3 files changed

+29
-21
lines changed

3 files changed

+29
-21
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build go mod download
99

1010
# Now adding all the code and start building
1111
ADD . .
12-
RUN --mount=type=cache,target=/root/.cache/go-build GOOS=linux go build -trimpath -ldflags "-s -X cmd.Version=$VERSION -X main.Version=$VERSION -linkmode external -extldflags '-static'" -v -o mev-boost-relay .
12+
RUN --mount=type=cache,target=/root/.cache/go-build GOOS=linux go build -trimpath -ldflags "-X cmd.Version=$VERSION -X main.Version=$VERSION" -v -o mev-boost-relay .
1313

1414
FROM alpine
1515
RUN apk add --no-cache libstdc++ libc6-compat

cmd/api.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,24 +24,24 @@ var (
2424
apiDefaultSecretKey = common.GetEnv("SECRET_KEY", "")
2525
apiDefaultLogTag = os.Getenv("LOG_TAG")
2626

27-
apiDefaultPprofEnabled = os.Getenv("PPROF") == "1"
27+
apiDefaultPprofListenAddr = os.Getenv("PPROF_ADDR")
2828
apiDefaultInternalAPIEnabled = os.Getenv("ENABLE_INTERNAL_API") == "1"
2929

3030
// Default Builder, Data, and Proposer API as true.
3131
apiDefaultBuilderAPIEnabled = os.Getenv("DISABLE_BUILDER_API") != "1"
3232
apiDefaultDataAPIEnabled = os.Getenv("DISABLE_DATA_API") != "1"
3333
apiDefaultProposerAPIEnabled = os.Getenv("DISABLE_PROPOSER_API") != "1"
3434

35-
apiListenAddr string
36-
apiPprofEnabled bool
37-
apiSecretKey string
38-
apiBlockSimURL string
39-
apiDebug bool
40-
apiBuilderAPI bool
41-
apiDataAPI bool
42-
apiInternalAPI bool
43-
apiProposerAPI bool
44-
apiLogTag string
35+
apiListenAddr string
36+
apiPprofListenAddr string
37+
apiSecretKey string
38+
apiBlockSimURL string
39+
apiDebug bool
40+
apiBuilderAPI bool
41+
apiDataAPI bool
42+
apiInternalAPI bool
43+
apiProposerAPI bool
44+
apiLogTag string
4545
)
4646

4747
func init() {
@@ -63,7 +63,8 @@ func init() {
6363
apiCmd.Flags().StringVar(&apiBlockSimURL, "blocksim", apiDefaultBlockSim, "URL for block simulator")
6464
apiCmd.Flags().StringVar(&network, "network", defaultNetwork, "Which network to use")
6565

66-
apiCmd.Flags().BoolVar(&apiPprofEnabled, "pprof", apiDefaultPprofEnabled, "enable pprof API")
66+
apiCmd.Flags().StringVar(&apiPprofListenAddr, "pprof-listen-addr", apiDefaultPprofListenAddr, "listen address for pprof, empty to disable")
67+
6768
apiCmd.Flags().BoolVar(&apiBuilderAPI, "builder-api", apiDefaultBuilderAPIEnabled, "enable builder API (/builder/...)")
6869
apiCmd.Flags().BoolVar(&apiDataAPI, "data-api", apiDefaultDataAPIEnabled, "enable data API (/data/...)")
6970
apiCmd.Flags().BoolVar(&apiInternalAPI, "internal-api", apiDefaultInternalAPIEnabled, "enable internal API (/internal/...)")
@@ -165,11 +166,12 @@ var apiCmd = &cobra.Command{
165166
EthNetDetails: *networkInfo,
166167
BlockSimURL: apiBlockSimURL,
167168

169+
PprofListenAddr: apiPprofListenAddr,
170+
168171
BlockBuilderAPI: apiBuilderAPI,
169172
DataAPI: apiDataAPI,
170173
InternalAPI: apiInternalAPI,
171174
ProposerAPI: apiProposerAPI,
172-
PprofAPI: apiPprofEnabled,
173175
}
174176

175177
// Decode the private key

services/api/service.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,12 @@ type RelayAPIOpts struct {
143143
// Network specific variables
144144
EthNetDetails common.EthNetworkDetails
145145

146+
PprofListenAddr string
147+
146148
// APIs to enable
147149
ProposerAPI bool
148150
BlockBuilderAPI bool
149151
DataAPI bool
150-
PprofAPI bool
151152
InternalAPI bool
152153
}
153154

@@ -383,12 +384,6 @@ func (api *RelayAPI) getRouter() http.Handler {
383384
r.HandleFunc(pathDataValidatorRegistration, api.handleDataValidatorRegistration).Methods(http.MethodGet)
384385
}
385386

386-
// Pprof
387-
if api.opts.PprofAPI {
388-
api.log.Info("pprof API enabled")
389-
r.PathPrefix("/debug/pprof/").Handler(http.DefaultServeMux)
390-
}
391-
392387
// /internal/...
393388
if api.opts.InternalAPI {
394389
api.log.Info("internal API enabled")
@@ -516,6 +511,17 @@ func (api *RelayAPI) StartServer() (err error) {
516511
}
517512
}()
518513

514+
if api.opts.PprofListenAddr != "" {
515+
api.log.Info("pprof API is listening on", api.opts.PprofListenAddr)
516+
go func() {
517+
//nolint:gosec // we should not expose pprof externally anyway
518+
err := http.ListenAndServe(api.opts.PprofListenAddr, http.DefaultServeMux)
519+
if err != nil && !errors.Is(err, http.ErrServerClosed) {
520+
api.log.WithError(err).Fatal("failed to start pprof API")
521+
}
522+
}()
523+
}
524+
519525
// create and start HTTP server
520526
api.srv = &http.Server{
521527
Addr: api.opts.ListenAddr,

0 commit comments

Comments
 (0)