Add new stats metric

This commit is contained in:
9seconds
2019-10-08 11:55:57 +03:00
parent d44474012a
commit 6c7edfb7db
21 changed files with 477 additions and 510 deletions
+5 -54
View File
@@ -7,69 +7,20 @@ import (
"net/http"
"github.com/9seconds/mtg/config"
"github.com/9seconds/mtg/conntypes"
)
type Stats interface {
IngressTraffic(int)
EgressTraffic(int)
ClientConnected(conntypes.ConnectionType, *net.TCPAddr)
ClientDisconnected(conntypes.ConnectionType, *net.TCPAddr)
Crash()
AntiReplayDetected()
}
type multiStats []Stats
func (m multiStats) IngressTraffic(traffic int) {
for i := range m {
go m[i].IngressTraffic(traffic)
}
}
func (m multiStats) EgressTraffic(traffic int) {
for i := range m {
go m[i].EgressTraffic(traffic)
}
}
func (m multiStats) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
for i := range m {
go m[i].ClientConnected(connectionType, addr)
}
}
func (m multiStats) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
for i := range m {
go m[i].ClientDisconnected(connectionType, addr)
}
}
func (m multiStats) Crash() {
for i := range m {
go m[i].Crash()
}
}
func (m multiStats) AntiReplayDetected() {
for i := range m {
go m[i].AntiReplayDetected()
}
}
var S Stats
var Stats Interface
func Init(ctx context.Context) error {
mux := http.NewServeMux()
instanceJSON := newStatsJSON(mux)
instancePrometheus, err := newStatsPrometheus(mux)
if err != nil {
return fmt.Errorf("cannot initialize prometheus: %w", err)
}
stats := []Stats{instanceJSON, instancePrometheus}
if config.C.StatsdStats.Addr.IP != nil {
stats := []Interface{instancePrometheus}
if config.C.StatsdAddr != nil {
instanceStatsd, err := newStatsStatsd()
if err != nil {
return fmt.Errorf("cannot inialize statsd: %w", err)
@@ -77,7 +28,7 @@ func Init(ctx context.Context) error {
stats = append(stats, instanceStatsd)
}
listener, err := net.Listen("tcp", config.C.StatsAddr.String())
listener, err := net.Listen("tcp", config.C.StatsBind.String())
if err != nil {
return fmt.Errorf("cannot initialize stats server: %w", err)
}
@@ -91,7 +42,7 @@ func Init(ctx context.Context) error {
srv.Shutdown(context.Background()) // nolint: errcheck
}()
S = multiStats(stats)
Stats = multiStats(stats)
return nil
}