github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/common/diag/goroutine.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package diag
     8  
     9  import (
    10  	"bytes"
    11  	"runtime/pprof"
    12  )
    13  
    14  type Logger interface {
    15  	Infof(template string, args ...interface{})
    16  	Errorf(template string, args ...interface{})
    17  }
    18  
    19  func CaptureGoRoutines() (string, error) {
    20  	var buf bytes.Buffer
    21  	err := pprof.Lookup("goroutine").WriteTo(&buf, 2)
    22  	if err != nil {
    23  		return "", err
    24  	}
    25  	return buf.String(), nil
    26  }
    27  
    28  func LogGoRoutines(logger Logger) {
    29  	output, err := CaptureGoRoutines()
    30  	if err != nil {
    31  		logger.Errorf("failed to capture go routines: %s", err)
    32  		return
    33  	}
    34  
    35  	logger.Infof("Go routines report:\n%s", output)
    36  }