Skip to content

Commit

Permalink
feat: add mem_sys_bytes metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Jan 14, 2020
1 parent 868fece commit a2ea46d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## 1.0.0-dev

- Add `mem_sys_bytes` metric. ([@palkan][])

Returns the total bytes of memory obtained from the OS
(according to [`runtime.MemStats.Sys`](https://golang.org/pkg/runtime/#MemStats)).

- Add `--enable_ws_compression` option to enable WebSocket per message compression. ([@palkan][])

Disabled by default due to the experimental status in [Gorilla](https://github.com/gorilla/websocket/blob/c3e18be99d19e6b3e8f1559eea2c161a665c4b6b/doc.go#L201-L214).
Expand Down
5 changes: 5 additions & 0 deletions metrics/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ func (g *Gauge) Set(value int) {
atomic.StoreInt64(&g.value, int64(value))
}

// Set64 sets gauge value as int64
func (g *Gauge) Set64(value int64) {
atomic.StoreInt64(&g.value, value)
}

// Value returns the current gauge value
func (g *Gauge) Value() int64 {
return atomic.LoadInt64(&g.value)
Expand Down
6 changes: 6 additions & 0 deletions node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
statsCollectInterval = 5 * time.Second

metricsGoroutines = "goroutines_num"
metricsMemSys = "mem_sys_bytes"
metricsClientsNum = "clients_num"
metricsUniqClientsNum = "clients_uniq_num"
metricsStreamsNum = "broadcast_streams_num"
Expand Down Expand Up @@ -341,6 +342,10 @@ func (n *Node) collectStats() {
func (n *Node) collectStatsOnce() {
n.Metrics.Gauge(metricsGoroutines).Set(runtime.NumGoroutine())

var m runtime.MemStats
runtime.ReadMemStats(&m)
n.Metrics.Gauge(metricsMemSys).Set64(int64(m.Sys))

n.Metrics.Gauge(metricsClientsNum).Set(n.hub.Size())
n.Metrics.Gauge(metricsUniqClientsNum).Set(n.hub.UniqSize())
n.Metrics.Gauge(metricsStreamsNum).Set(n.hub.StreamsSize())
Expand All @@ -349,6 +354,7 @@ func (n *Node) collectStatsOnce() {

func (n *Node) registerMetrics() {
n.Metrics.RegisterGauge(metricsGoroutines, "The number of Go routines")
n.Metrics.RegisterGauge(metricsMemSys, "The total bytes of memory obtained from the OS")

n.Metrics.RegisterGauge(metricsClientsNum, "The number of active clients")
n.Metrics.RegisterGauge(metricsUniqClientsNum, "The number of unique clients (with respect to connection identifiers)")
Expand Down

0 comments on commit a2ea46d

Please sign in to comment.