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_prometheus.go 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package stats
  2. import (
  3. "net"
  4. "net/http"
  5. "strconv"
  6. "github.com/prometheus/client_golang/prometheus"
  7. "github.com/prometheus/client_golang/prometheus/promhttp"
  8. "mtg/config"
  9. "mtg/conntypes"
  10. )
  11. type statsPrometheus struct {
  12. connections *prometheus.GaugeVec
  13. telegramConnections *prometheus.GaugeVec
  14. traffic *prometheus.GaugeVec
  15. crashes prometheus.Counter
  16. replayAttacks prometheus.Counter
  17. }
  18. func (s *statsPrometheus) IngressTraffic(traffic int) {
  19. s.traffic.WithLabelValues("ingress").Add(float64(traffic))
  20. }
  21. func (s *statsPrometheus) EgressTraffic(traffic int) {
  22. s.traffic.WithLabelValues("egress").Add(float64(traffic))
  23. }
  24. func (s *statsPrometheus) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  25. s.changeConnections(connectionType, addr, 1.0)
  26. }
  27. func (s *statsPrometheus) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  28. s.changeConnections(connectionType, addr, -1.0)
  29. }
  30. func (s *statsPrometheus) changeConnections(connectionType conntypes.ConnectionType,
  31. addr *net.TCPAddr,
  32. increment float64) {
  33. labels := [...]string{
  34. "intermediate",
  35. "ipv4",
  36. }
  37. switch connectionType {
  38. case conntypes.ConnectionTypeAbridged:
  39. labels[0] = "abridged"
  40. case conntypes.ConnectionTypeSecure:
  41. labels[0] = "secured"
  42. }
  43. if addr.IP.To4() == nil {
  44. labels[1] = "ipv6" // nolint: goconst
  45. }
  46. s.connections.WithLabelValues(labels[:]...).Add(increment)
  47. }
  48. func (s *statsPrometheus) TelegramConnected(dc conntypes.DC, addr *net.TCPAddr) {
  49. s.changeTelegramConnections(dc, addr, 1.0)
  50. }
  51. func (s *statsPrometheus) TelegramDisconnected(dc conntypes.DC, addr *net.TCPAddr) {
  52. s.changeTelegramConnections(dc, addr, -1.0)
  53. }
  54. func (s *statsPrometheus) changeTelegramConnections(dc conntypes.DC, addr *net.TCPAddr, increment float64) {
  55. labels := [...]string{
  56. strconv.Itoa(int(dc)),
  57. "ipv4",
  58. }
  59. if addr.IP.To4() == nil {
  60. labels[1] = "ipv6"
  61. }
  62. s.telegramConnections.WithLabelValues(labels[:]...).Add(increment)
  63. }
  64. func (s *statsPrometheus) Crash() {
  65. s.crashes.Inc()
  66. }
  67. func (s *statsPrometheus) ReplayDetected() {
  68. s.replayAttacks.Inc()
  69. }
  70. func newStatsPrometheus(mux *http.ServeMux) Interface {
  71. registry := prometheus.NewPedanticRegistry()
  72. instance := &statsPrometheus{
  73. connections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  74. Namespace: config.C.StatsNamespace,
  75. Name: "connections",
  76. Help: "Current number of client connections to the proxy.",
  77. }, []string{"type", "protocol"}),
  78. telegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  79. Namespace: config.C.StatsNamespace,
  80. Name: "telegram_connections",
  81. Help: "Current number of telegram connections established by this proxy.",
  82. }, []string{"dc", "protocol"}),
  83. traffic: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  84. Namespace: config.C.StatsNamespace,
  85. Name: "traffic",
  86. Help: "Traffic passed through the proxy in bytes.",
  87. }, []string{"direction"}),
  88. crashes: prometheus.NewCounter(prometheus.CounterOpts{
  89. Namespace: config.C.StatsNamespace,
  90. Name: "crashes",
  91. Help: "How many crashes happened.",
  92. }),
  93. replayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
  94. Namespace: config.C.StatsNamespace,
  95. Name: "replay_attacks",
  96. Help: "How many replay attacks were prevented.",
  97. }),
  98. }
  99. registry.MustRegister(instance.connections)
  100. registry.MustRegister(instance.telegramConnections)
  101. registry.MustRegister(instance.traffic)
  102. registry.MustRegister(instance.crashes)
  103. registry.MustRegister(instance.replayAttacks)
  104. handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
  105. mux.Handle("/", handler)
  106. return instance
  107. }