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
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

prometheus.go 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(evt mtglib.EventIPBlocklisted) {
  92. p.factory.metricIPBlocklisted.Inc()
  93. }
  94. func (p prometheusProcessor) Shutdown() {
  95. for _, v := range p.streams {
  96. releaseStreamInfo(v)
  97. }
  98. p.streams = make(map[string]*streamInfo)
  99. }
  100. type PrometheusFactory struct {
  101. httpServer *http.Server
  102. metricClientConnections *prometheus.GaugeVec
  103. metricTelegramConnections *prometheus.GaugeVec
  104. metricDomainFrontingConnections *prometheus.GaugeVec
  105. metricTelegramTraffic *prometheus.CounterVec
  106. metricDomainFrontingTraffic *prometheus.CounterVec
  107. metricDomainFronting prometheus.Counter
  108. metricConcurrencyLimited prometheus.Counter
  109. metricIPBlocklisted prometheus.Counter
  110. metricReplayAttacks prometheus.Counter
  111. }
  112. func (p *PrometheusFactory) Make() events.Observer {
  113. return prometheusProcessor{
  114. streams: make(map[string]*streamInfo),
  115. factory: p,
  116. }
  117. }
  118. func (p *PrometheusFactory) Serve(listener net.Listener) error {
  119. return p.httpServer.Serve(listener)
  120. }
  121. func (p *PrometheusFactory) Close() error {
  122. return p.httpServer.Shutdown(context.Background())
  123. }
  124. func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory { // nolint: funlen
  125. registry := prometheus.NewPedanticRegistry()
  126. httpHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
  127. EnableOpenMetrics: true,
  128. })
  129. mux := http.NewServeMux()
  130. mux.Handle(httpPath, httpHandler)
  131. factory := &PrometheusFactory{
  132. httpServer: &http.Server{
  133. Handler: mux,
  134. },
  135. metricClientConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  136. Namespace: metricPrefix,
  137. Name: MetricClientConnections,
  138. Help: "A number of actively processing client connections.",
  139. }, []string{TagIPFamily}),
  140. metricTelegramConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  141. Namespace: metricPrefix,
  142. Name: MetricTelegramConnections,
  143. Help: "A number of connections to Telegram servers.",
  144. }, []string{TagTelegramIP, TagDC}),
  145. metricDomainFrontingConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  146. Namespace: metricPrefix,
  147. Name: MetricDomainFrontingConnections,
  148. Help: "A number of connections which talk to front domain.",
  149. }, []string{TagIPFamily}),
  150. metricTelegramTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
  151. Namespace: metricPrefix,
  152. Name: MetricTelegramTraffic,
  153. Help: "Traffic which is generated talking with Telegram servers.",
  154. }, []string{TagTelegramIP, TagDC, TagDirection}),
  155. metricDomainFrontingTraffic: prometheus.NewCounterVec(prometheus.CounterOpts{
  156. Namespace: metricPrefix,
  157. Name: MetricDomainFrontingTraffic,
  158. Help: "Traffic which is generated talking with front domain.",
  159. }, []string{TagDirection}),
  160. metricDomainFronting: prometheus.NewCounter(prometheus.CounterOpts{
  161. Namespace: metricPrefix,
  162. Name: MetricDomainFronting,
  163. Help: "A number of routings to front domain.",
  164. }),
  165. metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
  166. Namespace: metricPrefix,
  167. Name: MetricConcurrencyLimited,
  168. Help: "A number of sessions that were rejected by concurrency limiter.",
  169. }),
  170. metricIPBlocklisted: prometheus.NewCounter(prometheus.CounterOpts{
  171. Namespace: metricPrefix,
  172. Name: MetricIPBlocklisted,
  173. Help: "A number of rejected sessions due to ip blocklisting.",
  174. }),
  175. metricReplayAttacks: prometheus.NewCounter(prometheus.CounterOpts{
  176. Namespace: metricPrefix,
  177. Name: MetricReplayAttacks,
  178. Help: "A number of detected replay attacks.",
  179. }),
  180. }
  181. registry.MustRegister(factory.metricClientConnections)
  182. registry.MustRegister(factory.metricTelegramConnections)
  183. registry.MustRegister(factory.metricDomainFrontingConnections)
  184. registry.MustRegister(factory.metricTelegramTraffic)
  185. registry.MustRegister(factory.metricDomainFrontingTraffic)
  186. registry.MustRegister(factory.metricDomainFronting)
  187. registry.MustRegister(factory.metricConcurrencyLimited)
  188. registry.MustRegister(factory.metricIPBlocklisted)
  189. registry.MustRegister(factory.metricReplayAttacks)
  190. return factory
  191. }