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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

stats_statsd.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package stats
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "time"
  9. statsd "github.com/smira/go-statsd"
  10. "go.uber.org/zap"
  11. "github.com/9seconds/mtg/config"
  12. "github.com/9seconds/mtg/conntypes"
  13. )
  14. var (
  15. tagTrafficIngress = &statsStatsdTag{
  16. name: "ingress",
  17. tag: statsd.StringTag("type", "ingress"),
  18. }
  19. tagTrafficEgress = &statsStatsdTag{
  20. name: "egress",
  21. tag: statsd.StringTag("type", "egress"),
  22. }
  23. tagConnectionTypeAbridged = &statsStatsdTag{
  24. name: "abridged",
  25. tag: statsd.StringTag("type", "abridged"),
  26. }
  27. tagConnectionTypeIntermediate = &statsStatsdTag{
  28. name: "intermediate",
  29. tag: statsd.StringTag("type", "intermediate"),
  30. }
  31. tagConnectionTypeSecured = &statsStatsdTag{
  32. name: "secured",
  33. tag: statsd.StringTag("type", "secured"),
  34. }
  35. tagConnectionProtocol4 = &statsStatsdTag{
  36. name: "ipv4",
  37. tag: statsd.StringTag("protocol", "ipv4"),
  38. }
  39. tagConnectionProtocol6 = &statsStatsdTag{
  40. name: "ipv6",
  41. tag: statsd.StringTag("protocol", "ipv6"),
  42. }
  43. )
  44. type statsStatsdTag struct {
  45. tag statsd.Tag
  46. name string
  47. }
  48. type statsStatsdLogger struct {
  49. log *zap.SugaredLogger
  50. }
  51. func (s statsStatsdLogger) Printf(msg string, args ...interface{}) {
  52. s.log.Debugw(fmt.Sprintf(msg, args...))
  53. }
  54. type statsStatsd struct {
  55. seen map[string]struct{}
  56. seenMutex sync.RWMutex
  57. client *statsd.Client
  58. }
  59. func (s *statsStatsd) IngressTraffic(traffic int) {
  60. s.gauge("traffic", int64(traffic), tagTrafficIngress)
  61. }
  62. func (s *statsStatsd) EgressTraffic(traffic int) {
  63. s.gauge("traffic", int64(traffic), tagTrafficEgress)
  64. }
  65. func (s *statsStatsd) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  66. s.changeConnections(connectionType, addr, 1)
  67. }
  68. func (s *statsStatsd) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  69. s.changeConnections(connectionType, addr, -1)
  70. }
  71. func (s *statsStatsd) changeConnections(connectionType conntypes.ConnectionType, addr *net.TCPAddr, increment int64) {
  72. tags := make([]*statsStatsdTag, 0, 2)
  73. switch connectionType {
  74. case conntypes.ConnectionTypeAbridged:
  75. tags = append(tags, tagConnectionTypeAbridged)
  76. case conntypes.ConnectionTypeIntermediate:
  77. tags = append(tags, tagConnectionTypeIntermediate)
  78. default:
  79. tags = append(tags, tagConnectionTypeSecured)
  80. }
  81. if addr.IP.To4() == nil {
  82. tags = append(tags, tagConnectionProtocol6)
  83. } else {
  84. tags = append(tags, tagConnectionProtocol4)
  85. }
  86. s.gauge("connections", increment, tags...)
  87. }
  88. func (s *statsStatsd) TelegramConnected(dc conntypes.DC, addr *net.TCPAddr) {
  89. s.changeTelegramConnections(dc, addr, 1)
  90. }
  91. func (s *statsStatsd) TelegramDisconnected(dc conntypes.DC, addr *net.TCPAddr) {
  92. s.changeTelegramConnections(dc, addr, -1)
  93. }
  94. func (s *statsStatsd) changeTelegramConnections(dc conntypes.DC, addr *net.TCPAddr, increment int64) {
  95. tags := []*statsStatsdTag{
  96. {
  97. name: "dc" + strconv.Itoa(int(dc)),
  98. tag: statsd.IntTag("dc", int(dc)),
  99. },
  100. }
  101. if addr.IP.To4() == nil {
  102. tags = append(tags, tagConnectionProtocol6)
  103. } else {
  104. tags = append(tags, tagConnectionProtocol4)
  105. }
  106. s.gauge("telegram_connections", increment, tags...)
  107. }
  108. func (s *statsStatsd) Crash() {
  109. s.gauge("crashes", 1)
  110. }
  111. func (s *statsStatsd) ReplayDetected() {
  112. s.gauge("replay_attacks", 1)
  113. }
  114. func (s *statsStatsd) gauge(metric string, value int64, tags ...*statsStatsdTag) {
  115. key, tagList := s.prepareVals(metric, tags)
  116. s.initGauge(metric, key, tagList)
  117. s.client.GaugeDelta(metric, value, tagList...)
  118. }
  119. func (s *statsStatsd) prepareVals(metric string, tags []*statsStatsdTag) (string, []statsd.Tag) {
  120. tagList := make([]statsd.Tag, len(tags))
  121. builder := strings.Builder{}
  122. builder.WriteString(metric)
  123. for i, v := range tags {
  124. builder.WriteRune('.')
  125. builder.WriteString(v.name)
  126. tagList[i] = v.tag
  127. }
  128. return builder.String(), tagList
  129. }
  130. func (s *statsStatsd) initGauge(metric, key string, tags []statsd.Tag) {
  131. s.seenMutex.RLock()
  132. _, ok := s.seen[key]
  133. s.seenMutex.RUnlock()
  134. if ok {
  135. return
  136. }
  137. s.seenMutex.Lock()
  138. defer s.seenMutex.Unlock()
  139. if _, ok = s.seen[key]; !ok {
  140. s.seen[key] = struct{}{}
  141. s.client.Gauge(metric, 0, tags...)
  142. }
  143. }
  144. func newStatsStatsd() Interface {
  145. prefix := strings.TrimSuffix(config.C.StatsNamespace, ".") + "."
  146. logger := statsStatsdLogger{
  147. log: zap.S().Named("stats").Named("statsd"),
  148. }
  149. return &statsStatsd{
  150. seen: make(map[string]struct{}),
  151. client: statsd.NewClient(config.C.StatsdAddr.String(),
  152. statsd.SendLoopCount(2),
  153. statsd.ReconnectInterval(10*time.Second),
  154. statsd.Logger(logger),
  155. statsd.MetricPrefix(prefix),
  156. statsd.TagStyle(config.C.StatsdTagsFormat),
  157. ),
  158. }
  159. }