Skip to content

Commit

Permalink
Merge pull request #64 from topfreegames/chore/add-go-stats-reporter
Browse files Browse the repository at this point in the history
chore: add go stats reporter and pprof server
  • Loading branch information
fbgava-wildlife authored Dec 6, 2024
2 parents 3110398 + 46f5a36 commit c749415
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ vendor/
*.swo
.*swp

.vscode
.idea

.tool-versions
4 changes: 4 additions & 0 deletions config/default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ stats:
host: "localhost:8125"
prefix: "push"
buflen: 1
pprof:
enabled: false
host: "127.0.0.1"
port: 8081
invalidToken:
pg:
table: "test_apns"
Expand Down
4 changes: 4 additions & 0 deletions config/docker_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ stats:
host: "statsd:8125"
prefix: "push"
flushIntervalMs: 5000
pprof:
enabled: false
host: "127.0.0.1"
port: 8081
invalidToken:
handlers:
- pg
Expand Down
6 changes: 5 additions & 1 deletion config/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ stats:
statsd:
host: "localhost:40001"
prefix: "push"
flushIntervalMs: 5000
flushIntervalMs:
pprof:
enabled: false
host: "127.0.0.1"
port: 8081
invalidToken:
handlers:
- pg
Expand Down
50 changes: 49 additions & 1 deletion feedback/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ package feedback
import (
"context"
"fmt"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"runtime"
"sync"
"syscall"
"time"
Expand Down Expand Up @@ -75,6 +78,10 @@ func NewListener(
func (l *Listener) loadConfigurationDefaults() {
l.Config.SetDefault("feedbackListeners.gracefulShutdownTimeout", 1)
l.Config.SetDefault("stats.flush.s", 5)

l.Config.SetDefault("pprof.enabled", false)
l.Config.SetDefault("pprof.host", "127.0.0.1")
l.Config.SetDefault("pprof.port", 8081)
}

func (l *Listener) configure(statsdClientrOrNil interfaces.StatsDClient) error {
Expand Down Expand Up @@ -128,14 +135,20 @@ func (l *Listener) Start() {
)
log.Info("starting the feedback listener...")

if l.Config.GetBool("pprof.enabled") {
go l.StartPprofServer()
}

go l.Queue.ConsumeLoop(context.Background())
l.Broker.Start()
l.InvalidTokenHandler.Start()

go l.reportGoStats()

statsReporterReportMetricCount(l.StatsReporters,
"feedback_listener_restart", 1, "", "")

sigchan := make(chan os.Signal)
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

flushTicker := time.NewTicker(l.statsFlushTime)
Expand Down Expand Up @@ -172,6 +185,23 @@ func (l *Listener) flushStats() {
"invalid_token_handler_buffer", bufferSize, "", "")
}

func (l *Listener) reportGoStats() {
for {
num := runtime.NumGoroutine()
m := &runtime.MemStats{}
runtime.ReadMemStats(m)
gcTime := m.PauseNs[(m.NumGC+255)%256]
for _, statsReporter := range l.StatsReporters {
statsReporter.ReportGoStats(
num,
m.Alloc, m.HeapObjects, m.NextGC,
gcTime,
)
}
time.Sleep(30 * time.Second)
}
}

// Cleanup ends the Listener execution
func (l *Listener) Cleanup() {
l.flushStats()
Expand Down Expand Up @@ -217,3 +247,21 @@ func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
return true // timed out
}
}

func (l *Listener) StartPprofServer() error {
mux := http.NewServeMux()

mux.HandleFunc("/debug/pprof/", 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)

addr := fmt.Sprintf("%s:%d", l.Config.GetString("pprof.host"), l.Config.GetInt("pprof.port"))
server := &http.Server{
Addr: addr,
Handler: mux,
}

return server.ListenAndServe()
}
2 changes: 1 addition & 1 deletion pusher/pusher.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (p *Pusher) Start(ctx context.Context) {
go p.Queue.ConsumeLoop(ctx)
go p.reportGoStats()

sigchan := make(chan os.Signal)
sigchan := make(chan os.Signal, 1)
signal.Notify(sigchan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

select {
Expand Down

0 comments on commit c749415

Please sign in to comment.