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
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

prometheus.go 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. package stats
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "strconv"
  7. "github.com/9seconds/mtg/v2/events"
  8. "github.com/9seconds/mtg/v2/mtglib"
  9. "github.com/prometheus/client_golang/prometheus"
  10. "github.com/prometheus/client_golang/prometheus/promhttp"
  11. )
  12. type prometheusProcessor struct {
  13. streams map[string]*streamInfo
  14. factory *PrometheusFactory
  15. }
  16. func (p prometheusProcessor) EventStart(evt mtglib.EventStart) {
  17. info := acquireStreamInfo()
  18. if evt.RemoteIP.To4() != nil {
  19. info.tags[TagIPFamily] = TagIPFamilyIPv4
  20. } else {
  21. info.tags[TagIPFamily] = TagIPFamilyIPv6
  22. }
  23. p.streams[evt.StreamID()] = info
  24. p.factory.metricClientConnections.
  25. WithLabelValues(info.tags[TagIPFamily]).
  26. Inc()
  27. }
  28. func (p prometheusProcessor) EventConnectedToDC(evt mtglib.EventConnectedToDC) {
  29. info, ok := p.streams[evt.StreamID()]
  30. if !ok {
  31. return
  32. }
  33. info.tags[TagTelegramIP] = evt.RemoteIP.String()
  34. info.tags[TagDC] = strconv.Itoa(evt.DC)
  35. p.factory.metricTelegramConnections.
  36. WithLabelValues(info.tags[TagTelegramIP], info.tags[TagDC]).
  37. Inc()
  38. }
  39. func (p prometheusProcessor) EventDomainFronting(evt mtglib.EventDomainFronting) {
  40. info, ok := p.streams[evt.StreamID()]
  41. if !ok {
  42. return
  43. }
  44. info.isDomainFronted = true
  45. p.factory.metricDomainFronting.Inc()
  46. p.factory.metricDomainFrontingConnections.
  47. WithLabelValues(info.tags[TagIPFamily]).
  48. Inc()
  49. }
  50. func (p prometheusProcessor) EventTraffic(evt mtglib.EventTraffic) {
  51. info, ok := p.streams[evt.StreamID()]
  52. if !ok {
  53. return
  54. }
  55. direction := getDirection(evt.IsRead)
  56. if info.isDomainFronted {
  57. p.factory.metricDomainFrontingTraffic.
  58. WithLabelValues(direction).
  59. Add(float64(evt.Traffic))
  60. } else {
  61. p.factory.metricTelegramTraffic.
  62. WithLabelValues(info.tags[TagTelegramIP], info.tags[TagDC], direction).
  63. Add(float64(evt.Traffic))
  64. }
  65. }
  66. func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
  67. info, ok := p.streams[evt.StreamID()]
  68. if !ok {
  69. return
  70. }
  71. defer func() {
  72. delete(p.streams, evt.StreamID())
  73. releaseStreamInfo(info)
  74. }()
  75. p.factory.metricClientConnections.
  76. WithLabelValues(info.tags[TagIPFamily]).
  77. Dec()
  78. if info.isDomainFronted {
  79. p.factory.metricDomainFrontingConnections.
  80. WithLabelValues(info.tags[TagIPFamily]).
  81. Dec()
  82. } else if telegramIP, ok := info.tags[TagTelegramIP]; ok {
  83. p.factory.metricTelegramConnections.
  84. WithLabelValues(telegramIP, info.tags[TagDC]).
  85. Dec()
  86. }
  87. }
  88. func (p prometheusProcessor) EventConcurrencyLimited(_ mtglib.EventConcurrencyLimited) {
  89. p.factory.metricConcurrencyLimited.Inc()
  90. }
  91. func (p prometheusProcessor) EventIPBlocklisted(_ mtglib.EventIPBlocklisted) {
  92. p.factory.metricIPBlocklisted.Inc()
  93. }
  94. func (p prometheusProcessor) EventReplayAttack(_ mtglib.EventReplayAttack) {
  95. p.factory.metricReplayAttacks.Inc()
  96. }
  97. func (p prometheusProcessor) Shutdown() {
  98. for _, v := range p.streams {
  99. releaseStreamInfo(v)
  100. }
  101. p.streams = make(map[string]*streamInfo)
  102. }
  103. type PrometheusFactory struct {
  104. httpServer *http.Server
  105. metricClientConnections *prometheus.GaugeVec
  106. metricTelegramConnections *prometheus.GaugeVec
  107. metricDomainFrontingConnections *prometheus.GaugeVec
  108. metricTelegramTraffic *prometheus.CounterVec
  109. metricDomainFrontingTraffic *prometheus.CounterVec
  110. metricDomainFronting prometheus.Counter
  111. metricConcurrencyLimited prometheus.Counter
  112. metricIPBlocklisted prometheus.Counter
  113. metricReplayAttacks prometheus.Counter
  114. }
  115. func (p *PrometheusFactory) Make() events.Observer {
  116. return prometheusProcessor{
  117. streams: make(map[string]*streamInfo),
  118. factory: p,
  119. }
  120. }
  121. func (p *PrometheusFactory) Serve(listener net.Listener) error {
  122. return p.httpServer.Serve(listener)
  123. }
  124. func (p *PrometheusFactory) Close() error {
  125. return p.httpServer.Shutdown(context.Background())
  126. }
  127. func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint: funlen
  128. registry := prometheus.NewPedanticRegistry()
  129. httpHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
  130. EnableOpenMetrics: true,
  131. })
  132. mux := http.NewServeMux()
  133. mux.Handle(httpPath, httpHandler)
  134. factory := &PrometheusFactory{
  135. httpServer: &http.Server{
  136. Handler: mux,
  137. },
  138. metricClientConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  139. Namespace: metricPrefix,
  140. Name: MetricClientConnections,
  141. Help: "A number of actively processing client connections.",
  142. }, []string{TagIPFamily}),
  143. metricTelegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  144. Namespace: metricPrefix,
  145. Name: MetricTelegramConnections,
  146. Help: "A number of connections to Telegram servers.",
  147. }, []string{TagTelegramIP, TagDC}),
  148. metricDomainFrontingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  149. Namespace: metricPrefix,
  150. Name: MetricDomainFrontingConnections,
  151. Help: "A number of connections which talk to front domain.",
  152. }, []string{TagIPFamily}),
  153. metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
  154. Namespace: metricPrefix,
  155. Name: MetricTelegramTraffic,
  156. Help: "Traffic which is generated talking with Telegram servers.",
  157. }, []string{TagTelegramIP, TagDC, TagDirection}),
  158. metricDomainFrontingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
  159. Namespace: metricPrefix,
  160. Name: MetricDomainFrontingTraffic,
  161. Help: "Traffic which is generated talking with front domain.",
  162. }, []string{TagDirection}),
  163. metricDomainFronting: prometheus.NewCounter(prometheus.CounterOpts{
  164. Namespace: metricPrefix,
  165. Name: MetricDomainFronting,
  166. Help: "A number of routings to front domain.",
  167. }),
  168. metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
  169. Namespace: metricPrefix,
  170. Name: MetricConcurrencyLimited,
  171. Help: "A number of sessions that were rejected by concurrency limiter.",
  172. }),
  173. metricIPBlocklisted: prometheus.NewCounter(prometheus.CounterOpts{
  174. Namespace: metricPrefix,
  175. Name: MetricIPBlocklisted,
  176. Help: "A number of rejected sessions due to ip blocklisting.",
  177. }),
  178. metricReplayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
  179. Namespace: metricPrefix,
  180. Name: MetricReplayAttacks,
  181. Help: "A number of detected replay attacks.",
  182. }),
  183. }
  184. registry.MustRegister(factory.metricClientConnections)
  185. registry.MustRegister(factory.metricTelegramConnections)
  186. registry.MustRegister(factory.metricDomainFrontingConnections)
  187. registry.MustRegister(factory.metricTelegramTraffic)
  188. registry.MustRegister(factory.metricDomainFrontingTraffic)
  189. registry.MustRegister(factory.metricDomainFronting)
  190. registry.MustRegister(factory.metricConcurrencyLimited)
  191. registry.MustRegister(factory.metricIPBlocklisted)
  192. registry.MustRegister(factory.metricReplayAttacks)
  193. return factory
  194. }