FILE / ScuroNeko/mtg

newstats/stats.go

Исходный файл и его история в репозитории.
FILE 07985cf418c3b6d7e0b59de2d29e7e927bedf401
Files
mtg/newstats/stats.go
T
2019-08-29 13:16:08 +03:00

94 lines
1.9 KiB
Go

package newstats
import (
"net"
"net/http"
"github.com/juju/errors"
"github.com/9seconds/mtg/newconfig"
"github.com/9seconds/mtg/newprotocol"
)
type Stats interface {
IngressTraffic(int)
EgressTraffic(int)
ClientConnected(newprotocol.ConnectionType, *net.TCPAddr)
ClientDisconnected(newprotocol.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 newprotocol.ConnectionType, addr *net.TCPAddr) {
for i := range m {
go m[i].ClientConnected(connectionType, addr)
}
}
func (m multiStats) ClientDisconnected(connectionType newprotocol.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
func Init() error {
mux := http.NewServeMux()
instanceJSON := newStatsJSON(mux)
instancePrometheus, err := newStatsPrometheus(mux)
if err != nil {
return errors.Annotate(err, "Cannot initialize Prometheus")
}
stats := []Stats{instanceJSON, instancePrometheus}
if newconfig.C.StatsdStats.Addr.IP != nil {
instanceStatsd, err := newStatsStatsd()
if err != nil {
return errors.Annotate(err, "Cannot initialize StatsD")
}
stats = append(stats, instanceStatsd)
}
listener, err := net.Listen("tcp", newconfig.C.StatsAddr.String())
if err != nil {
return errors.Annotate(err, "Cannot initialize stats server")
}
srv := http.Server{
Handler: mux,
}
go srv.Serve(listener) // nolint: errcheck
S = multiStats(stats)
return nil
}