github.com/pyroscope-io/godeltaprof@v0.1.3-0.20230906152420-0d7eeca7b8c1/http/pprof/pprof.go (about)

     1  package pprof
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/pyroscope-io/godeltaprof"
     6  	"io"
     7  	"net/http"
     8  	"runtime"
     9  	"strconv"
    10  )
    11  
    12  var deltaHeapProfiler = godeltaprof.NewHeapProfiler()
    13  var deltaBlockProfiler = godeltaprof.NewBlockProfiler()
    14  var deltaMutexProfiler = godeltaprof.NewMutexProfiler()
    15  
    16  type deltaProfiler interface {
    17  	Profile(w io.Writer) error
    18  }
    19  
    20  func init() {
    21  	http.HandleFunc("/debug/pprof/delta_heap", Heap)
    22  	http.HandleFunc("/debug/pprof/delta_block", Block)
    23  	http.HandleFunc("/debug/pprof/delta_mutex", Mutex)
    24  }
    25  
    26  func Heap(w http.ResponseWriter, r *http.Request) {
    27  	gc, _ := strconv.Atoi(r.FormValue("gc"))
    28  	if gc > 0 {
    29  		runtime.GC()
    30  	}
    31  	writeDeltaProfile(deltaHeapProfiler, "heap", w)
    32  }
    33  
    34  func Block(w http.ResponseWriter, r *http.Request) {
    35  	writeDeltaProfile(deltaBlockProfiler, "block", w)
    36  }
    37  
    38  func Mutex(w http.ResponseWriter, r *http.Request) {
    39  	writeDeltaProfile(deltaMutexProfiler, "mutex", w)
    40  }
    41  
    42  func writeDeltaProfile(p deltaProfiler, name string, w http.ResponseWriter) {
    43  	w.Header().Set("X-Content-Type-Options", "nosniff")
    44  	w.Header().Set("Content-Type", "application/octet-stream")
    45  	w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s.pprof.gz"`, name))
    46  	_ = p.Profile(w)
    47  }