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
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

statsd.go 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package stats
  2. import (
  3. "fmt"
  4. "strconv"
  5. "strings"
  6. "time"
  7. "github.com/9seconds/mtg/v2/events"
  8. "github.com/9seconds/mtg/v2/logger"
  9. "github.com/9seconds/mtg/v2/mtglib"
  10. statsd "github.com/smira/go-statsd"
  11. )
  12. type statsdProcessor struct {
  13. streams map[string]streamInfo
  14. client *statsd.Client
  15. }
  16. func (s statsdProcessor) EventStart(evt mtglib.EventStart) {
  17. info := acquireStreamInfo()
  18. if evt.RemoteIP.To4() != nil {
  19. info[TagIPFamily] = TagIPFamilyIPv4
  20. } else {
  21. info[TagIPFamily] = TagIPFamilyIPv6
  22. }
  23. s.streams[evt.StreamID()] = info
  24. s.client.GaugeDelta(MetricClientConnections,
  25. 1,
  26. info.T(TagIPFamily))
  27. }
  28. func (s statsdProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
  29. info, ok := s.streams[evt.StreamID()]
  30. if !ok {
  31. return
  32. }
  33. info[TagTelegramIP] = evt.RemoteIP.String()
  34. info[TagDC] = strconv.Itoa(evt.DC)
  35. s.client.GaugeDelta(MetricTelegramConnections,
  36. 1,
  37. info.T(TagTelegramIP),
  38. info.T(TagDC))
  39. }
  40. func (s statsdProcessor) EventTraffic(evt mtglib.EventTraffic) {
  41. info, ok := s.streams[evt.StreamID()]
  42. if !ok {
  43. return
  44. }
  45. s.client.Incr(MetricTelegramTraffic,
  46. int64(evt.Traffic),
  47. info.T(TagTelegramIP),
  48. info.T(TagDC),
  49. statsd.StringTag(TagDirection, getDirection(evt.IsRead)))
  50. }
  51. func (s statsdProcessor) EventFinish(evt mtglib.EventFinish) {
  52. info, ok := s.streams[evt.StreamID()]
  53. if !ok {
  54. return
  55. }
  56. defer func() {
  57. delete(s.streams, evt.StreamID())
  58. releaseStreamInfo(info)
  59. }()
  60. s.client.GaugeDelta(MetricClientConnections,
  61. -1,
  62. info.T(TagIPFamily))
  63. if _, ok := info[TagTelegramIP]; ok {
  64. s.client.GaugeDelta(MetricTelegramConnections,
  65. -1,
  66. info.T(TagTelegramIP),
  67. info.T(TagDC))
  68. }
  69. }
  70. func (s statsdProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {
  71. s.client.Incr(MetricConcurrencyLimited, 1)
  72. }
  73. func (s statsdProcessor) EventIPBlocklisted(evt mtglib.EventIPBlocklisted) {
  74. s.client.Incr(MetricIPBlocklisted, 1)
  75. }
  76. func (s statsdProcessor) Shutdown() {
  77. now := time.Now()
  78. events := make([]mtglib.EventFinish, 0, len(s.streams))
  79. for k := range s.streams {
  80. events = append(events, mtglib.EventFinish{
  81. CreatedAt: now,
  82. ConnID: k,
  83. })
  84. }
  85. for i := range events {
  86. s.EventFinish(events[i])
  87. }
  88. }
  89. type StatsdFactory struct {
  90. client *statsd.Client
  91. }
  92. func (s StatsdFactory) Close() error {
  93. return s.client.Close()
  94. }
  95. func (s StatsdFactory) Make() events.Observer {
  96. return statsdProcessor{
  97. client: s.client,
  98. streams: make(map[string]streamInfo),
  99. }
  100. }
  101. func NewStatsd(address string, log logger.StdLikeLogger,
  102. metricPrefix, tagFormat string) (StatsdFactory, error) {
  103. options := []statsd.Option{
  104. statsd.MetricPrefix(metricPrefix),
  105. statsd.Logger(log),
  106. }
  107. switch strings.ToLower(tagFormat) {
  108. case "datadog":
  109. options = append(options, statsd.TagStyle(statsd.TagFormatDatadog))
  110. case "influxdb":
  111. options = append(options, statsd.TagStyle(statsd.TagFormatInfluxDB))
  112. case "graphite":
  113. options = append(options, statsd.TagStyle(statsd.TagFormatGraphite))
  114. default:
  115. return StatsdFactory{}, fmt.Errorf("unknown tag format %s", tagFormat)
  116. }
  117. return StatsdFactory{
  118. client: statsd.NewClient(address, options...),
  119. }, nil
  120. }