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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package stats
  2. import (
  3. "net"
  4. "net/http"
  5. "github.com/juju/errors"
  6. "github.com/prometheus/client_golang/prometheus"
  7. "github.com/prometheus/client_golang/prometheus/promhttp"
  8. "github.com/9seconds/mtg/config"
  9. "github.com/9seconds/mtg/conntypes"
  10. )
  11. type statsPrometheus struct {
  12. connections *prometheus.GaugeVec
  13. traffic *prometheus.GaugeVec
  14. crashes prometheus.Gauge
  15. antiReplays prometheus.Gauge
  16. }
  17. func (s *statsPrometheus) IngressTraffic(traffic int) {
  18. s.traffic.WithLabelValues("ingress").Add(float64(traffic))
  19. }
  20. func (s *statsPrometheus) EgressTraffic(traffic int) {
  21. s.traffic.WithLabelValues("egress").Add(float64(traffic))
  22. }
  23. func (s *statsPrometheus) ClientConnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  24. s.changeConnections(connectionType, addr, 1.0)
  25. }
  26. func (s *statsPrometheus) ClientDisconnected(connectionType conntypes.ConnectionType, addr *net.TCPAddr) {
  27. s.changeConnections(connectionType, addr, -1.0)
  28. }
  29. func (s *statsPrometheus) changeConnections(connectionType conntypes.ConnectionType,
  30. addr *net.TCPAddr,
  31. increment float64) {
  32. var labels [2]string
  33. switch connectionType {
  34. case conntypes.ConnectionTypeAbridged:
  35. labels[0] = "abridged"
  36. case conntypes.ConnectionTypeSecure:
  37. labels[0] = "secured"
  38. default:
  39. labels[0] = "intermediate"
  40. }
  41. labels[1] = "ipv4"
  42. if addr.IP.To4() == nil {
  43. labels[1] = "ipv6"
  44. }
  45. s.connections.WithLabelValues(labels[:]...).Add(increment)
  46. }
  47. func (s *statsPrometheus) Crash() {
  48. s.crashes.Inc()
  49. }
  50. func (s *statsPrometheus) AntiReplayDetected() {
  51. s.antiReplays.Inc()
  52. }
  53. func newStatsPrometheus(mux *http.ServeMux) (Stats, error) {
  54. registry := prometheus.NewRegistry()
  55. instance := &statsPrometheus{
  56. connections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  57. Namespace: config.C.PrometheusStats.Prefix,
  58. Name: "connections",
  59. Help: "Current number of connections to the proxy.",
  60. }, []string{"type", "protocol"}),
  61. traffic: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  62. Namespace: config.C.PrometheusStats.Prefix,
  63. Name: "traffic",
  64. Help: "Traffic passed through the proxy in bytes.",
  65. }, []string{"direction"}),
  66. crashes: prometheus.NewGauge(prometheus.GaugeOpts{
  67. Namespace: config.C.PrometheusStats.Prefix,
  68. Name: "crashes",
  69. Help: "How many crashes happened.",
  70. }),
  71. antiReplays: prometheus.NewGauge(prometheus.GaugeOpts{
  72. Namespace: config.C.PrometheusStats.Prefix,
  73. Name: "anti_replays",
  74. Help: "How many anti replay attacks were prevented.",
  75. }),
  76. }
  77. if err := registry.Register(instance.connections); err != nil {
  78. return nil, errors.Annotate(err, "Cannot register metrics for connections")
  79. }
  80. if err := registry.Register(instance.traffic); err != nil {
  81. return nil, errors.Annotate(err, "Cannot register metrics for traffic")
  82. }
  83. if err := registry.Register(instance.crashes); err != nil {
  84. return nil, errors.Annotate(err, "Cannot register metrics for crashes")
  85. }
  86. if err := registry.Register(instance.antiReplays); err != nil {
  87. return nil, errors.Annotate(err, "Cannot register metrics for anti replays")
  88. }
  89. handler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
  90. mux.Handle("/prometheus", handler)
  91. return instance, nil
  92. }