💻 Tech
expvar package allows to expose server and custom variables. By just importing the package, you will get the side effect of registerring the variables. You can access them via http://localhost:8081/debug/vars.
import (
_ "expvar"
)
Sample output:
{
"cmdline": [
"/tmp/go-build387866508/b001/exe/nlpd"
],
"memstats": {
"Alloc": 330648,
"TotalAlloc": 330648,
"Sys": 6380560,
"Lookups": 0,
"Mallocs": 831,
"Frees": 63,
"HeapAlloc": 330648,
"HeapSys": 3833856,
"HeapIdle": 2899968,
"HeapInuse": 933888,
"HeapReleased": 2899968,
"HeapObjects": 768,
"StackInuse": 360448,
"StackSys": 360448,
"MSpanInuse": 24320,
"MSpanSys": 32640,
"MCacheInuse": 14400,
"MCacheSys": 15600,
"BuckHashSys": 7198,
"GCSys": 1727224,
"OtherSys": 403594,
"NextGC": 4194304,
"LastGC": 0,
"PauseTotalNs": 0,
"NumGC": 0,
"NumForcedGC": 0,
"GCCPUFraction": 0,
"EnableGC": true,
"DebugGC": false,
"BySize": [
{
"Size": 0,
"Mallocs": 0,
"Frees": 0
}
}
}
You can even customize the displayed JSON values. counter will become part of the JSON output.
var (
counter = expvar.NewInt("counter")
)
// ...
counter.Add(1)