Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stats.go 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package newstats
  2. import (
  3. "net"
  4. "net/http"
  5. "github.com/juju/errors"
  6. "github.com/9seconds/mtg/newconfig"
  7. "github.com/9seconds/mtg/newprotocol"
  8. )
  9. type Stats interface {
  10. IngressTraffic(int)
  11. EgressTraffic(int)
  12. ClientConnected(newprotocol.ConnectionType, *net.TCPAddr)
  13. ClientDisconnected(newprotocol.ConnectionType, *net.TCPAddr)
  14. Crash()
  15. AntiReplayDetected()
  16. }
  17. type multiStats []Stats
  18. func (m multiStats) IngressTraffic(traffic int) {
  19. for i := range m {
  20. go m[i].IngressTraffic(traffic)
  21. }
  22. }
  23. func (m multiStats) EgressTraffic(traffic int) {
  24. for i := range m {
  25. go m[i].EgressTraffic(traffic)
  26. }
  27. }
  28. func (m multiStats) ClientConnected(connectionType newprotocol.ConnectionType, addr *net.TCPAddr) {
  29. for i := range m {
  30. go m[i].ClientConnected(connectionType, addr)
  31. }
  32. }
  33. func (m multiStats) ClientDisconnected(connectionType newprotocol.ConnectionType, addr *net.TCPAddr) {
  34. for i := range m {
  35. go m[i].ClientDisconnected(connectionType, addr)
  36. }
  37. }
  38. func (m multiStats) Crash() {
  39. for i := range m {
  40. go m[i].Crash()
  41. }
  42. }
  43. func (m multiStats) AntiReplayDetected() {
  44. for i := range m {
  45. go m[i].AntiReplayDetected()
  46. }
  47. }
  48. var S Stats
  49. func Init() error {
  50. mux := http.NewServeMux()
  51. instanceJSON := newStatsJSON(mux)
  52. instancePrometheus, err := newStatsPrometheus(mux)
  53. if err != nil {
  54. return errors.Annotate(err, "Cannot initialize Prometheus")
  55. }
  56. stats := []Stats{instanceJSON, instancePrometheus}
  57. if newconfig.C.StatsdStats.Addr.IP != nil {
  58. instanceStatsd, err := newStatsStatsd()
  59. if err != nil {
  60. return errors.Annotate(err, "Cannot initialize StatsD")
  61. }
  62. stats = append(stats, instanceStatsd)
  63. }
  64. listener, err := net.Listen("tcp", newconfig.C.StatsAddr.String())
  65. if err != nil {
  66. return errors.Annotate(err, "Cannot initialize stats server")
  67. }
  68. srv := http.Server{
  69. Handler: mux,
  70. }
  71. go srv.Serve(listener) // nolint: errcheck
  72. S = multiStats(stats)
  73. return nil
  74. }