]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/pprof: restore printing descriptive errors from net/http/pprof endpoints
authorKale Blankenship <kale@lemnisys.com>
Thu, 15 Jun 2017 00:39:03 +0000 (17:39 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 15 Jun 2017 02:14:24 +0000 (02:14 +0000)
Restores functionality added in https://golang.org/cl/35564/ which was
lost in https://golang.org/cl/36798/ by the addition of the custom
fetcher to src/cmd/pprof/pprof.go. The custom fetcher overrides the
upstream default.

Change-Id: Ic71e5e475d043276d916298ab5acb5c9b9ad063e
Reviewed-on: https://go-review.googlesource.com/45812
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
src/cmd/pprof/pprof.go

index 24bec07d9752cd32d5a1b35c66675510247e1176..2f3268cbc4f800eaa90fa03f6a74a93a9fcc8826 100644 (file)
@@ -13,11 +13,13 @@ import (
        "crypto/tls"
        "debug/dwarf"
        "fmt"
+       "io/ioutil"
        "net/http"
        "net/url"
        "os"
        "regexp"
        "strconv"
+       "strings"
        "sync"
        "time"
 
@@ -82,11 +84,22 @@ func getProfile(source string, timeout time.Duration) (*profile.Profile, error)
                return nil, err
        }
        if resp.StatusCode != http.StatusOK {
-               return nil, fmt.Errorf("server response: %s", resp.Status)
+               defer resp.Body.Close()
+               return nil, statusCodeError(resp)
        }
        return profile.Parse(resp.Body)
 }
 
+func statusCodeError(resp *http.Response) error {
+       if resp.Header.Get("X-Go-Pprof") != "" && strings.Contains(resp.Header.Get("Content-Type"), "text/plain") {
+               // error is from pprof endpoint
+               if body, err := ioutil.ReadAll(resp.Body); err == nil {
+                       return fmt.Errorf("server response: %s - %s", resp.Status, body)
+               }
+       }
+       return fmt.Errorf("server response: %s", resp.Status)
+}
+
 // cpuProfileHandler is the Go pprof CPU profile handler URL.
 const cpuProfileHandler = "/debug/pprof/profile"