Skip to content

Go Memory Monitoring

Mike Perham edited this page Jun 23, 2018 · 1 revision

The Go language makes it really easy to expose runtime metrics for Go's memory usage and garbage collector. Inspeqtor can display graphs of this data for real-time monitoring in production!

memory monitoring

Enable Monitoring

This feature does require code changes within your Go daemon. Your daemon must expose its expvar data:

  1. import _ "expvar"
  2. Start an HTTP server to make the data available on a port:
sock, err := net.Listen("tcp", "localhost:4001")
if err != nil {
  return err
}
go func() {
  fmt.Println("HTTP now available at port 4001")
  http.Serve(sock, nil)
}()

You should now be able to hit http://localhost:4001/debug/vars and see a blob of JSON.

Now configure Inspeqtor's .inq file to fetch and expose this data:

check service etcd with port 4001
  expose memstats
  if memstats:Alloc > 1m for 2 cycles then alert

with port 4001 tells Inspeqtor which port to fetch the expvar data. Inspeqtor must be running on the same machine as the daemon. expose memstats tells Inspeqtor to enable the Web UI to visualize the memstats within the expvar data. Notice you can also write typical Inspeqtor rules against the memstats metrics.

You can find Inspeqtor Pro's Web UI on http://localhost:4677/memory/. 4677 is INSP on an old telephone handset.

memstats Metrics

Inspeqtor tracks five memstats metrics; you can write rules as normal against them:

  • Alloc - current amount of allocated memory (gauge)
  • TotalAlloc - total amount of allocated memory for all time (counter)
  • HeapAlloc - current heap size (gauge)
  • NumGC - total number of garbage collections (counter)
  • PauseTotalNs - total GC pause (counter)

Example:

check service etcd with port 4001
  # ensure we aren't garbage collecting really frequently
  if memstats:NumGC > 1/sec then alert
  if memstat:Alloc > 100m then alert