-
Notifications
You must be signed in to change notification settings - Fork 9
/
pprof.go
48 lines (39 loc) · 1.29 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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fncobra
import (
"context"
"fmt"
"io"
"net"
"net/http"
"net/http/pprof"
"github.com/gorilla/mux"
"namespacelabs.dev/foundation/internal/fnnet"
)
func ListenPProf(debugSink io.Writer) {
if err := listenPProf(debugSink); err != nil {
fmt.Fprintf(debugSink, "pprof: failed to listen: %v\n", err)
}
}
func listenPProf(debugSink io.Writer) error {
const target = 6060
h := mux.NewRouter()
RegisterPprof(h)
lst, err := fnnet.ListenPort(context.Background(), "127.0.0.1", 0, target)
if err != nil {
return err
}
localPort := lst.Addr().(*net.TCPAddr).Port
fmt.Fprintf(debugSink, "pprof: listening on http://127.0.0.1:%d/debug/pprof/\n", localPort)
return http.Serve(lst, h)
}
func RegisterPprof(r *mux.Router) {
r.PathPrefix("/debug/pprof/").HandlerFunc(pprof.Index)
r.PathPrefix("/debug/pprof/cmdline").HandlerFunc(pprof.Cmdline)
r.PathPrefix("/debug/pprof/profile").HandlerFunc(pprof.Profile)
r.PathPrefix("/debug/pprof/symbol").HandlerFunc(pprof.Symbol)
r.PathPrefix("/debug/pprof/trace").HandlerFunc(pprof.Trace)
r.PathPrefix("/debug/pprof/goroutine").HandlerFunc(pprof.Index)
}