github.com/grafana/pyroscope-go/godeltaprof@v0.1.8-0.20240513050943-1b1f97373e2a/http/pprof/pprof.go (about)

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