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
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

stats_statsd.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package stats
  2. import (
  3. "fmt"
  4. "net"
  5. "strconv"
  6. "strings"
  7. "gopkg.in/alexcesaro/statsd.v2"
  8. "mtg/config"
  9. "mtg/conntypes"
  10. )
  11. type statsStatsd struct {
  12. client *statsd.Client
  13. }
  14. func (s *statsStatsd) IngressTraffic(traffic int) {
  15. s.client.Count("traffic.ingress", traffic)
  16. }
  17. func (s *statsStatsd) EgressTraffic(traffic int) {
  18. s.client.Count("traffic.egress", traffic)
  19. }
  20. func (s *statsStatsd) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  21. s.changeConnections(connectionType, addr, 1)
  22. }
  23. func (s *statsStatsd) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  24. s.changeConnections(connectionType, addr, -1)
  25. }
  26. func (s *statsStatsd) changeConnections(connectionType conntypes.ConnectionType, addr *net.TCPAddr, value int) {
  27. labels := [...]string{
  28. "connections",
  29. "intermediate",
  30. "ipv4",
  31. }
  32. switch connectionType {
  33. case conntypes.ConnectionTypeAbridged:
  34. labels[1] = "abridged"
  35. case conntypes.ConnectionTypeSecure:
  36. labels[1] = "secured"
  37. }
  38. if addr.IP.To4() == nil {
  39. labels[2] = "ipv6"
  40. }
  41. s.client.Count(strings.Join(labels[:], "."), value)
  42. }
  43. func (s *statsStatsd) TelegramConnected(dc conntypes.DC, addr *net.TCPAddr) {
  44. s.changeTelegramConnections(dc, addr, 1)
  45. }
  46. func (s *statsStatsd) TelegramDisconnected(dc conntypes.DC, addr *net.TCPAddr) {
  47. s.changeTelegramConnections(dc, addr, -1)
  48. }
  49. func (s *statsStatsd) changeTelegramConnections(dc conntypes.DC, addr *net.TCPAddr, value int) {
  50. labels := [...]string{
  51. "telegram_connections",
  52. strconv.Itoa(int(dc)),
  53. "ipv4",
  54. }
  55. if addr.IP.To4() == nil {
  56. labels[2] = "ipv6"
  57. }
  58. s.client.Count(strings.Join(labels[:], "."), value)
  59. }
  60. func (s *statsStatsd) Crash() {
  61. s.client.Increment("crashes")
  62. }
  63. func (s *statsStatsd) ReplayDetected() {
  64. s.client.Increment("replay_attacks")
  65. }
  66. func newStatsStatsd() (Interface, error) {
  67. options := []statsd.Option{
  68. statsd.Prefix(config.C.StatsNamespace),
  69. statsd.Network(config.C.StatsdNetwork),
  70. statsd.Address(config.C.StatsdAddr.String()),
  71. statsd.TagsFormat(config.C.StatsdTagsFormat),
  72. }
  73. if len(config.C.StatsdTags) > 0 {
  74. tags := make([]string, len(config.C.StatsdTags)*2)
  75. for k, v := range config.C.StatsdTags {
  76. tags = append(tags, k, v)
  77. }
  78. options = append(options, statsd.Tags(tags...))
  79. }
  80. client, err := statsd.New(options...)
  81. if err != nil {
  82. return nil, fmt.Errorf("cannot initialize a client: %w", err)
  83. }
  84. return &statsStatsd{
  85. client: client,
  86. }, nil
  87. }