metrics.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2020 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package main
  17. import (
  18. "context"
  19. "net/http"
  20. "runtime/debug"
  21. "time"
  22. "github.com/prometheus/client_golang/prometheus"
  23. "github.com/prometheus/client_golang/prometheus/promauto"
  24. "github.com/prometheus/client_golang/prometheus/promhttp"
  25. log "maunium.net/go/maulogger/v2"
  26. "maunium.net/go/mautrix/event"
  27. "maunium.net/go/mautrix-whatsapp/database"
  28. )
  29. type MetricsHandler struct {
  30. db *database.Database
  31. server *http.Server
  32. log log.Logger
  33. running bool
  34. ctx context.Context
  35. stopRecorder func()
  36. messageHandling *prometheus.HistogramVec
  37. countCollection prometheus.Histogram
  38. puppetCount prometheus.Gauge
  39. userCount prometheus.Gauge
  40. messageCount prometheus.Gauge
  41. portalCount *prometheus.GaugeVec
  42. encryptedGroupCount prometheus.Gauge
  43. encryptedPrivateCount prometheus.Gauge
  44. unencryptedGroupCount prometheus.Gauge
  45. unencryptedPrivateCount prometheus.Gauge
  46. }
  47. func NewMetricsHandler(address string, log log.Logger, db *database.Database) *MetricsHandler {
  48. portalCount := promauto.NewGaugeVec(prometheus.GaugeOpts{
  49. Name: "whatsapp_portals_total",
  50. Help: "Number of portal rooms on Matrix",
  51. }, []string{"type", "encrypted"})
  52. return &MetricsHandler{
  53. db: db,
  54. server: &http.Server{Addr: address, Handler: promhttp.Handler()},
  55. log: log,
  56. running: false,
  57. messageHandling: promauto.NewHistogramVec(prometheus.HistogramOpts{
  58. Name: "matrix_event",
  59. Help: "Time spent processing Matrix events",
  60. }, []string{"event_type"}),
  61. countCollection: promauto.NewHistogram(prometheus.HistogramOpts{
  62. Name: "whatsapp_count_collection",
  63. Help: "Time spent collecting the whatsapp_*_total metrics",
  64. }),
  65. puppetCount: promauto.NewGauge(prometheus.GaugeOpts{
  66. Name: "whatsapp_puppets_total",
  67. Help: "Number of WhatsApp users bridged into Matrix",
  68. }),
  69. userCount: promauto.NewGauge(prometheus.GaugeOpts{
  70. Name: "whatsapp_users_total",
  71. Help: "Number of Matrix users using the bridge",
  72. }),
  73. messageCount: promauto.NewGauge(prometheus.GaugeOpts{
  74. Name: "whatsapp_messages_total",
  75. Help: "Number of messages bridged",
  76. }),
  77. portalCount: portalCount,
  78. encryptedGroupCount: portalCount.With(prometheus.Labels{"type": "group", "encrypted": "true"}),
  79. encryptedPrivateCount: portalCount.With(prometheus.Labels{"type": "private", "encrypted": "true"}),
  80. unencryptedGroupCount: portalCount.With(prometheus.Labels{"type": "group", "encrypted": "false"}),
  81. unencryptedPrivateCount: portalCount.With(prometheus.Labels{"type": "private", "encrypted": "false"}),
  82. }
  83. }
  84. func (mh *MetricsHandler) TrackEvent(eventType event.Type) func() {
  85. start := time.Now()
  86. return func() {
  87. duration := time.Now().Sub(start)
  88. mh.messageHandling.
  89. With(prometheus.Labels{"event_type": eventType.Type}).
  90. Observe(duration.Seconds())
  91. }
  92. }
  93. func (mh *MetricsHandler) updateStats() {
  94. start := time.Now()
  95. var puppetCount int
  96. err := mh.db.QueryRowContext(mh.ctx, "SELECT COUNT(*) FROM puppet").Scan(&puppetCount)
  97. if err != nil {
  98. mh.log.Warnln("Failed to scan number of puppets:", err)
  99. } else {
  100. mh.puppetCount.Set(float64(puppetCount))
  101. }
  102. var userCount int
  103. err = mh.db.QueryRowContext(mh.ctx, `SELECT COUNT(*) FROM "user"`).Scan(&userCount)
  104. if err != nil {
  105. mh.log.Warnln("Failed to scan number of users:", err)
  106. } else {
  107. mh.userCount.Set(float64(userCount))
  108. }
  109. var messageCount int
  110. err = mh.db.QueryRowContext(mh.ctx, "SELECT COUNT(*) FROM message").Scan(&messageCount)
  111. if err != nil {
  112. mh.log.Warnln("Failed to scan number of messages:", err)
  113. } else {
  114. mh.messageCount.Set(float64(messageCount))
  115. }
  116. var encryptedGroupCount, encryptedPrivateCount, unencryptedGroupCount, unencryptedPrivateCount int
  117. err = mh.db.QueryRowContext(mh.ctx, `
  118. SELECT
  119. COUNT(CASE WHEN jid LIKE '%@g.us' AND encrypted THEN 1 END) AS encrypted_group_portals,
  120. COUNT(CASE WHEN jid LIKE '%@s.whatsapp.net' AND encrypted THEN 1 END) AS encrypted_private_portals,
  121. COUNT(CASE WHEN jid LIKE '%@g.us' AND NOT encrypted THEN 1 END) AS unencrypted_group_portals,
  122. COUNT(CASE WHEN jid LIKE '%@s.whatsapp.net' AND NOT encrypted THEN 1 END) AS unencrypted_private_portals
  123. FROM portal WHERE mxid<>''
  124. `).Scan(&encryptedGroupCount, &encryptedPrivateCount, &unencryptedGroupCount, &unencryptedPrivateCount)
  125. if err != nil {
  126. mh.log.Warnln("Failed to scan number of portals:", err)
  127. } else {
  128. mh.encryptedGroupCount.Set(float64(encryptedGroupCount))
  129. mh.encryptedPrivateCount.Set(float64(encryptedPrivateCount))
  130. mh.unencryptedGroupCount.Set(float64(unencryptedGroupCount))
  131. mh.unencryptedPrivateCount.Set(float64(encryptedPrivateCount))
  132. }
  133. mh.countCollection.Observe(time.Now().Sub(start).Seconds())
  134. }
  135. func (mh *MetricsHandler) startUpdatingStats() {
  136. defer func() {
  137. err := recover()
  138. if err != nil {
  139. mh.log.Fatalfln("Panic in metric updater: %v\n%s", err, string(debug.Stack()))
  140. }
  141. }()
  142. ticker := time.Tick(10 * time.Second)
  143. for {
  144. mh.updateStats()
  145. select {
  146. case <-mh.ctx.Done():
  147. return
  148. case <-ticker:
  149. }
  150. }
  151. }
  152. func (mh *MetricsHandler) Start() {
  153. mh.running = true
  154. mh.ctx, mh.stopRecorder = context.WithCancel(context.Background())
  155. go mh.startUpdatingStats()
  156. err := mh.server.ListenAndServe()
  157. mh.running = false
  158. if err != nil {
  159. mh.log.Fatalln("Error in metrics listener:", err)
  160. }
  161. }
  162. func (mh *MetricsHandler) Stop() {
  163. if !mh.running {
  164. return
  165. }
  166. mh.stopRecorder()
  167. err := mh.server.Close()
  168. if err != nil {
  169. mh.log.Errorln("Error closing metrics listener:", err)
  170. }
  171. }