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

Add pprof Debug routes for Sherpa server profiling. #124

Merged
merged 2 commits into from
Jan 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions docs/configuration/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The Sherpa server can be configured by supplying either CLI flags or using envir
* `--bind-port` (uint16: 8000) - The HTTP server port to bind to.
* `--cluster-advertise-addr` (string: "http://127.0.0.1:8000") - The Sherpa server advertise address used for NAT traversal on HTTP redirects.
* `--cluster-name` (string: "") - Specifies the identifier for the Sherpa cluster.
* `--debug-enabled` (bool: false) - Specifies if the debugging HTTP endpoints should be enabled.
* `--log-format` (string: "auto") - Specify the log format ("auto", "zerolog" or "human").
* `--log-level` (string: "info") - Change the level used for logging.
* `--log-use-color` (bool: true) - Use ANSI colors in logging output.
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,
},
}
}