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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

statsd.go 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. package stats
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "github.com/9seconds/mtg/v2/events"
  7. "github.com/9seconds/mtg/v2/logger"
  8. "github.com/9seconds/mtg/v2/mtglib"
  9. statsd "github.com/smira/go-statsd"
  10. )
  11. type statsdProcessor struct {
  12. streams map[string]*streamInfo
  13. client *statsd.Client
  14. }
  15. func (s statsdProcessor) EventStart(evt mtglib.EventStart) {
  16. info := acquireStreamInfo()
  17. if evt.RemoteIP.To4() != nil {
  18. info.tags[TagIPFamily] = TagIPFamilyIPv4
  19. } else {
  20. info.tags[TagIPFamily] = TagIPFamilyIPv6
  21. }
  22. s.streams[evt.StreamID()] = info
  23. s.client.GaugeDelta(MetricClientConnections,
  24. 1,
  25. info.T(TagIPFamily))
  26. }
  27. func (s statsdProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
  28. info, ok := s.streams[evt.StreamID()]
  29. if !ok {
  30. return
  31. }
  32. info.tags[TagTelegramIP] = evt.RemoteIP.String()
  33. info.tags[TagDC] = strconv.Itoa(evt.DC)
  34. s.client.GaugeDelta(MetricTelegramConnections,
  35. 1,
  36. info.T(TagTelegramIP),
  37. info.T(TagDC))
  38. }
  39. func (s statsdProcessor) EventDomainFronting(evt mtglib.EventDomainFronting) {
  40. info, ok := s.streams[evt.StreamID()]
  41. if !ok {
  42. return
  43. }
  44. info.isDomainFronted = true
  45. s.client.Incr(MetricDomainFronting, 1)
  46. s.client.GaugeDelta(MetricDomainFrontingConnections,
  47. 1,
  48. info.T(TagIPFamily))
  49. }
  50. func (s statsdProcessor) EventTraffic(evt mtglib.EventTraffic) {
  51. info, ok := s.streams[evt.StreamID()]
  52. if !ok {
  53. return
  54. }
  55. directionTag := statsd.StringTag(TagDirection, getDirection(evt.IsRead))
  56. if info.isDomainFronted {
  57. s.client.Incr(MetricDomainFrontingTraffic,
  58. int64(evt.Traffic),
  59. directionTag)
  60. } else {
  61. s.client.Incr(MetricTelegramTraffic,
  62. int64(evt.Traffic),
  63. info.T(TagTelegramIP),
  64. info.T(TagDC),
  65. directionTag)
  66. }
  67. }
  68. func (s statsdProcessor) EventFinish(evt mtglib.EventFinish) {
  69. info, ok := s.streams[evt.StreamID()]
  70. if !ok {
  71. return
  72. }
  73. defer func() {
  74. delete(s.streams, evt.StreamID())
  75. releaseStreamInfo(info)
  76. }()
  77. s.client.GaugeDelta(MetricClientConnections,
  78. -1,
  79. info.T(TagIPFamily))
  80. if info.isDomainFronted {
  81. s.client.GaugeDelta(MetricDomainFrontingConnections,
  82. -1,
  83. info.T(TagIPFamily))
  84. } else if _, ok := info.tags[TagTelegramIP]; ok {
  85. s.client.GaugeDelta(MetricTelegramConnections,
  86. -1,
  87. info.T(TagTelegramIP),
  88. info.T(TagDC))
  89. }
  90. }
  91. func (s statsdProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {
  92. s.client.Incr(MetricConcurrencyLimited, 1)
  93. }
  94. func (s statsdProcessor) EventIPBlocklisted(_ mtglib.EventIPBlocklisted) {
  95. s.client.Incr(MetricIPBlocklisted, 1)
  96. }
  97. func (s statsdProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
  98. s.client.Incr(MetricReplayAttacks, 1)
  99. }
  100. func (s statsdProcessor) Shutdown() {
  101. events := make([]mtglib.EventFinish, 0, len(s.streams))
  102. for k := range s.streams {
  103. events = append(events, mtglib.NewEventFinish(k))
  104. }
  105. for i := range events {
  106. s.EventFinish(events[i])
  107. }
  108. }
  109. // StatsdFactory is a factory of events.Observers which dumps
  110. // information to statsd.
  111. //
  112. // Please beware that we support ONLY UDP endpoints there. And this
  113. // factory won't use mtglib.Network so it won't use a proxy if you
  114. // provide any. If you need it, I would recommend starting a local
  115. // statsd and route metrics further by features of the chosen server.
  116. type StatsdFactory struct {
  117. client *statsd.Client
  118. }
  119. // Close stops sending requests to statsd.
  120. func (s StatsdFactory) Close() error {
  121. return s.client.Close() // nolint: wrapcheck
  122. }
  123. // Make build a new observer.
  124. func (s StatsdFactory) Make() events.Observer {
  125. return statsdProcessor{
  126. client: s.client,
  127. streams: make(map[string]*streamInfo),
  128. }
  129. }
  130. // NewStatsd builds an events.ObserverFactory that sends events
  131. // to statsd.
  132. //
  133. // Valid tagFormats are 'datadog', 'influxdb' and 'graphite'.
  134. func NewStatsd(address string, log logger.StdLikeLogger,
  135. metricPrefix, tagFormat string) (StatsdFactory, error) {
  136. options := []statsd.Option{
  137. statsd.MetricPrefix(metricPrefix),
  138. statsd.Logger(log),
  139. }
  140. switch strings.ToLower(tagFormat) {
  141. case "datadog":
  142. options = append(options, statsd.TagStyle(statsd.TagFormatDatadog))
  143. case "influxdb":
  144. options = append(options, statsd.TagStyle(statsd.TagFormatInfluxDB))
  145. case "graphite":
  146. options = append(options, statsd.TagStyle(statsd.TagFormatGraphite))
  147. default:
  148. return StatsdFactory{}, fmt.Errorf("unknown tag format %s", tagFormat)
  149. }
  150. return StatsdFactory{
  151. client: statsd.NewClient(address, options...),
  152. }, nil
  153. }