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

Commit 4bee061

Browse files
authored
pprof HTTP (#193)
* uses net/http/pprof to help expose debug info * will be used to help diagnose memory growth
1 parent 8c6470f commit 4bee061

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

cmd/kiam/opts.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ package main
1616
import (
1717
"context"
1818
"fmt"
19-
"time"
20-
2119
log "github.com/sirupsen/logrus"
20+
"github.com/uswitch/kiam/pkg/pprof"
2221
"github.com/uswitch/kiam/pkg/prometheus"
2322
"github.com/uswitch/kiam/pkg/statsd"
23+
"time"
2424
)
2525

2626
type logOptions struct {
@@ -56,6 +56,7 @@ type telemetryOptions struct {
5656
statsDPrefix string
5757
prometheusListen string
5858
prometheusSync time.Duration
59+
pprofListen string
5960
}
6061

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

6667
parser.Flag("prometheus-listen-addr", "Prometheus HTTP listen address. e.g. localhost:9620").StringVar(&o.prometheusListen)
6768
parser.Flag("prometheus-sync-interval", "How frequently to update Prometheus metrics").Default("5s").DurationVar(&o.prometheusSync)
69+
70+
parser.Flag("pprof-listen-addr", "Address to bind pprof HTTP server. e.g. localhost:9990").Default("").StringVar(&o.pprofListen)
6871
}
6972

7073
func (o telemetryOptions) start(ctx context.Context, identifier string) {
@@ -82,6 +85,12 @@ func (o telemetryOptions) start(ctx context.Context, identifier string) {
8285
metrics := prometheus.NewServer(identifier, o.prometheusListen, o.prometheusSync)
8386
metrics.Listen(ctx)
8487
}
88+
89+
if o.pprofListen != "" {
90+
log.Infof("pprof listen address specified, will listen on %s", o.pprofListen)
91+
server := pprof.NewServer(o.pprofListen)
92+
go pprof.ListenAndWait(ctx, server)
93+
}
8594
}
8695

8796
type tlsOptions struct {

pkg/pprof/server.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package pprof
2+
3+
import (
4+
"context"
5+
log "github.com/sirupsen/logrus"
6+
"net/http"
7+
_ "net/http/pprof"
8+
)
9+
10+
func NewServer(listenAddr string) http.Server {
11+
server := http.Server{Addr: listenAddr, Handler: http.DefaultServeMux}
12+
return server
13+
}
14+
15+
// Starts server and shuts down when context signals
16+
func ListenAndWait(ctx context.Context, server http.Server) {
17+
go func() {
18+
err := server.ListenAndServe()
19+
if err != nil {
20+
log.Errorf("error starting pprof http server: %s", err.Error())
21+
}
22+
}()
23+
<-ctx.Done()
24+
log.Infof("shutting down pprof server")
25+
server.Shutdown(context.Background())
26+
}

0 commit comments

Comments
 (0)