Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Commit

Permalink
Add pprof Debug routes for Sherpa server profiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrasell committed Jan 4, 2020
1 parent d215c85 commit 773329b
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cmd/server/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func RegisterCommand(rootCmd *cobra.Command) error {
serverCfg.RegisterTelemetryConfig(cmd)
serverCfg.RegisterClusterConfig(cmd)
serverCfg.RegisterMetricProviderConfig(cmd)
serverCfg.RegisterDebugConfig(cmd)
logCfg.RegisterConfig(cmd)
rootCmd.AddCommand(cmd)

Expand All @@ -53,6 +54,7 @@ func runServer(_ *cobra.Command, _ []string) {
}

cfg := &server.Config{
Debug: serverCfg.GetDebugEnabled(),
Cluster: &clusterConfig,
MetricProvider: metricProviderConfig,
Server: &serverConfig,
Expand Down
30 changes: 30 additions & 0 deletions pkg/config/server/debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package server

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

const configKeyDebugEnable = "debug-enabled"

// GetDebugEnabled is used to identify whether the operator has enabled the server debug API
// endpoints.
func GetDebugEnabled() bool { return viper.GetBool(configKeyDebugEnable) }

// RegisterDebugConfig registers the CLI flags used to alter the server debug API endpoints.
func RegisterDebugConfig(cmd *cobra.Command) {
flags := cmd.PersistentFlags()

{
const (
key = configKeyDebugEnable
longOpt = "debug-enabled"
defaultValue = false
description = "Specifies if the debugging HTTP endpoints should be enabled"
)

flags.Bool(longOpt, defaultValue, description)
_ = viper.BindPFlag(key, flags.Lookup(longOpt))
viper.SetDefault(key, defaultValue)
}
}
15 changes: 15 additions & 0 deletions pkg/config/server/debug_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package server

import (
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func Test_DebugConfig(t *testing.T) {
fakeCMD := &cobra.Command{}
RegisterDebugConfig(fakeCMD)
cfg := GetClusterConfig()
assert.False(t, GetDebugEnabled(), cfg.Addr)
}
15 changes: 15 additions & 0 deletions pkg/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
)

type Config struct {
Debug bool
Cluster *serverCfg.ClusterConfig
MetricProvider *serverCfg.MetricProviderConfig
Server *serverCfg.Config
Expand Down Expand Up @@ -59,3 +60,17 @@ const (
routeSystemInfoName = "GetSystemInfo"
routeSystemInfoPattern = "/v1/system/info"
)

// Debug server routes.
const (
routeGetDebugPPROFName = "GetDebugPPROF"
routeGetDebugPPROFPattern = "/debug/pprof/"
routeGetDebugPPROFCMDLineName = "GetDebugPPROFCMDLine"
routeGetDebugPPROFCMDLinePattern = "/debug/pprof/cmdline"
routeGetDebugPPROFProfileName = "GetDebugPPROFProfile"
routeGetDebugPPROFProfilePattern = "/debug/pprof/profile"
routeGetDebugPPROFSymbolName = "GetDebugPPROFSymbol"
routeGetDebugPPROFSymbolPattern = "/debug/pprof/symbol"
routeGetDebugPPROFTraceName = "GetDebugPPROFTrace"
routeGetDebugPPROFTracePattern = "/debug/pprof/trace"
)
44 changes: 44 additions & 0 deletions pkg/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package server

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

policyV1 "github.com/jrasell/sherpa/pkg/policy/v1"
scaleV1 "github.com/jrasell/sherpa/pkg/scale/v1"
Expand Down Expand Up @@ -33,6 +34,12 @@ func (h *HTTPServer) setupRoutes() *router.RouteTable {
policyRoutes := h.setupPolicyRoutes()
r = append(r, policyRoutes)

// Setup the server debug routes if enabled.
if h.cfg.Debug {
debugRoutes := h.setupDebugRoutes()
r = append(r, debugRoutes)
}

// Setup the UI routes if it is enabled.
if h.cfg.Server.UI {
uiRoutes := h.setupUIRoutes()
Expand Down Expand Up @@ -213,3 +220,40 @@ func (h *HTTPServer) setupAPIPolicyRoutes() []router.Route {
},
}
}

func (h *HTTPServer) setupDebugRoutes() []router.Route {
h.logger.Debug().Msg("setting up server Debug routes")

return router.Routes{
router.Route{
Name: routeGetDebugPPROFName,
Method: http.MethodGet,
Pattern: routeGetDebugPPROFPattern,
HandlerFunc: pprof.Index,
},
router.Route{
Name: routeGetDebugPPROFCMDLineName,
Method: http.MethodGet,
Pattern: routeGetDebugPPROFCMDLinePattern,
HandlerFunc: pprof.Cmdline,
},
router.Route{
Name: routeGetDebugPPROFProfileName,
Method: http.MethodGet,
Pattern: routeGetDebugPPROFProfilePattern,
HandlerFunc: pprof.Profile,
},
router.Route{
Name: routeGetDebugPPROFSymbolName,
Method: http.MethodGet,
Pattern: routeGetDebugPPROFSymbolPattern,
HandlerFunc: pprof.Symbol,
},
router.Route{
Name: routeGetDebugPPROFTraceName,
Method: http.MethodGet,
Pattern: routeGetDebugPPROFTracePattern,
HandlerFunc: pprof.Trace,
},
}
}

0 comments on commit 773329b

Please sign in to comment.