fastly/go-utils — pull request #10 by sfjuggernaut (2014-07-16 20:52:37+00:00)
Title: Add simple function to return system stats.

Add a common function that all Go projects can use to return useful debugging information. We can add more stats as we see fit.

For example, one could create an endpoint such as:
```
func (api *Api) QQDebugHandler(w rest.ResponseWriter, r *rest.Request) {
info := instrumentation.GetSystemStats()

fmt.Fprint(w.(http.ResponseWriter), "\nSystem Stats\n============\n")
s := reflect.ValueOf(&info).Elem()
typeOfT := s.Type()
for i := 0; i < s.NumField(); i++ {
f := s.Field(i)
fmt.Fprintf(w.(http.ResponseWriter), "%s: %v\n", typeOfT.Field(i).Name, f.Interface())
}

stack := make([]byte, 10240)
runtime.Stack(stack, true)
fmt.Fprintf(w.(http.ResponseWriter), "\nStack Traces\n============\n")
fmt.Fprintf(w.(http.ResponseWriter), string(stack))
}
```

Which would return the following:
```
$ curl -XGET localhost:1337/qq_debug

System Stats
============
NumGoRoutines: 7
UserTime: 0.004
SystemTime: 0
BytesAlloc: 650720
BytesFromSystem: 4331752
GCPauseTimesNs: 0.309417
GCPauseTimeMax: 0.309417
GCPauseTimeTotal: 0.655444
GCPauseSince: 2.255102499

Stack Traces
============
goroutine 8 [running]:
github.com/fastly/astral/api.(*Api).QQDebugHandler(0xc21009c5e0, 0x7fcb2d956198, 0xc21009c140, 0xc21009c100)
/mnt/hgfs/wombat/Code/Astral/src/github.com/fastly/astral/api/instrumentation.go:67 +0x501
github.com/fastly/astral/api.*Api.QQDebugHandlerÂ·fm(0x7fcb2d956198, 0xc21009c140, 0xc21009c100)
/mnt/hgfs/wombat/Code/Astral/src/github.com/fastly/astral/api/api.go:165 +0x44
github.com/ant0ine/go-json-rest/rest.funcÂ·006(0x7fcb2d956198, 0xc21009c140, 0xc21009c100)
/mnt/hgfs/wombat/Code/Astral/src/github.com/ant0ine/go-json-rest/rest/handler.go:204 +0x282
github.com/ant0ine/go-json-rest/rest.funcÂ·008(0x7fcb2d956158, 0xc21009c120, 0xc21009c100)
/mnt/hgfs/wombat/Code/Astral/src/github.com/ant0ine/go-json-rest/rest/recorder.go:15 +0x8b
github.com/ant0ine/go-json-rest/rest.funcÂ·011(0x7fcb2d956158, 0xc21009c120, 0xc21009c100)
/mnt/hgfs/wombat/Code/Astral/src/github.com/ant0ine/go-json-rest/rest/timer.go:17 +0x77
github.com/ant0ine/go-json-rest/rest.funcÂ·007(0x7fcb2d956158, 0xc21009c120, 0xc21009c100)
/mnt/hgfs/wombat/Code/Astral/src/github.com/ant0ine/go-json-rest/rest/log.go:27 +0x4b
github.com/ant0ine/go-json-rest/rest.funcÂ·005(0x7fcb2d956120, 0xc21000f6e0, 0xc2100cfdd0)
/mnt/hgfs/wombat/Code/Astral/src/github.com/ant0ine/go-json-rest/rest/handler.go:158 +0x1a9
github.com/ant0ine/go-json-rest/rest.(*ResourceHandler).ServeHTTP(0xc2100a2140, 0x7fcb2d956120, 0xc21000f6e0, 0xc2100cfdd0)
/mnt/hgfs/wombat/Code/Astral/src/github.com/ant0ine/go-json-rest/rest/handler.go:211 +0x44
net/http.serverHandler.ServeHTTP(0xc2100c2fa0, 0x7fcb2d956120, 0xc21000f6e0, 0xc2100cfdd0)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/http/server.go:1597 +0x16e
net/http.(*conn).serve(0xc2100b7380)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/http/server.go:1167 +0x7b7
created by net/http.(*Server).Serve
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/http/server.go:1644 +0x28b

goroutine 1 [IO wait]:
net.runtime_pollWait(0x7fcb2d955338, 0x72, 0x0)
/tmp/bindist375750859/go/src/pkg/runtime/netpoll.goc:116 +0x6a
net.(*pollDesc).Wait(0xc2100b5ca0, 0x72, 0x7fcb2d9540e8, 0xb)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/fd_poll_runtime.go:81 +0x34
net.(*pollDesc).WaitRead(0xc2100b5ca0, 0xb, 0x7fcb2d9540e8)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/fd_poll_runtime.go:86 +0x30
net.(*netFD).accept(0xc2100b5c40, 0x8f2a48, 0x0, 0x7fcb2d9540e8, 0xb)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/fd_unix.go:382 +0x2c2
net.(*TCPListener).AcceptTCP(0xc210000428, 0x4f5b1b, 0x7fcb2d7b4c00, 0x4f5b1b)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/tcpsock_posix.go:233 +0x47
net.(*TCPListener).Accept(0xc210000428, 0x7fcb2d955b60, 0xc210000460, 0xc2100b7380, 0x0)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/tcpsock_posix.go:243 +0x27
net/http.(*Server).Serve(0xc2100c2fa0, 0x7fcb2d955ff0, 0xc210000428, 0x0, 0x0)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/http/server.go:1622 +0x91
net/http.Serve(0x7fcb2d955ff0, 0xc210000428, 0x7fcb2d956028, 0xc2100a2140, 0x7fcb2d955ff0, ...)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/net/http/server.go:1561 +0x70
github.com/fastly/astral.(*Astral).Run(0xc2100623c0)
/mnt/hgfs/wombat/Code/Astral/src/github.com/fastly/astral/listener.go:83 +0x4a6
main.main()
/mnt/hgfs/wombat/Code/Astral/src/github.com/fastly/astral/bin/astral/astral.go:65 +0x87e

goroutine 4 [chan receive]:
database/sql.(*DB).connectionOpener(0xc21006b700)
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/database/sql/sql.go:574 +0x3e
created by database/sql.Open
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/database/sql/sql.go:436 +0x24d

goroutine 5 [syscall]:
runtime.goexit()
/mnt/hgfs/wombat/Code/Astral/go/src/pkg/runtime/proc.c:1394

goroutine 6 [select]:
github.com/fastly/astral/zonemanager.(*ZoneManager).UpdateManager(0xc2100a0460)
/mnt/hgfs/wombat/Code/Astral/src/github.com/fastly/astral/zonemanager/zonemanager.go:71 +0x482
created by github.com/fastly/astral.NewAstral
/mnt/hgfs/wombat/Code/Astral/src/github.com/fastly/astral/listener.go:47 +0xc6
$
```