From 091d28d52e944db21b0d9a8d06b2793263f03689 Mon Sep 17 00:00:00 2001
From: hyung-hwan <hyunghwan.chung@gmail.com>
Date: Mon, 13 Jan 2025 15:20:36 +0900
Subject: [PATCH] added space for extra statistical items by exterrnal services

---
 server-ctl.go |  8 ++++++++
 server.go     | 10 ++++++++++
 2 files changed, 18 insertions(+)

diff --git a/server-ctl.go b/server-ctl.go
index ac46120..7d0a3ab 100644
--- a/server-ctl.go
+++ b/server-ctl.go
@@ -34,6 +34,8 @@ type json_out_server_stats struct {
 	ServerPeers int64 `json:"server-peers"`
 
 	SshProxySessions int64 `json:"ssh-proxy-session"`
+
+	Extra map[string]int64 `json:"extra"`
 }
 
 // ------------------------------------
@@ -344,6 +346,8 @@ func (ctl *server_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request)
 		case http.MethodGet:
 			var stats json_out_server_stats
 			var mstat runtime.MemStats
+			var k string
+			var v int64
 			runtime.ReadMemStats(&mstat)
 			stats.CPUs = runtime.NumCPU()
 			stats.Goroutines = runtime.NumGoroutine()
@@ -355,6 +359,10 @@ func (ctl *server_ctl_stats) ServeHTTP(w http.ResponseWriter, req *http.Request)
 			stats.ServerRoutes = s.stats.routes.Load()
 			stats.ServerPeers = s.stats.peers.Load()
 			stats.SshProxySessions = s.stats.ssh_proxy_sessions.Load()
+			s.stats.extra_mtx.Lock()
+			stats.Extra = make(map[string]int64, len(s.stats.extra))
+			for k, v = range s.stats.extra { stats.Extra[k] = v }
+			s.stats.extra_mtx.Unlock()
 			status_code = http.StatusOK; w.WriteHeader(status_code)
 			if err = je.Encode(stats); err != nil { goto oops }
 
diff --git a/server.go b/server.go
index 008e8d1..c16ad81 100644
--- a/server.go
+++ b/server.go
@@ -89,6 +89,9 @@ type Server struct {
 		routes atomic.Int64
 		peers atomic.Int64
 		ssh_proxy_sessions atomic.Int64
+
+		extra_mtx sync.Mutex
+		extra map[string]int64
 	}
 
 	wpx_resp_tf     ServerWpxResponseTransformer
@@ -1146,6 +1149,7 @@ func NewServer(ctx context.Context, logger Logger, ctl_addrs []string, rpc_addrs
 	s.stats.routes.Store(0)
 	s.stats.peers.Store(0)
 	s.stats.ssh_proxy_sessions.Store(0)
+	s.stats.extra = make(map[string]int64, 64)
 
 	return &s, nil
 
@@ -1660,3 +1664,9 @@ func (s *Server) WaitForTermination() {
 func (s *Server) WriteLog(id string, level LogLevel, fmtstr string, args ...interface{}) {
 	s.log.Write(id, level, fmtstr, args...)
 }
+
+func (s *Server) SetExtraStat(key string, val int64) {
+	s.stats.extra_mtx.Lock()
+	s.stats.extra[key] = val
+	s.stats.extra_mtx.Unlock()
+}