Skip to content

Commit

Permalink
Merge pull request #1764 from mysteriumnetwork/pprof-flag
Browse files Browse the repository at this point in the history
Add flag to enable pprof http endpoints
  • Loading branch information
soffokl committed Feb 25, 2020
2 parents 7efd6bd + d947451 commit 5773169
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cmd/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/mysteriumnetwork/node/communication/nats"
nats_dialog "github.com/mysteriumnetwork/node/communication/nats/dialog"
nats_discovery "github.com/mysteriumnetwork/node/communication/nats/discovery"
"github.com/mysteriumnetwork/node/config"
appconfig "github.com/mysteriumnetwork/node/config"
"github.com/mysteriumnetwork/node/consumer/bandwidth"
consumer_session "github.com/mysteriumnetwork/node/consumer/session"
Expand Down Expand Up @@ -564,6 +565,11 @@ func (di *Dependencies) bootstrapTequilapi(nodeOptions node.Options, listener ne
tequilapi_endpoints.AddRoutesForFeedback(router, di.Reporter)
tequilapi_endpoints.AddRoutesForConnectivityStatus(router, di.SessionConnectivityStatusStorage)
identity_registry.AddIdentityRegistrationEndpoint(router, di.IdentityRegistry)

if config.GetBool(config.FlagPProfEnable) {
tequilapi_endpoints.AddRoutesForPProf(router)
}

corsPolicy := tequilapi.NewMysteriumCorsPolicy()
return tequilapi.NewServer(listener, router, corsPolicy)
}
Expand Down
8 changes: 8 additions & 0 deletions config/flags_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ var (
Usage: "Port for listening incoming api requests",
Value: 4050,
}
// FlagPProfEnable enables pprof via TequilAPI.
FlagPProfEnable = cli.BoolFlag{
Name: "pprof.enable",
Usage: "Enables pprof",
Value: false,
}
// FlagUIEnable enables built-in web UI for node.
FlagUIEnable = cli.BoolFlag{
Name: "ui.enable",
Expand Down Expand Up @@ -195,6 +201,7 @@ func RegisterFlagsNode(flags *[]cli.Flag) error {
&FlagTequilapiAddress,
&FlagTequilapiPort,
&FlagUIEnable,
&FlagPProfEnable,
&FlagUIPort,
&FlagVendorID,
)
Expand Down Expand Up @@ -229,6 +236,7 @@ func ParseFlagsNode(ctx *cli.Context) {
Current.ParseStringFlag(ctx, FlagQualityType)
Current.ParseStringFlag(ctx, FlagTequilapiAddress)
Current.ParseIntFlag(ctx, FlagTequilapiPort)
Current.ParseBoolFlag(ctx, FlagPProfEnable)
Current.ParseBoolFlag(ctx, FlagUIEnable)
Current.ParseIntFlag(ctx, FlagUIPort)
Current.ParseStringFlag(ctx, FlagVendorID)
Expand Down
46 changes: 46 additions & 0 deletions tequilapi/endpoints/pprof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (C) 2020 The "MysteriumNetwork/node" Authors.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package endpoints

import (
"net/http"
"net/http/pprof"

"github.com/julienschmidt/httprouter"
)

// AddRoutesForPProf adds pprof http handlers to given router
func AddRoutesForPProf(router *httprouter.Router) {
router.GET("/debug/pprof/", pprofHandler)
router.GET("/debug/pprof/:profile", pprofHandler)
}

func pprofHandler(w http.ResponseWriter, r *http.Request, params httprouter.Params) {
switch params.ByName("profile") {
case "cmdline":
pprof.Cmdline(w, r)
case "profile":
pprof.Profile(w, r)
case "symbol":
pprof.Symbol(w, r)
case "trace":
pprof.Trace(w, r)
default:
pprof.Index(w, r)
}
}

0 comments on commit 5773169

Please sign in to comment.