2020-04-08 05:24:06 +00:00
|
|
|
// Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2022-06-07 18:50:12 +00:00
|
|
|
//go:build !js
|
|
|
|
// +build !js
|
|
|
|
|
2020-04-08 05:24:06 +00:00
|
|
|
// Package logheap logs a heap pprof profile.
|
|
|
|
package logheap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-06-12 17:12:35 +00:00
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
2020-04-08 05:24:06 +00:00
|
|
|
"runtime"
|
|
|
|
"runtime/pprof"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-09-08 02:17:53 +00:00
|
|
|
// LogHeap uploads a JSON logtail record with the base64 heap pprof by means
|
|
|
|
// of an HTTP POST request to the endpoint referred to in postURL.
|
2020-06-12 17:12:35 +00:00
|
|
|
func LogHeap(postURL string) {
|
|
|
|
if postURL == "" {
|
|
|
|
return
|
|
|
|
}
|
2020-04-08 05:24:06 +00:00
|
|
|
runtime.GC()
|
|
|
|
buf := new(bytes.Buffer)
|
2020-09-08 02:17:53 +00:00
|
|
|
if err := pprof.WriteHeapProfile(buf); err != nil {
|
|
|
|
log.Printf("LogHeap: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2020-06-12 17:12:35 +00:00
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequestWithContext(ctx, "POST", postURL, buf)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("LogHeap: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("LogHeap: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
2020-04-08 05:24:06 +00:00
|
|
|
}
|