Pprof listen (#639)
* add listen addr option * Add listen address option to pprof * There is configuration * code styling
This commit is contained in:
parent
4c9351b0a3
commit
f359aea2fa
4 changed files with 34 additions and 9 deletions
|
@ -11,14 +11,14 @@ For more information, please see [Go's pprof
|
||||||
documentation](https://golang.org/pkg/net/http/pprof/) and read
|
documentation](https://golang.org/pkg/net/http/pprof/) and read
|
||||||
[Profiling Go Programs](https://blog.golang.org/profiling-go-programs).
|
[Profiling Go Programs](https://blog.golang.org/profiling-go-programs).
|
||||||
|
|
||||||
There is not configuration.
|
|
||||||
|
|
||||||
## Syntax
|
## Syntax
|
||||||
|
|
||||||
~~~
|
~~~
|
||||||
pprof
|
pprof [ADDRESS]
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
If not specified, ADDRESS defaults to localhost:6053.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
Enable pprof endpoints:
|
Enable pprof endpoints:
|
||||||
|
@ -26,3 +26,15 @@ Enable pprof endpoints:
|
||||||
~~~
|
~~~
|
||||||
pprof
|
pprof
|
||||||
~~~
|
~~~
|
||||||
|
|
||||||
|
Listen on an alternate address:
|
||||||
|
|
||||||
|
~~~
|
||||||
|
pprof 10.9.8.7:6060
|
||||||
|
~~~
|
||||||
|
|
||||||
|
Listen on an all addresses on port 6060:
|
||||||
|
|
||||||
|
~~~
|
||||||
|
pprof :6060
|
||||||
|
~~~
|
||||||
|
|
|
@ -10,12 +10,13 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type handler struct {
|
type handler struct {
|
||||||
ln net.Listener
|
addr string
|
||||||
mux *http.ServeMux
|
ln net.Listener
|
||||||
|
mux *http.ServeMux
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *handler) Startup() error {
|
func (h *handler) Startup() error {
|
||||||
ln, err := net.Listen("tcp", addr)
|
ln, err := net.Listen("tcp", h.addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("[ERROR] Failed to start pprof handler: %s", err)
|
log.Printf("[ERROR] Failed to start pprof handler: %s", err)
|
||||||
return err
|
return err
|
||||||
|
@ -44,6 +45,5 @@ func (h *handler) Shutdown() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
addr = "localhost:6053"
|
|
||||||
path = "/debug/pprof"
|
path = "/debug/pprof"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package pprof
|
package pprof
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/coredns/coredns/middleware"
|
"github.com/coredns/coredns/middleware"
|
||||||
|
@ -8,6 +9,8 @@ import (
|
||||||
"github.com/mholt/caddy"
|
"github.com/mholt/caddy"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultAddr = "localhost:6053"
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
caddy.RegisterPlugin("pprof", caddy.Plugin{
|
caddy.RegisterPlugin("pprof", caddy.Plugin{
|
||||||
ServerType: "dns",
|
ServerType: "dns",
|
||||||
|
@ -17,11 +20,20 @@ func init() {
|
||||||
|
|
||||||
func setup(c *caddy.Controller) error {
|
func setup(c *caddy.Controller) error {
|
||||||
found := false
|
found := false
|
||||||
|
h := &handler{addr: defaultAddr}
|
||||||
for c.Next() {
|
for c.Next() {
|
||||||
if found {
|
if found {
|
||||||
return middleware.Error("pprof", c.Err("pprof can only be specified once"))
|
return middleware.Error("pprof", c.Err("pprof can only be specified once"))
|
||||||
}
|
}
|
||||||
if len(c.RemainingArgs()) != 0 {
|
args := c.RemainingArgs()
|
||||||
|
if len(args) == 1 {
|
||||||
|
h.addr = args[0]
|
||||||
|
_, _, e := net.SplitHostPort(h.addr)
|
||||||
|
if e != nil {
|
||||||
|
return e
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if len(args) > 1 {
|
||||||
return middleware.Error("pprof", c.ArgErr())
|
return middleware.Error("pprof", c.ArgErr())
|
||||||
}
|
}
|
||||||
if c.NextBlock() {
|
if c.NextBlock() {
|
||||||
|
@ -30,7 +42,6 @@ func setup(c *caddy.Controller) error {
|
||||||
found = true
|
found = true
|
||||||
}
|
}
|
||||||
|
|
||||||
h := &handler{}
|
|
||||||
pprofOnce.Do(func() {
|
pprofOnce.Do(func() {
|
||||||
c.OnStartup(h.Startup)
|
c.OnStartup(h.Startup)
|
||||||
c.OnShutdown(h.Shutdown)
|
c.OnShutdown(h.Shutdown)
|
||||||
|
|
|
@ -12,6 +12,8 @@ func TestPProf(t *testing.T) {
|
||||||
shouldErr bool
|
shouldErr bool
|
||||||
}{
|
}{
|
||||||
{`pprof`, false},
|
{`pprof`, false},
|
||||||
|
{`pprof 1.2.3.4:1234`, false},
|
||||||
|
{`pprof :1234`, false},
|
||||||
{`pprof {}`, true},
|
{`pprof {}`, true},
|
||||||
{`pprof /foo`, true},
|
{`pprof /foo`, true},
|
||||||
{`pprof {
|
{`pprof {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue