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

Added pprof profiling to monitor heap memory #318

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion build/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ WORKDIR /app
RUN apk add --update --no-cache dumb-init su-exec ca-certificates curl
RUN mkdir -p /app/public

EXPOSE 8081 8082 8083 8084 3000
EXPOSE 8081 8082 8083 8084 3000 8080

RUN chmod +x entrypoint.sh probe.sh
ENTRYPOINT ["/app/entrypoint.sh", "metro"]
20 changes: 19 additions & 1 deletion cmd/service/metro/metro.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import (
configreader "github.com/razorpay/metro/pkg/config"
"github.com/razorpay/metro/pkg/encryption"
"github.com/razorpay/metro/pkg/logger"

"net/http"

// blank import added for testing.
_ "net/http/pprof"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
G108: Profiling endpoint is automatically exposed on /debug/pprof (gosec)

)

const (
Expand All @@ -38,7 +43,7 @@ func isValidComponent(component string) bool {
}

// Init initializes all modules (logger, tracing, config, metro component)
func Init(_ context.Context, env string, componentName string) {
func Init(ctx context.Context, env string, componentName string) {
// componentName validation
ok := isValidComponent(componentName)
if !ok {
Expand Down Expand Up @@ -68,6 +73,8 @@ func Init(_ context.Context, env string, componentName string) {

err = boot.InitMonitoring(env, appConfig.App, appConfig.Sentry, appConfig.Tracing)

setPprofProfiles(ctx, componentName)

if err != nil {
log.Fatalf("error in setting up monitoring : %v", err)
}
Expand Down Expand Up @@ -114,3 +121,14 @@ func Run(ctx context.Context) {

logger.Ctx(ctx).Infow("stopped metro")
}

// sets up pprof profile for perfomance monitoring
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
perfomance is a misspelling of performance (misspell)

func setPprofProfiles(ctx context.Context, componentName string) {
logger.Ctx(ctx).Infow("initialising pprof profiles")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [golangci] reported by reviewdog 🐶
initialising is a misspelling of initializing (misspell)

go func() {
myMux := http.DefaultServeMux
if err := http.ListenAndServe("localhost:8080", myMux); err != nil {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found an HTTP server without TLS. Use 'http.ListenAndServeTLS' instead. See https://golang.org/pkg/net/http/#ListenAndServeTLS for more information.

🔴 Fix or ignore this finding to merge your pull request.
🙈 From go.lang.security.audit.net.use-tls.use-tls.

logger.Ctx(ctx).Fatalw("Error when starting or running %v pprof http server: %v", componentName, err)
}
}()
}