From 30cc92870515af08cbe2647d598b3ba5c697d9cf Mon Sep 17 00:00:00 2001 From: ginuerzh Date: Thu, 4 Jul 2024 23:03:22 +0800 Subject: [PATCH] add observer/stats --- go.mod | 1 - go.sum | 2 - listener/option.go | 2 +- observer/stats/stats.go | 93 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 4 deletions(-) create mode 100644 observer/stats/stats.go diff --git a/go.mod b/go.mod index b9bffba..b374330 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.22 toolchain go1.22.2 require ( - github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5 github.com/vishvananda/netns v0.0.4 golang.org/x/sys v0.21.0 ) diff --git a/go.sum b/go.sum index 87ed461..d991bfe 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5 h1:IiZLdqGMx0lGVbDBy/N9LPu10qSlxm939EBvZ77qJNI= -github.com/go-gost/x v0.0.0-20240131151842-25dcf536c6f5/go.mod h1:FDqjiiPbCqJLU/wY+q2IZCBVcYnfTJTw+SJLrspLQms= github.com/vishvananda/netns v0.0.4 h1:Oeaw1EM2JMxD51g9uhtC0D7erkIjgmj8+JZc26m1YX8= github.com/vishvananda/netns v0.0.4/go.mod h1:SpkAiCQRtJ6TvvxPnOSyH3BMl6unz3xZlaprSwhNNJM= golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws= diff --git a/listener/option.go b/listener/option.go index 48c9fa1..8419e1a 100644 --- a/listener/option.go +++ b/listener/option.go @@ -10,7 +10,7 @@ import ( "github.com/go-gost/core/limiter/conn" "github.com/go-gost/core/limiter/traffic" "github.com/go-gost/core/logger" - "github.com/go-gost/x/stats" + "github.com/go-gost/core/observer/stats" ) type Options struct { diff --git a/observer/stats/stats.go b/observer/stats/stats.go new file mode 100644 index 0000000..d00bf75 --- /dev/null +++ b/observer/stats/stats.go @@ -0,0 +1,93 @@ +package stats + +import ( + "sync/atomic" + + "github.com/go-gost/core/observer" +) + +type Kind int + +const ( + KindTotalConns Kind = 1 + KindCurrentConns Kind = 2 + KindInputBytes Kind = 3 + KindOutputBytes Kind = 4 + KindTotalErrs Kind = 5 +) + +type Stats struct { + updated atomic.Bool + totalConns atomic.Uint64 + currentConns atomic.Int64 + inputBytes atomic.Uint64 + outputBytes atomic.Uint64 + totalErrs atomic.Uint64 +} + +func (s *Stats) Add(kind Kind, n int64) { + if s == nil { + return + } + switch kind { + case KindTotalConns: + if n > 0 { + s.totalConns.Add(uint64(n)) + } + case KindCurrentConns: + s.currentConns.Add(n) + case KindInputBytes: + if n > 0 { + s.inputBytes.Add(uint64(n)) + } + case KindOutputBytes: + if n > 0 { + s.outputBytes.Add(uint64(n)) + } + case KindTotalErrs: + if n > 0 { + s.totalErrs.Add(uint64(n)) + } + } + s.updated.Store(true) +} + +func (s *Stats) Get(kind Kind) uint64 { + if s == nil { + return 0 + } + + switch kind { + case KindTotalConns: + return s.totalConns.Load() + case KindCurrentConns: + return uint64(s.currentConns.Load()) + case KindInputBytes: + return s.inputBytes.Load() + case KindOutputBytes: + return s.outputBytes.Load() + case KindTotalErrs: + return s.totalErrs.Load() + } + return 0 +} + +func (s *Stats) IsUpdated() bool { + return s.updated.Swap(false) +} + +type StatsEvent struct { + Kind string + Service string + Client string + + TotalConns uint64 + CurrentConns uint64 + InputBytes uint64 + OutputBytes uint64 + TotalErrs uint64 +} + +func (StatsEvent) Type() observer.EventType { + return observer.EventStats +}