Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
4d9741dd
Commit
4d9741dd
authored
Mar 19, 2016
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pprof: Only handle if path matches /debug/pprof, add tests
parent
a05a664d
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
86 additions
and
21 deletions
+86
-21
caddy/setup/pprof.go
caddy/setup/pprof.go
+2
-1
dist/CHANGES.txt
dist/CHANGES.txt
+2
-0
middleware/pprof/pprof.go
middleware/pprof/pprof.go
+27
-20
middleware/pprof/pprof_test.go
middleware/pprof/pprof_test.go
+55
-0
No files found.
caddy/setup/pprof.go
View file @
4d9741dd
...
...
@@ -20,7 +20,8 @@ func PProf(c *Controller) (middleware.Middleware, error) {
}
found
=
true
}
return
func
(
next
middleware
.
Handler
)
middleware
.
Handler
{
return
pprof
.
New
(
next
)
return
&
pprof
.
Handler
{
Next
:
next
,
Mux
:
pprof
.
NewMux
()}
},
nil
}
dist/CHANGES.txt
View file @
4d9741dd
CHANGES
<master>
- New pprof directive for exposing process performance profile
- Toggle case-sensitive path matching with environment variable
- proxy: New max_conns setting to limit max connections per upstream
- Internal improvements, restructuring, and bug fixes
...
...
middleware/pprof/pprof.go
View file @
4d9741dd
...
...
@@ -7,28 +7,35 @@ import (
"github.com/mholt/caddy/middleware"
)
//Handler is a simple struct whose ServeHTTP will delegate relevant pprof endpoints to net/http/pprof
type
handler
struct
{
mux
*
http
.
ServeMux
// BasePath is the base path to match for all pprof requests.
const
BasePath
=
"/debug/pprof"
// Handler is a simple struct whose ServeHTTP will delegate pprof
// endpoints to their equivalent net/http/pprof handlers.
type
Handler
struct
{
Next
middleware
.
Handler
Mux
*
http
.
ServeMux
}
//New creates a new pprof middleware
func
New
(
next
middleware
.
Handler
)
middleware
.
Handler
{
//pretty much copying what pprof does on init: https://golang.org/src/net/http/pprof/pprof.go#L67
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
"/debug/pprof/"
,
pp
.
Index
)
mux
.
HandleFunc
(
"/debug/pprof/cmdline"
,
pp
.
Cmdline
)
mux
.
HandleFunc
(
"/debug/pprof/profile"
,
pp
.
Profile
)
mux
.
HandleFunc
(
"/debug/pprof/symbol"
,
pp
.
Symbol
)
mux
.
HandleFunc
(
"/debug/pprof/trace"
,
pp
.
Trace
)
mux
.
HandleFunc
(
"/"
,
func
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
{
next
.
ServeHTTP
(
w
,
r
)
})
return
&
handler
{
mux
}
// ServeHTTP handles requests to BasePath with pprof, or passes
// all other requests up the chain.
func
(
h
*
Handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
if
middleware
.
Path
(
r
.
URL
.
Path
)
.
Matches
(
BasePath
)
{
h
.
Mux
.
ServeHTTP
(
w
,
r
)
return
0
,
nil
}
return
h
.
Next
.
ServeHTTP
(
w
,
r
)
}
func
(
h
*
handler
)
ServeHTTP
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
rec
:=
middleware
.
NewResponseRecorder
(
w
)
h
.
mux
.
ServeHTTP
(
rec
,
r
)
return
rec
.
Status
(),
nil
// NewMux returns a new http.ServeMux that routes pprof requests.
// It pretty much copies what the std lib pprof does on init:
// https://golang.org/src/net/http/pprof/pprof.go#L67
func
NewMux
()
*
http
.
ServeMux
{
mux
:=
http
.
NewServeMux
()
mux
.
HandleFunc
(
BasePath
+
"/"
,
pp
.
Index
)
mux
.
HandleFunc
(
BasePath
+
"/cmdline"
,
pp
.
Cmdline
)
mux
.
HandleFunc
(
BasePath
+
"/profile"
,
pp
.
Profile
)
mux
.
HandleFunc
(
BasePath
+
"/symbol"
,
pp
.
Symbol
)
mux
.
HandleFunc
(
BasePath
+
"/trace"
,
pp
.
Trace
)
return
mux
}
middleware/pprof/pprof_test.go
0 → 100644
View file @
4d9741dd
package
pprof
import
(
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/mholt/caddy/middleware"
)
func
TestServeHTTP
(
t
*
testing
.
T
)
{
h
:=
Handler
{
Next
:
middleware
.
HandlerFunc
(
nextHandler
),
Mux
:
NewMux
(),
}
w
:=
httptest
.
NewRecorder
()
r
,
err
:=
http
.
NewRequest
(
"GET"
,
"/debug/pprof"
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
status
,
err
:=
h
.
ServeHTTP
(
w
,
r
)
if
status
!=
0
{
t
.
Errorf
(
"Expected status %d but got %d"
,
0
,
status
)
}
if
err
!=
nil
{
t
.
Errorf
(
"Expected nil error, but got: %v"
,
err
)
}
if
w
.
Body
.
String
()
==
"content"
{
t
.
Errorf
(
"Expected pprof to handle request, but it didn't"
)
}
w
=
httptest
.
NewRecorder
()
r
,
err
=
http
.
NewRequest
(
"GET"
,
"/foo"
,
nil
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
status
,
err
=
h
.
ServeHTTP
(
w
,
r
)
if
status
!=
http
.
StatusNotFound
{
t
.
Errorf
(
"Test two: Expected status %d but got %d"
,
http
.
StatusNotFound
,
status
)
}
if
err
!=
nil
{
t
.
Errorf
(
"Test two: Expected nil error, but got: %v"
,
err
)
}
if
w
.
Body
.
String
()
!=
"content"
{
t
.
Errorf
(
"Expected pprof to pass the request thru, but it didn't; got: %s"
,
w
.
Body
.
String
())
}
}
func
nextHandler
(
w
http
.
ResponseWriter
,
r
*
http
.
Request
)
(
int
,
error
)
{
fmt
.
Fprintf
(
w
,
"content"
)
return
http
.
StatusNotFound
,
nil
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment