github.com/m3db/m3@v1.5.0/src/x/debug/profile.go (about)

     1  // Copyright (c) 2019 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package debug
    22  
    23  import (
    24  	"fmt"
    25  	"io"
    26  	"net/http"
    27  	"runtime/pprof"
    28  )
    29  
    30  // ProfileSource defines a custom source of a runtime/pprof Profile. This can
    31  // either be part of a predefined or custom profile.
    32  type ProfileSource struct {
    33  	name  string
    34  	debug int
    35  }
    36  
    37  // NewProfileSource returns a ProfileSource with a name and debug level, where
    38  // the verbosity of the final stack information increases with value.
    39  // Will return an error if name is an empty string.
    40  func NewProfileSource(name string, debug int) (*ProfileSource, error) {
    41  	if name == "" {
    42  		return nil, fmt.Errorf("name can't be empty")
    43  	}
    44  	return &ProfileSource{
    45  		name:  name,
    46  		debug: debug,
    47  	}, nil
    48  }
    49  
    50  // Profile returns a *pprof.Profile according the the name of the ProfileSource.
    51  // This will first try find an existing profile, creating one if it can't be
    52  // found.
    53  func (p *ProfileSource) Profile() *pprof.Profile {
    54  	var pp *pprof.Profile
    55  	pp = pprof.Lookup(p.name)
    56  	if pp == nil {
    57  		return pprof.NewProfile(p.name)
    58  	}
    59  	return pp
    60  }
    61  
    62  // Write writes a pprof-formatted snapshot of the profile to w. If a write to w
    63  // returns an error, Write returns that error. Otherwise, Write returns nil.
    64  func (p *ProfileSource) Write(w io.Writer, _ *http.Request) error {
    65  	prof := p.Profile()
    66  
    67  	if err := prof.WriteTo(w, p.debug); err != nil {
    68  		return fmt.Errorf("unable to write %s profile: %s", p.name, err)
    69  	}
    70  	return nil
    71  }