Skip to content

Commit

Permalink
add pprof server
Browse files Browse the repository at this point in the history
  • Loading branch information
mycrEEpy committed Aug 14, 2023
1 parent 036709f commit a1add8b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
19 changes: 19 additions & 0 deletions cmd/pgopher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import (
"context"
"errors"
"flag"
"log/slog"
"net/http"
"net/http/pprof"
"os"
"os/signal"
"syscall"
Expand All @@ -16,6 +19,7 @@ var (
logLevel = flag.String("log-level", "INFO", "log level")
logJsonFormat = flag.Bool("log-json", false, "log in json format")
cfgFile = flag.String("config", "pgopher.yml", "config file")
pprofEnabled = flag.Bool("pprof", false, "enable pprof endpoint")
)

func init() {
Expand Down Expand Up @@ -64,6 +68,21 @@ func main() {

slog.Debug("loaded configuration", slog.String("file", *cfgFile), slog.Any("config", cfg))

if *pprofEnabled {
go func() {
pprofMux := http.NewServeMux()
pprofMux.HandleFunc("/debug/pprof/profile", pprof.Profile)
pprofServer := &http.Server{Addr: cfg.PprofListenAddress, Handler: pprofMux}

slog.Info("starting pprof server", slog.String("listenAddr", cfg.PprofListenAddress))

pprofErr := pprofServer.ListenAndServe()
if pprofErr != nil && !errors.Is(err, http.ErrServerClosed) {
slog.Error("pprof server failed", slog.String("err", pprofErr.Error()))
}
}()
}

err = pgopher.NewServer(cfg).Run(ctx)
if err != nil {
slog.Error("failed to run server", slog.String("err", err.Error()))
Expand Down
12 changes: 7 additions & 5 deletions internal/pgopher/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package pgopher
import "time"

type Config struct {
ListenAddress string `yaml:"listenAddress"`
ProfilingTargets []ProfilingTarget `yaml:"profilingTargets"`
Sink Sink `yaml:"sink"`
ListenAddress string `yaml:"listenAddress"`
PprofListenAddress string `yaml:"pprofListenAddress"`
ProfilingTargets []ProfilingTarget `yaml:"profilingTargets"`
Sink Sink `yaml:"sink"`
}

type ProfilingTarget struct {
Expand All @@ -26,8 +27,9 @@ type FileSinkOptions struct {

func DefaultConfig() Config {
return Config{
ListenAddress: ":8000",
ProfilingTargets: make([]ProfilingTarget, 0),
ListenAddress: ":8000",
PprofListenAddress: "localhost:8010",
ProfilingTargets: make([]ProfilingTarget, 0),
Sink: Sink{
Type: "file",
FileSinkOptions: FileSinkOptions{
Expand Down
10 changes: 6 additions & 4 deletions internal/pgopher/pgopher.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ func (s *Server) Run(ctx context.Context) error {

go s.startScheduler(ctx, wg)

http.HandleFunc("/_ready", readinessProbe)
http.HandleFunc("/_live", livenessProbe)
http.HandleFunc("/api/v1/profile/", s.handleProfile)
mux := http.NewServeMux()

mux.HandleFunc("/_ready", readinessProbe)
mux.HandleFunc("/_live", livenessProbe)
mux.HandleFunc("/api/v1/profile/", s.handleProfile)

httpServer := &http.Server{
Addr: s.cfg.ListenAddress,
Handler: http.DefaultServeMux,
Handler: mux,
}

go func() {
Expand Down
3 changes: 2 additions & 1 deletion pgopher.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
listenAddress: ":8000"
pprofListenAddress: "localhost:8010"
profilingTargets:
- name: test
- name: self
url: http://localhost:8010/debug/pprof/profile
duration: 5s
schedule: "* * * * *"
Expand Down

0 comments on commit a1add8b

Please sign in to comment.