package profileinstance import ( "flag" "log" "net/http" "os" "path" "sync" "github.com/go-chi/chi/v5" "github.com/google/pprof/driver" "github.com/rs/xid" "tuxpa.in/a/pprofweb/pkg/flagset" ) type Instance struct { id xid.ID dat []byte handler http.Handler mu sync.RWMutex } func (i *Instance) Id() xid.ID { return i.id } func (i *Instance) startServer() error { file, err := os.CreateTemp("", i.id.String()) if err != nil { return err } file.Write(i.dat) flags := &flagset.Set{ Argz: []string{"-http=localhost.invalid:0", "-no_browser", file.Name()}, } options := &driver.Options{ Flagset: flags, HTTPServer: i.startHTTP, } if err := driver.PProf(options); err != nil { return err } return nil } func (i *Instance) startHTTP(args *driver.HTTPServerArgs) error { r := chi.NewRouter() for pattern, handler := range args.Handlers { jpat := path.Join(pattern) r.Handle(jpat, handler) r.Handle(jpat+"/", handler) } i.handler = r return nil } func (i *Instance) ServeHTTP(w http.ResponseWriter, r *http.Request) { log.Printf("serveInstanceHTTP %s %s", i.id, r.URL.String()) i.handler.ServeHTTP(w, r) } // Mostly copied from https://github.com/google/pprof/blob/master/internal/driver/flags.go type pprofFlags struct { args []string s flag.FlagSet usage []string }