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 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. package stats
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "gopkg.in/alexcesaro/statsd.v2"
  7. "github.com/9seconds/mtg/config"
  8. "github.com/9seconds/mtg/conntypes"
  9. )
  10. type statsStatsd struct {
  11. client *statsd.Client
  12. }
  13. func (s *statsStatsd) IngressTraffic(traffic int) {
  14. s.client.Count("traffic.ingress", traffic)
  15. }
  16. func (s *statsStatsd) EgressTraffic(traffic int) {
  17. s.client.Count("traffic.egress", traffic)
  18. }
  19. func (s *statsStatsd) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  20. s.changeConnections(connectionType, addr, 1)
  21. }
  22. func (s *statsStatsd) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  23. s.changeConnections(connectionType, addr, -1)
  24. }
  25. func (s *statsStatsd) changeConnections(connectionType conntypes.ConnectionType, addr *net.TCPAddr, value int) {
  26. var labels [3]string
  27. labels[0] = "connections"
  28. switch connectionType {
  29. case conntypes.ConnectionTypeAbridged:
  30. labels[1] = "abridged"
  31. case conntypes.ConnectionTypeSecure:
  32. labels[1] = "secured"
  33. default:
  34. labels[1] = "intermediate"
  35. }
  36. labels[2] = "ipv4"
  37. if addr.IP.To4() == nil {
  38. labels[2] = "ipv6"
  39. }
  40. s.client.Count(strings.Join(labels[:], "."), value)
  41. }
  42. func (s *statsStatsd) Crash() {
  43. s.client.Increment("crashes")
  44. }
  45. func (s *statsStatsd) AntiReplayDetected() {
  46. s.client.Increment("anti_replays")
  47. }
  48. func newStatsStatsd() (Stats, error) {
  49. options := []statsd.Option{
  50. statsd.Prefix(config.C.StatsdStats.Prefix),
  51. statsd.Network(config.C.StatsdStats.Addr.Network()),
  52. statsd.Address(config.C.StatsdStats.Addr.String()),
  53. statsd.TagsFormat(config.C.StatsdStats.TagsFormat),
  54. }
  55. if len(config.C.StatsdStats.Tags) > 0 {
  56. tags := make([]string, len(config.C.StatsdStats.Tags)*2)
  57. for k, v := range config.C.StatsdStats.Tags {
  58. tags = append(tags, k, v)
  59. }
  60. options = append(options, statsd.Tags(tags...))
  61. }
  62. client, err := statsd.New(options...)
  63. if err != nil {
  64. return nil, fmt.Errorf("cannot initialize a client: %w", err)
  65. }
  66. return &statsStatsd{
  67. client: client,
  68. }, nil
  69. }