mirror of
https://github.com/caddyserver/caddy.git
synced 2025-01-06 22:40:31 -05:00
expvar: Allow no args; publish number of goroutines
This commit is contained in:
parent
a3127bed5f
commit
426d165254
3 changed files with 32 additions and 9 deletions
|
@ -1,7 +1,9 @@
|
|||
package setup
|
||||
|
||||
import (
|
||||
_ "expvar"
|
||||
stdexpvar "expvar"
|
||||
"runtime"
|
||||
"sync"
|
||||
|
||||
"github.com/mholt/caddy/middleware"
|
||||
"github.com/mholt/caddy/middleware/expvar"
|
||||
|
@ -14,6 +16,9 @@ func ExpVar(c *Controller) (middleware.Middleware, error) {
|
|||
return nil, err
|
||||
}
|
||||
|
||||
// publish any extra information/metrics we may want to capture
|
||||
publishExtraVars()
|
||||
|
||||
expvar := expvar.ExpVar{Resource: resource}
|
||||
|
||||
return func(next middleware.Handler) middleware.Handler {
|
||||
|
@ -24,12 +29,13 @@ func ExpVar(c *Controller) (middleware.Middleware, error) {
|
|||
|
||||
func expVarParse(c *Controller) (expvar.Resource, error) {
|
||||
var resource expvar.Resource
|
||||
|
||||
var err error
|
||||
|
||||
for c.Next() {
|
||||
args := c.RemainingArgs()
|
||||
|
||||
switch len(args) {
|
||||
case 0:
|
||||
resource = expvar.Resource(defaultExpvarPath)
|
||||
case 1:
|
||||
resource = expvar.Resource(args[0])
|
||||
default:
|
||||
|
@ -39,3 +45,16 @@ func expVarParse(c *Controller) (expvar.Resource, error) {
|
|||
|
||||
return resource, err
|
||||
}
|
||||
|
||||
func publishExtraVars() {
|
||||
// By using sync.Once instead of an init() function, we don't clutter
|
||||
// the app's expvar export unnecessarily, or risk colliding with it.
|
||||
publishOnce.Do(func() {
|
||||
stdexpvar.Publish("Goroutines", stdexpvar.Func(func() interface{} {
|
||||
return runtime.NumGoroutine()
|
||||
}))
|
||||
})
|
||||
}
|
||||
|
||||
var publishOnce sync.Once // publishing variables should only be done once
|
||||
var defaultExpvarPath = "/debug/vars"
|
||||
|
|
|
@ -7,29 +7,32 @@ import (
|
|||
)
|
||||
|
||||
func TestExpvar(t *testing.T) {
|
||||
c := NewTestController(`expvar /d/v`)
|
||||
|
||||
c := NewTestController(`expvar`)
|
||||
mid, err := ExpVar(c)
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected no errors, got: %v", err)
|
||||
}
|
||||
if mid == nil {
|
||||
t.Fatal("Expected middleware, was nil instead")
|
||||
}
|
||||
|
||||
c = NewTestController(`expvar /d/v`)
|
||||
mid, err = ExpVar(c)
|
||||
if err != nil {
|
||||
t.Errorf("Expected no errors, got: %v", err)
|
||||
}
|
||||
if mid == nil {
|
||||
t.Fatal("Expected middleware, was nil instead")
|
||||
}
|
||||
|
||||
handler := mid(EmptyNext)
|
||||
myHandler, ok := handler.(expvar.ExpVar)
|
||||
|
||||
if !ok {
|
||||
t.Fatalf("Expected handler to be type ExpVar, got: %#v", handler)
|
||||
}
|
||||
|
||||
if myHandler.Resource != "/d/v" {
|
||||
t.Errorf("Expected /d/v as expvar resource")
|
||||
}
|
||||
|
||||
if !SameNext(myHandler.Next, EmptyNext) {
|
||||
t.Error("'Next' field of handler was not set properly")
|
||||
}
|
||||
|
|
1
dist/CHANGES.txt
vendored
1
dist/CHANGES.txt
vendored
|
@ -2,6 +2,7 @@ CHANGES
|
|||
|
||||
<master>
|
||||
- New pprof directive for exposing process performance profile
|
||||
- New expvar directive for reading memory/GC performance
|
||||
- New -restart option to force in-process restarts on Unix systems
|
||||
- Toggle case-sensitive path matching with environment variable
|
||||
- proxy: New max_conns setting to limit max connections per upstream
|
||||
|
|
Loading…
Reference in a new issue