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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

prometheus.go 7.7KB

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