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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. // PrometheusFactory is a factory of events.Observers which collect
  104. // information in a format suitable for Prometheus.
  105. //
  106. // This factory can also serve on a given listener. In that case it
  107. // starts HTTP server with a single endpoint - a Prometheus-compatible
  108. // scrape output.
  109. type PrometheusFactory struct {
  110. httpServer *http.Server
  111. metricClientConnections *prometheus.GaugeVec
  112. metricTelegramConnections *prometheus.GaugeVec
  113. metricDomainFrontingConnections *prometheus.GaugeVec
  114. metricTelegramTraffic *prometheus.CounterVec
  115. metricDomainFrontingTraffic *prometheus.CounterVec
  116. metricDomainFronting prometheus.Counter
  117. metricConcurrencyLimited prometheus.Counter
  118. metricIPBlocklisted prometheus.Counter
  119. metricReplayAttacks prometheus.Counter
  120. }
  121. // Make builds a new observer.
  122. func (p *PrometheusFactory) Make() events.Observer {
  123. return prometheusProcessor{
  124. streams: make(map[string]*streamInfo),
  125. factory: p,
  126. }
  127. }
  128. // Serve starts an HTTP server on a given listener.
  129. func (p *PrometheusFactory) Serve(listener net.Listener) error {
  130. return p.httpServer.Serve(listener)
  131. }
  132. // Close stops a factory. Please pay attention that underlying listener
  133. // is not closed.
  134. func (p *PrometheusFactory) Close() error {
  135. return p.httpServer.Shutdown(context.Background())
  136. }
  137. // NewPrometheus builds an events.ObserverFactory which can serve HTTP
  138. // endpoint with Prometheus scrape data.
  139. func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint: funlen
  140. registry := prometheus.NewPedanticRegistry()
  141. httpHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
  142. EnableOpenMetrics: true,
  143. })
  144. mux := http.NewServeMux()
  145. mux.Handle(httpPath, httpHandler)
  146. factory := &PrometheusFactory{
  147. httpServer: &http.Server{
  148. Handler: mux,
  149. },
  150. metricClientConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  151. Namespace: metricPrefix,
  152. Name: MetricClientConnections,
  153. Help: "A number of actively processing client connections.",
  154. }, []string{TagIPFamily}),
  155. metricTelegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  156. Namespace: metricPrefix,
  157. Name: MetricTelegramConnections,
  158. Help: "A number of connections to Telegram servers.",
  159. }, []string{TagTelegramIP, TagDC}),
  160. metricDomainFrontingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  161. Namespace: metricPrefix,
  162. Name: MetricDomainFrontingConnections,
  163. Help: "A number of connections which talk to front domain.",
  164. }, []string{TagIPFamily}),
  165. metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
  166. Namespace: metricPrefix,
  167. Name: MetricTelegramTraffic,
  168. Help: "Traffic which is generated talking with Telegram servers.",
  169. }, []string{TagTelegramIP, TagDC, TagDirection}),
  170. metricDomainFrontingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
  171. Namespace: metricPrefix,
  172. Name: MetricDomainFrontingTraffic,
  173. Help: "Traffic which is generated talking with front domain.",
  174. }, []string{TagDirection}),
  175. metricDomainFronting: prometheus.NewCounter(prometheus.CounterOpts{
  176. Namespace: metricPrefix,
  177. Name: MetricDomainFronting,
  178. Help: "A number of routings to front domain.",
  179. }),
  180. metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
  181. Namespace: metricPrefix,
  182. Name: MetricConcurrencyLimited,
  183. Help: "A number of sessions that were rejected by concurrency limiter.",
  184. }),
  185. metricIPBlocklisted: prometheus.NewCounter(prometheus.CounterOpts{
  186. Namespace: metricPrefix,
  187. Name: MetricIPBlocklisted,
  188. Help: "A number of rejected sessions due to ip blocklisting.",
  189. }),
  190. metricReplayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
  191. Namespace: metricPrefix,
  192. Name: MetricReplayAttacks,
  193. Help: "A number of detected replay attacks.",
  194. }),
  195. }
  196. registry.MustRegister(factory.metricClientConnections)
  197. registry.MustRegister(factory.metricTelegramConnections)
  198. registry.MustRegister(factory.metricDomainFrontingConnections)
  199. registry.MustRegister(factory.metricTelegramTraffic)
  200. registry.MustRegister(factory.metricDomainFrontingTraffic)
  201. registry.MustRegister(factory.metricDomainFronting)
  202. registry.MustRegister(factory.metricConcurrencyLimited)
  203. registry.MustRegister(factory.metricIPBlocklisted)
  204. registry.MustRegister(factory.metricReplayAttacks)
  205. return factory
  206. }