server_metrics.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package server
  2. import (
  3. "github.com/prometheus/client_golang/prometheus"
  4. )
  5. var (
  6. metricMessagesPublishedSuccess prometheus.Counter
  7. metricMessagesPublishedFailure prometheus.Counter
  8. metricMessagesCached prometheus.Gauge
  9. metricMessagePublishDurationMillis prometheus.Gauge
  10. metricFirebasePublishedSuccess prometheus.Counter
  11. metricFirebasePublishedFailure prometheus.Counter
  12. metricEmailsPublishedSuccess prometheus.Counter
  13. metricEmailsPublishedFailure prometheus.Counter
  14. metricEmailsReceivedSuccess prometheus.Counter
  15. metricEmailsReceivedFailure prometheus.Counter
  16. metricCallsMadeSuccess prometheus.Counter
  17. metricCallsMadeFailure prometheus.Counter
  18. metricUnifiedPushPublishedSuccess prometheus.Counter
  19. metricMatrixPublishedSuccess prometheus.Counter
  20. metricMatrixPublishedFailure prometheus.Counter
  21. metricAttachmentsTotalSize prometheus.Gauge
  22. metricVisitors prometheus.Gauge
  23. metricSubscribers prometheus.Gauge
  24. metricTopics prometheus.Gauge
  25. metricUsers prometheus.Gauge
  26. metricHTTPRequests *prometheus.CounterVec
  27. )
  28. func initMetrics() {
  29. metricMessagesPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  30. Name: "ntfy_messages_published_success",
  31. })
  32. metricMessagesPublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
  33. Name: "ntfy_messages_published_failure",
  34. })
  35. metricMessagesCached = prometheus.NewGauge(prometheus.GaugeOpts{
  36. Name: "ntfy_messages_cached_total",
  37. })
  38. metricMessagePublishDurationMillis = prometheus.NewGauge(prometheus.GaugeOpts{
  39. Name: "ntfy_message_publish_duration_ms",
  40. })
  41. metricFirebasePublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  42. Name: "ntfy_firebase_published_success",
  43. })
  44. metricFirebasePublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
  45. Name: "ntfy_firebase_published_failure",
  46. })
  47. metricEmailsPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  48. Name: "ntfy_emails_sent_success",
  49. })
  50. metricEmailsPublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
  51. Name: "ntfy_emails_sent_failure",
  52. })
  53. metricEmailsReceivedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  54. Name: "ntfy_emails_received_success",
  55. })
  56. metricEmailsReceivedFailure = prometheus.NewCounter(prometheus.CounterOpts{
  57. Name: "ntfy_emails_received_failure",
  58. })
  59. metricCallsMadeSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  60. Name: "ntfy_calls_made_success",
  61. })
  62. metricCallsMadeFailure = prometheus.NewCounter(prometheus.CounterOpts{
  63. Name: "ntfy_calls_made_failure",
  64. })
  65. metricUnifiedPushPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  66. Name: "ntfy_unifiedpush_published_success",
  67. })
  68. metricMatrixPublishedSuccess = prometheus.NewCounter(prometheus.CounterOpts{
  69. Name: "ntfy_matrix_published_success",
  70. })
  71. metricMatrixPublishedFailure = prometheus.NewCounter(prometheus.CounterOpts{
  72. Name: "ntfy_matrix_published_failure",
  73. })
  74. metricAttachmentsTotalSize = prometheus.NewGauge(prometheus.GaugeOpts{
  75. Name: "ntfy_attachments_total_size",
  76. })
  77. metricVisitors = prometheus.NewGauge(prometheus.GaugeOpts{
  78. Name: "ntfy_visitors_total",
  79. })
  80. metricUsers = prometheus.NewGauge(prometheus.GaugeOpts{
  81. Name: "ntfy_users_total",
  82. })
  83. metricSubscribers = prometheus.NewGauge(prometheus.GaugeOpts{
  84. Name: "ntfy_subscribers_total",
  85. })
  86. metricTopics = prometheus.NewGauge(prometheus.GaugeOpts{
  87. Name: "ntfy_topics_total",
  88. })
  89. metricHTTPRequests = prometheus.NewCounterVec(prometheus.CounterOpts{
  90. Name: "ntfy_http_requests_total",
  91. }, []string{"http_code", "ntfy_code", "http_method"})
  92. prometheus.MustRegister(
  93. metricMessagesPublishedSuccess,
  94. metricMessagesPublishedFailure,
  95. metricMessagesCached,
  96. metricMessagePublishDurationMillis,
  97. metricFirebasePublishedSuccess,
  98. metricFirebasePublishedFailure,
  99. metricEmailsPublishedSuccess,
  100. metricEmailsPublishedFailure,
  101. metricEmailsReceivedSuccess,
  102. metricEmailsReceivedFailure,
  103. metricCallsMadeSuccess,
  104. metricCallsMadeFailure,
  105. metricUnifiedPushPublishedSuccess,
  106. metricMatrixPublishedSuccess,
  107. metricMatrixPublishedFailure,
  108. metricAttachmentsTotalSize,
  109. metricVisitors,
  110. metricUsers,
  111. metricSubscribers,
  112. metricTopics,
  113. metricHTTPRequests,
  114. )
  115. }
  116. // minc increments a prometheus.Counter if it is non-nil
  117. func minc(counter prometheus.Counter) {
  118. if counter != nil {
  119. counter.Inc()
  120. }
  121. }
  122. // mset sets a prometheus.Gauge if it is non-nil
  123. func mset[T int | int64 | float64](gauge prometheus.Gauge, value T) {
  124. if gauge != nil {
  125. gauge.Set(float64(value))
  126. }
  127. }