add observer/stats
This commit is contained in:
parent
4e831b95e8
commit
30cc928705
1
go.mod
1
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
|
||||
)
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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 {
|
||||
|
93
observer/stats/stats.go
Normal file
93
observer/stats/stats.go
Normal file
@ -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
|
||||
}
|
Loading…
Reference in New Issue
Block a user