Skip to content

Commit 9538d09

Browse files
committed
implement pprof to debug
1 parent 86979d1 commit 9538d09

2 files changed

Lines changed: 43 additions & 0 deletions

File tree

cmd/slowql-replayer/main.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"github.com/briandowns/spinner"
1717
"github.com/devops-works/slowql"
18+
"github.com/devops-works/slowql/cmd/slowql-replayer/pprof"
1819
"github.com/olekukonko/tablewriter"
1920
"github.com/sirupsen/logrus"
2021
"golang.org/x/crypto/ssh/terminal"
@@ -30,6 +31,7 @@ type options struct {
3031
kind string
3132
database string
3233
loglvl string
34+
pprof string
3335
workers int
3436
usePass bool
3537
dryRun bool
@@ -61,6 +63,7 @@ func main() {
6163
flag.StringVar(&opt.kind, "k", "", "Kind of the database (mysql, mariadb...)")
6264
flag.StringVar(&opt.database, "db", "", "Name of the database to use")
6365
flag.StringVar(&opt.loglvl, "l", "info", "Logging level")
66+
flag.StringVar(&opt.pprof, "pprof", "", "pprof server address")
6467
flag.IntVar(&opt.workers, "w", 100, "Number of maximum simultaneous connections to database")
6568
flag.BoolVar(&opt.usePass, "p", false, "Use a password to connect to database")
6669
flag.BoolVar(&opt.dryRun, "dry", false, "Replay the requests but don't write in the database")
@@ -84,6 +87,15 @@ func main() {
8487
}
8588
db.logger.Debugf("file %s successfully opened", opt.file)
8689

90+
if opt.pprof != "" {
91+
pprofServer, err := pprof.New(opt.pprof)
92+
if err != nil {
93+
db.logger.Fatalf("unable to create pprof server: %s", err)
94+
}
95+
go pprofServer.Run()
96+
db.logger.Infof("pprof started on 'http://%s'", pprofServer.Addr)
97+
}
98+
8799
r, err := db.replay(f)
88100
if err != nil {
89101
logrus.Fatalf("cannot replay %s: %s", opt.kind, err)

cmd/slowql-replayer/pprof/pprof.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package pprof
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/sirupsen/logrus"
8+
9+
// Import and mount pprof
10+
_ "net/http/pprof"
11+
)
12+
13+
// Server holds pprof server required informations
14+
type Server struct {
15+
http.Server
16+
}
17+
18+
// New returns a pprof server instance
19+
func New(addr string) (*Server, error) {
20+
s := Server{}
21+
s.ReadTimeout = time.Second
22+
s.Addr = addr
23+
return &s, nil
24+
}
25+
26+
// Run an independent pprof server
27+
func (p *Server) Run() {
28+
if err := p.ListenAndServe(); err != nil {
29+
logrus.Fatalf("error running pprof server: %s", err)
30+
}
31+
}

0 commit comments

Comments
 (0)