derp: export metric for server's initial MemStats.Sys reading

Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
reviewable/pr208/r1
Brad Fitzpatrick 2020-03-20 15:22:02 -07:00 committed by Dave Anderson
parent 64b5248929
commit fd824df1fa
1 changed files with 7 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import (
"io/ioutil" "io/ioutil"
"math/big" "math/big"
"os" "os"
"runtime"
"strconv" "strconv"
"sync" "sync"
"time" "time"
@ -45,6 +46,7 @@ type Server struct {
privateKey key.Private privateKey key.Private
publicKey key.Public publicKey key.Public
logf logger.Logf logf logger.Logf
memSys0 uint64 // runtime.MemStats.Sys at start (or early-ish)
// Counters: // Counters:
packetsSent, bytesSent expvar.Int packetsSent, bytesSent expvar.Int
@ -86,6 +88,9 @@ type Conn interface {
// NewServer returns a new DERP server. It doesn't listen on its own. // NewServer returns a new DERP server. It doesn't listen on its own.
// Connections are given to it via Server.Accept. // Connections are given to it via Server.Accept.
func NewServer(privateKey key.Private, logf logger.Logf) *Server { func NewServer(privateKey key.Private, logf logger.Logf) *Server {
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
s := &Server{ s := &Server{
privateKey: privateKey, privateKey: privateKey,
publicKey: privateKey.Public(), publicKey: privateKey.Public(),
@ -94,6 +99,7 @@ func NewServer(privateKey key.Private, logf logger.Logf) *Server {
clients: make(map[key.Public]*sclient), clients: make(map[key.Public]*sclient),
clientsEver: make(map[key.Public]bool), clientsEver: make(map[key.Public]bool),
netConns: make(map[Conn]chan struct{}), netConns: make(map[Conn]chan struct{}),
memSys0: ms.Sys,
} }
s.packetsDroppedUnknown = s.packetsDroppedReason.Get("unknown_dest") s.packetsDroppedUnknown = s.packetsDroppedReason.Get("unknown_dest")
s.packetsDroppedGone = s.packetsDroppedReason.Get("gone") s.packetsDroppedGone = s.packetsDroppedReason.Get("gone")
@ -639,6 +645,7 @@ func (s *Server) expVarFunc(f func() interface{}) expvar.Func {
func (s *Server) ExpVar() expvar.Var { func (s *Server) ExpVar() expvar.Var {
m := new(metrics.Set) m := new(metrics.Set)
m.Set("counter_unique_clients_ever", s.expVarFunc(func() interface{} { return len(s.clientsEver) })) m.Set("counter_unique_clients_ever", s.expVarFunc(func() interface{} { return len(s.clientsEver) }))
m.Set("gauge_memstats_sys0", expvar.Func(func() interface{} { return int64(s.memSys0) }))
m.Set("gauge_current_connnections", &s.curClients) m.Set("gauge_current_connnections", &s.curClients)
m.Set("gauge_current_home_connnections", &s.curHomeClients) m.Set("gauge_current_home_connnections", &s.curHomeClients)
m.Set("accepts", &s.accepts) m.Set("accepts", &s.accepts)