github.com/anycable/anycable-go@v1.5.1/diagnostics/diagnostics_gops.go (about)

     1  //go:build gops
     2  // +build gops
     3  
     4  package diagnostics
     5  
     6  import (
     7  	"fmt"
     8  	"net/http"
     9  	_ "net/http/pprof"
    10  	"os"
    11  	"runtime"
    12  	"strconv"
    13  
    14  	"log"
    15  
    16  	"github.com/google/gops/agent"
    17  )
    18  
    19  func init() {
    20  	if err := agent.Listen(agent.Options{}); err != nil {
    21  		log.Fatal(err.Error())
    22  	}
    23  
    24  	pprofRequired := false
    25  
    26  	if vals := os.Getenv("BLOCK_PROFILE_RATE"); vals != "" {
    27  		val, err := strconv.Atoi(vals)
    28  
    29  		if err != nil {
    30  			log.Fatalf("Invalid value for block profile rate: %s", vals)
    31  		}
    32  
    33  		runtime.SetBlockProfileRate(val)
    34  		pprofRequired = true
    35  		fmt.Println("[PPROF] Block profiling enabled")
    36  	}
    37  
    38  	if vals := os.Getenv("MUTEX_PROFILE_FRACTION"); vals != "" {
    39  		val, err := strconv.Atoi(vals)
    40  
    41  		if err != nil {
    42  			log.Fatalf("Invalid value for mutex profile fraction: %s", vals)
    43  		}
    44  
    45  		runtime.SetMutexProfileFraction(val)
    46  		pprofRequired = true
    47  		fmt.Println("[PPROF] Mutex profiling enabled")
    48  	}
    49  
    50  	// Run pprof web server as well to be able to capture blocks and mutex profiles
    51  	// (not supported by gops)
    52  	if pprofRequired {
    53  		go func() { http.ListenAndServe(":6060", nil) }()
    54  	}
    55  }