Skip to content

Commit

Permalink
added the feature to profiling application using pprof
Browse files Browse the repository at this point in the history
  • Loading branch information
christiangda committed Sep 13, 2020
1 parent 9b775b5 commit 4210c10
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ VOLUME [ "/home/nobody", "/etc/mq-to-db"]
USER nobody
WORKDIR ${HOME}

#ENTRYPOINT [ "/bin/mq-to-db" ]
CMD [ "/bin/mq-to-db" ]
ENTRYPOINT [ "/bin/mq-to-db" ]
#CMD [ "/bin/mq-to-db" ]
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This is a [Golang (go)](https://golang.org/) program to read from a Message Queu

* The number of queue consumers could be different from the numbers of storage workers
* The process (job) of consume one message from queue and store into the database is synchronous because every message needs to be acknowledge (confirm as storage).
* pprof enabled via --profile cmd line
* Prometheus metrics for consumers, storage workers, go statistics and database.
* Grafana dashboard for prometheus metrics
* Dockerfile multi-stage build
Expand Down Expand Up @@ -80,6 +81,15 @@ make
./mq-to-db --configFile config-sample.yaml
```

### http endpoint

The application expose different endpoints via http server

* http://localhost:8080/
* http://localhost:8080/metrics
* http://localhost:8080/health
* http://localhost:8080/debug/pprof

## docker-compose

### Up
Expand All @@ -94,6 +104,20 @@ docker-compose up --build
docker-compose down -v
```

### Profiling

```bash
# terminal 1, for mq-to-db-01 inside the docker-compose-file
go tool pprof http://127.0.0.1:8080/debug/pprof/goroutine

# terminal 2, for mq-to-db-02 inside the docker-compose-file
go tool pprof http://127.0.0.1:8081/debug/pprof/goroutine


# once you are into tool pprof, execute the command web
(pprof) web
```

### Links

* [mq-to-db-01 home page](http://localhost:8080/)
Expand Down
25 changes: 25 additions & 0 deletions cmd/mq-to-db/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"os/signal"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -80,6 +81,7 @@ func init() { // package initializer
flag.DurationVar(&conf.Server.ShutdownTimeout, "server.shutdownTimeout", 30*time.Second, "Server ShutdownTimeout")
flag.BoolVar(&conf.Server.KeepAlivesEnabled, "server.keepAlivesEnabled", true, "Server KeepAlivesEnabled")
flag.BoolVar(&conf.Server.Debug, "debug", false, "debug")
flag.BoolVar(&conf.Server.Profile, "profile", false, "Enable program profile")
flag.StringVar(&conf.Server.LogFormat, "logFormat", "text", "Log Format [text|json] ")
// Application conf flags
flag.StringVar(&conf.Application.ConfigFile, "configFile", "config", "Configuration file")
Expand Down Expand Up @@ -379,6 +381,20 @@ func main() {
// health check handler
mux.HandleFunc(conf.Application.HealthPath, HealthCheck)

// Profilling endpoints whe -profile or --profile
if conf.Server.Profile {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/heap", pprof.Index)
mux.HandleFunc("/debug/pprof/mutex", pprof.Index)
mux.HandleFunc("/debug/pprof/goroutine", pprof.Index)
mux.HandleFunc("/debug/pprof/threadcreate", pprof.Index)
mux.HandleFunc("/debug/pprof/block", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
}

// http server conf
httpServer := &http.Server{
ReadTimeout: conf.Server.ReadTimeout,
Expand Down Expand Up @@ -619,12 +635,19 @@ func HomePage(w http.ResponseWriter, r *http.Request) {
<li><a href="{{.MetricsPath}}">{{.MetricsPath}}</a></li>
<li><a href="{{.HealthPath}}">{{.HealthPath}}</a></li>
</ul>
<h2>Version</h2>
<ul>
<li>{{.VersionInfo}}</li>
<li>{{.BuildInfo}}</li>
</ul>
<h2>Go profile is enabled</h2>
<ul>
<li><a href="{{.ProfileLink}}">{{.ProfileLink}}</a></li>
</ul>
<h3><a href="https://prometheus.io/">If you want to know more about Metrics and Exporters go to https://prometheus.io</a></h3>
</body>
</html>
Expand All @@ -638,6 +661,7 @@ func HomePage(w http.ResponseWriter, r *http.Request) {
HealthPath string
VersionInfo string
BuildInfo string
ProfileLink string
}{
conf.Application.Name,
conf.Application.Name,
Expand All @@ -647,6 +671,7 @@ func HomePage(w http.ResponseWriter, r *http.Request) {
conf.Application.HealthPath,
conf.Application.VersionInfo,
conf.Application.BuildInfo,
"/debug/pprof/",
}

t := template.Must(template.New("index").Parse(indexHTMLTmpl))
Expand Down
10 changes: 8 additions & 2 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ services:
volumes:
- ./docker-compose/mq-to-db/config.yaml:/home/nobody/config.yaml:Z
environment:
#SERVER_ADDRESS: 0.0.0.0 # means all the ips into the container
SERVER_ADDRESS: 0.0.0.0 # means all the ips into the container
SERVER_PORT: 8080
command:
- --profile
#- --debug
ports:
- 8080:8080
links:
Expand All @@ -123,8 +126,11 @@ services:
volumes:
- ./docker-compose/mq-to-db/config.yaml:/home/nobody/config.yaml
environment:
#SERVER_ADDRESS: 0.0.0.0 # means all the ips into the container
SERVER_ADDRESS: 0.0.0.0 # means all the ips into the container
SERVER_PORT: 8081
command:
- --profile
#- --debug
ports:
- 8081:8081
links:
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Config struct {
KeepAlivesEnabled bool `json:"keepAlivesEnabled" yaml:"keepAlivesEnabled"`
LogFormat string `json:"logFormat" yaml:"logFormat"`
Debug bool `json:"debug" yaml:"debug"`
Profile bool `json:"profile" yaml:"profile"`
}

Dispatcher struct {
Expand Down

0 comments on commit 4210c10

Please sign in to comment.