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 символов.

prometheus.go 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package stats
  2. import (
  3. "context"
  4. "net"
  5. "net/http"
  6. "time"
  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. sInfo := &streamInfo{
  18. createdAt: evt.CreatedAt,
  19. clientIP: evt.RemoteIP,
  20. }
  21. p.streams[evt.StreamID()] = sInfo
  22. p.factory.metricActiveConnections.WithLabelValues(sInfo.IPType()).Inc()
  23. }
  24. func (p prometheusProcessor) EventFinish(evt mtglib.EventFinish) {
  25. sInfo, ok := p.streams[evt.StreamID()]
  26. if !ok {
  27. return
  28. }
  29. defer delete(p.streams, evt.StreamID())
  30. duration := evt.CreatedAt.Sub(sInfo.createdAt)
  31. p.factory.metricActiveConnections.WithLabelValues(sInfo.IPType()).Dec()
  32. p.factory.metricSessionDuration.Observe(float64(duration) / float64(time.Second))
  33. }
  34. func (p prometheusProcessor) EventConcurrencyLimited(evt mtglib.EventConcurrencyLimited) {
  35. p.factory.metricConcurrencyLimited.Inc()
  36. }
  37. func (p prometheusProcessor) Shutdown() {
  38. p.streams = make(map[string]*streamInfo)
  39. }
  40. type PrometheusFactory struct {
  41. httpServer *http.Server
  42. metricActiveConnections *prometheus.GaugeVec
  43. metricConcurrencyLimited prometheus.Counter
  44. metricSessionDuration prometheus.Histogram
  45. }
  46. func (p *PrometheusFactory) Make() events.Observer {
  47. return prometheusProcessor{
  48. streams: make(map[string]*streamInfo),
  49. factory: p,
  50. }
  51. }
  52. func (p *PrometheusFactory) Serve(listener net.Listener) error {
  53. return p.httpServer.Serve(listener)
  54. }
  55. func (p *PrometheusFactory) Close() error {
  56. return p.httpServer.Shutdown(context.Background())
  57. }
  58. func NewPrometheus(metricPrefix, httpPath string) *PrometheusFactory {
  59. registry := prometheus.NewPedanticRegistry()
  60. httpHandler := promhttp.HandlerFor(registry, promhttp.HandlerOpts{
  61. EnableOpenMetrics: true,
  62. })
  63. mux := http.NewServeMux()
  64. mux.Handle(httpPath, httpHandler)
  65. factory := &PrometheusFactory{
  66. httpServer: &http.Server{
  67. Handler: mux,
  68. },
  69. metricActiveConnections: prometheus.NewGaugeVec(prometheus.GaugeOpts{
  70. Namespace: metricPrefix,
  71. Name: MetricActiveConnection,
  72. Help: "A number of connections under active processing.",
  73. }, []string{TagIPType}),
  74. metricSessionDuration: prometheus.NewHistogram(prometheus.HistogramOpts{
  75. Namespace: metricPrefix,
  76. Name: MetricSessionDuration,
  77. Help: "Session duration.",
  78. Buckets: []float64{ // per 30 seconds
  79. 30,
  80. 60,
  81. 90,
  82. 120,
  83. 150,
  84. 180,
  85. 210,
  86. 240,
  87. 270,
  88. 300,
  89. },
  90. }),
  91. metricConcurrencyLimited: prometheus.NewCounter(prometheus.CounterOpts{
  92. Namespace: metricPrefix,
  93. Name: MetricConcurrencyLimited,
  94. Help: "A number of sessions that were rejected by concurrency limiter.",
  95. }),
  96. }
  97. registry.MustRegister(factory.metricActiveConnections)
  98. registry.MustRegister(factory.metricSessionDuration)
  99. registry.MustRegister(factory.metricConcurrencyLimited)
  100. return factory
  101. }