Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

Commit

Permalink
pprof HTTP (#193)
Browse files Browse the repository at this point in the history
* uses net/http/pprof to help expose debug info
* will be used to help diagnose memory growth
  • Loading branch information
pingles authored Dec 3, 2018
1 parent 8c6470f commit 4bee061
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cmd/kiam/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ package main
import (
"context"
"fmt"
"time"

log "github.com/sirupsen/logrus"
"github.com/uswitch/kiam/pkg/pprof"
"github.com/uswitch/kiam/pkg/prometheus"
"github.com/uswitch/kiam/pkg/statsd"
"time"
)

type logOptions struct {
Expand Down Expand Up @@ -56,6 +56,7 @@ type telemetryOptions struct {
statsDPrefix string
prometheusListen string
prometheusSync time.Duration
pprofListen string
}

func (o *telemetryOptions) bind(parser parser) {
Expand All @@ -65,6 +66,8 @@ func (o *telemetryOptions) bind(parser parser) {

parser.Flag("prometheus-listen-addr", "Prometheus HTTP listen address. e.g. localhost:9620").StringVar(&o.prometheusListen)
parser.Flag("prometheus-sync-interval", "How frequently to update Prometheus metrics").Default("5s").DurationVar(&o.prometheusSync)

parser.Flag("pprof-listen-addr", "Address to bind pprof HTTP server. e.g. localhost:9990").Default("").StringVar(&o.pprofListen)
}

func (o telemetryOptions) start(ctx context.Context, identifier string) {
Expand All @@ -82,6 +85,12 @@ func (o telemetryOptions) start(ctx context.Context, identifier string) {
metrics := prometheus.NewServer(identifier, o.prometheusListen, o.prometheusSync)
metrics.Listen(ctx)
}

if o.pprofListen != "" {
log.Infof("pprof listen address specified, will listen on %s", o.pprofListen)
server := pprof.NewServer(o.pprofListen)
go pprof.ListenAndWait(ctx, server)
}
}

type tlsOptions struct {
Expand Down
26 changes: 26 additions & 0 deletions pkg/pprof/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pprof

import (
"context"
log "github.com/sirupsen/logrus"
"net/http"
_ "net/http/pprof"
)

func NewServer(listenAddr string) http.Server {
server := http.Server{Addr: listenAddr, Handler: http.DefaultServeMux}
return server
}

// Starts server and shuts down when context signals
func ListenAndWait(ctx context.Context, server http.Server) {
go func() {
err := server.ListenAndServe()
if err != nil {
log.Errorf("error starting pprof http server: %s", err.Error())
}
}()
<-ctx.Done()
log.Infof("shutting down pprof server")
server.Shutdown(context.Background())
}

0 comments on commit 4bee061

Please sign in to comment.