Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pprof to flintlock #504

Merged
merged 1 commit into from
Aug 18, 2022
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
8 changes: 8 additions & 0 deletions internal/command/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
tlsKeyFlag = "tls-key"
tlsClientValidateFlag = "tls-client-validate"
tlsClientCAFlag = "tls-client-ca"
debugEndpointFlag = "debug-endpoint"
)

// AddGRPCServerFlagsToCommand will add gRPC server flags to the supplied command.
Expand Down Expand Up @@ -178,3 +179,10 @@ func AddContainerDFlagsToCommand(cmd *cobra.Command, cfg *config.Config) {
defaults.ContainerdNamespace,
"The name of the containerd namespace to use.")
}

func AddDebugFlagsToCommand(cmd *cobra.Command, cfg *config.Config) {
cmd.Flags().StringVar(&cfg.DebugEndpoint,
debugEndpointFlag,
"",
"The endpoint for the debug web server to listen on. It must include a port (e.g. localhost:10500). An empty string means disable the debug endpoint.")
}
46 changes: 46 additions & 0 deletions internal/command/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (
"os"
"os/signal"
"sync"
"time"

_ "net/http/pprof"

grpc_mw "github.com/grpc-ecosystem/go-grpc-middleware"
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
Expand Down Expand Up @@ -59,6 +62,7 @@ func NewCommand(cfg *config.Config) (*cobra.Command, error) {
cmdflags.AddTLSFlagsToCommand(cmd, cfg)
cmdflags.AddContainerDFlagsToCommand(cmd, cfg)
cmdflags.AddFirecrackerFlagsToCommand(cmd, cfg)
cmdflags.AddDebugFlagsToCommand(cmd, cfg)

if err := cmdflags.AddNetworkFlagsToCommand(cmd, cfg); err != nil {
return nil, fmt.Errorf("adding network flags to run command: %w", err)
Expand All @@ -81,6 +85,18 @@ func runServer(ctx context.Context, cfg *config.Config) error {
wg := &sync.WaitGroup{}
ctx, cancel := context.WithCancel(log.WithLogger(ctx, logger))

if cfg.DebugEndpoint != "" {
wg.Add(1)

go func() {
defer wg.Done()

if err := runPProf(ctx, cfg); err != nil {
logger.Errorf("failed serving api: %v", err)
}
}()
}

if !cfg.DisableAPI {
wg.Add(1)

Expand Down Expand Up @@ -225,3 +241,33 @@ func generateOpts(ctx context.Context, cfg *config.Config) ([]grpc.ServerOption,

return opts, nil
}

func runPProf(ctx context.Context, cfg *config.Config) error {
logger := log.GetLogger(ctx)
logger.Warnf("Debug endpoint is ENABLED at %s", cfg.DebugEndpoint)

srv := &http.Server{
Addr: cfg.DebugEndpoint,
Handler: http.DefaultServeMux,
}

go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatalf("starting debug endpoint: %v", err)
}
}()

<-ctx.Done()
logger.Debug("Exiting")

shutDownCtx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer func() {
cancel()
}()

if err := srv.Shutdown(shutDownCtx); err != nil {
logger.Warnf("Debug server shutdown failed:%+v", err)
}

return nil
}
4 changes: 3 additions & 1 deletion internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Config struct {
Logging log.Config
// GRPCEndpoint is the endpoint for the gRPC server.
GRPCAPIEndpoint string
// HTTPAPIEndpoint is the endpoint for the HTTP proxy for the gRPC service..
// HTTPAPIEndpoint is the endpoint for the HTTP proxy for the gRPC service
HTTPAPIEndpoint string
// FirecrackerBin is the firecracker binary to use.
FirecrackerBin string
Expand Down Expand Up @@ -46,6 +46,8 @@ type Config struct {
BasicAuthToken string
// TLS holds the TLS related configuration.
TLS TLSConfig
// DebugEndpoint is the endpoint for the debug web server. An empty string means disable the debug endpoint.
DebugEndpoint string
}

// TLSConfig holds the configuration for TLS.
Expand Down