github.com/influxdata/telegraf@v1.30.3/cmd/telegraf/pprof.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	_ "net/http/pprof" //nolint:gosec // Import for pprof, only enabled via CLI flag
     7  	"strings"
     8  	"time"
     9  )
    10  
    11  type Server interface {
    12  	Start(string)
    13  	ErrChan() <-chan error
    14  }
    15  
    16  type PprofServer struct {
    17  	err chan error
    18  }
    19  
    20  func NewPprofServer() *PprofServer {
    21  	return &PprofServer{
    22  		err: make(chan error),
    23  	}
    24  }
    25  
    26  func (p *PprofServer) Start(address string) {
    27  	go func() {
    28  		pprofHostPort := address
    29  		parts := strings.Split(pprofHostPort, ":")
    30  		if len(parts) == 2 && parts[0] == "" {
    31  			pprofHostPort = "localhost:" + parts[1]
    32  		}
    33  		pprofHostPort = "http://" + pprofHostPort + "/debug/pprof"
    34  
    35  		log.Printf("I! Starting pprof HTTP server at: %s", pprofHostPort)
    36  
    37  		server := &http.Server{
    38  			Addr:         address,
    39  			ReadTimeout:  10 * time.Second,
    40  			WriteTimeout: 10 * time.Second,
    41  		}
    42  
    43  		if err := server.ListenAndServe(); err != nil {
    44  			p.err <- err
    45  		}
    46  		close(p.err)
    47  	}()
    48  }
    49  
    50  func (p *PprofServer) ErrChan() <-chan error {
    51  	return p.err
    52  }