/
pprof.go
78 lines (66 loc) · 1.98 KB
/
pprof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2021-2022 Teal.Finance contributors
// This file is part of Teal.Finance/Garcon,
// an API and website server, under the MIT License.
// SPDX-License-Identifier: MIT
// Package pprof serves the /debug/pprof endpoint
package pprof
import (
"log"
"net/http"
"net/http/pprof"
"strconv"
"github.com/go-chi/chi/v5"
"github.com/pkg/profile"
)
// ProbeCPU is used like the following:
//
// defer pprof.ProbeCPU.Stop()
//
// When the caller reaches its function end,
// the defer executes Stop() that writes the file "cpu.pprof".
// To visualize "cpu.pprof" use the pprof tool:
//
// cd ~/go
// go get -u github.com/google/pprof
// cd -
// pprof -http=: cpu.pprof
func ProbeCPU() interface{ Stop() } {
log.Print("Probing CPU. To visualize the profile: pprof -http=: cpu.pprof")
return profile.Start(profile.ProfilePath("."))
}
// StartServer starts a PProf server in background.
// Endpoints usage example:
//
// curl http://localhost:6063/debug/pprof/allocs > allocs.pprof
// pprof -http=: allocs.pprof
//
// wget http://localhost:31415/debug/pprof/goroutine
// pprof -http=: goroutine
//
// wget http://localhost:31415/debug/pprof/heap
// pprof -http=: heap
//
// wget http://localhost:31415/debug/pprof/trace
// pprof -http=: trace
func StartServer(port int) {
if port == 0 {
return // Disable PProf endpoints /debug/pprof/*
}
addr := "localhost:" + strconv.Itoa(port)
h := handler()
go runServer(addr, h)
}
func handler() http.Handler {
r := chi.NewRouter()
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)
r.NotFound(pprof.Index) // also serves /debug/pprof/{heap,goroutine,block…}
return r
}
func runServer(addr string, handler http.Handler) {
log.Print("Enable PProf endpoints: http://" + addr + "/debug/pprof")
err := http.ListenAndServe(addr, handler)
log.Fatal(err)
}