metrics_test.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package sqlx
  2. import (
  3. "database/sql"
  4. "io"
  5. "net/http"
  6. "strings"
  7. "testing"
  8. "time"
  9. "github.com/prometheus/client_golang/prometheus"
  10. "github.com/prometheus/client_golang/prometheus/testutil"
  11. "github.com/stretchr/testify/assert"
  12. "github.com/wuntsong-org/go-zero-plus/core/conf"
  13. "github.com/wuntsong-org/go-zero-plus/internal/devserver"
  14. )
  15. func TestSqlxMetric(t *testing.T) {
  16. cfg := devserver.Config{}
  17. _ = conf.FillDefault(&cfg)
  18. cfg.Port = 6480
  19. server := devserver.NewServer(cfg)
  20. server.StartAsync()
  21. time.Sleep(time.Second)
  22. metricReqDur.Observe(8, "test-cmd")
  23. metricReqErr.Inc("test-cmd", "internal-error")
  24. metricSlowCount.Inc("test-cmd")
  25. url := "http://127.0.0.1:6480/metrics"
  26. resp, err := http.Get(url)
  27. assert.Nil(t, err)
  28. defer resp.Body.Close()
  29. s, err := io.ReadAll(resp.Body)
  30. assert.Nil(t, err)
  31. content := string(s)
  32. assert.Contains(t, content, "mysql_client_requests_duration_ms_sum{command=\"test-cmd\"} 8\n")
  33. assert.Contains(t, content, "mysql_client_requests_duration_ms_count{command=\"test-cmd\"} 1\n")
  34. assert.Contains(t, content, "mysql_client_requests_error_total{command=\"test-cmd\",error=\"internal-error\"} 1\n")
  35. assert.Contains(t, content, "mysql_client_requests_slow_total{command=\"test-cmd\"} 1\n")
  36. }
  37. func TestMetricCollector(t *testing.T) {
  38. prometheus.Unregister(connCollector)
  39. c := newCollector()
  40. c.registerClient(&statGetter{
  41. dbName: "db-1",
  42. hash: "hash-1",
  43. poolStats: func() sql.DBStats {
  44. return sql.DBStats{
  45. MaxOpenConnections: 1,
  46. OpenConnections: 2,
  47. InUse: 3,
  48. Idle: 4,
  49. WaitCount: 5,
  50. WaitDuration: 6 * time.Second,
  51. MaxIdleClosed: 7,
  52. MaxIdleTimeClosed: 8,
  53. MaxLifetimeClosed: 9,
  54. }
  55. },
  56. })
  57. c.registerClient(&statGetter{
  58. dbName: "db-1",
  59. hash: "hash-2",
  60. poolStats: func() sql.DBStats {
  61. return sql.DBStats{
  62. MaxOpenConnections: 10,
  63. OpenConnections: 20,
  64. InUse: 30,
  65. Idle: 40,
  66. WaitCount: 50,
  67. WaitDuration: 60 * time.Second,
  68. MaxIdleClosed: 70,
  69. MaxIdleTimeClosed: 80,
  70. MaxLifetimeClosed: 90,
  71. }
  72. },
  73. })
  74. c.registerClient(&statGetter{
  75. dbName: "db-2",
  76. hash: "hash-2",
  77. poolStats: func() sql.DBStats {
  78. return sql.DBStats{
  79. MaxOpenConnections: 100,
  80. OpenConnections: 200,
  81. InUse: 300,
  82. Idle: 400,
  83. WaitCount: 500,
  84. WaitDuration: 600 * time.Second,
  85. MaxIdleClosed: 700,
  86. MaxIdleTimeClosed: 800,
  87. MaxLifetimeClosed: 900,
  88. }
  89. },
  90. })
  91. val := `
  92. # HELP mysql_client_idle_connections The number of idle connections.
  93. # TYPE mysql_client_idle_connections gauge
  94. mysql_client_idle_connections{db_name="db-1",hash="hash-1"} 4
  95. mysql_client_idle_connections{db_name="db-1",hash="hash-2"} 40
  96. mysql_client_idle_connections{db_name="db-2",hash="hash-2"} 400
  97. # HELP mysql_client_in_use_connections The number of connections currently in use.
  98. # TYPE mysql_client_in_use_connections gauge
  99. mysql_client_in_use_connections{db_name="db-1",hash="hash-1"} 3
  100. mysql_client_in_use_connections{db_name="db-1",hash="hash-2"} 30
  101. mysql_client_in_use_connections{db_name="db-2",hash="hash-2"} 300
  102. # HELP mysql_client_max_idle_closed_total The total number of connections closed due to SetMaxIdleConns.
  103. # TYPE mysql_client_max_idle_closed_total counter
  104. mysql_client_max_idle_closed_total{db_name="db-1",hash="hash-1"} 7
  105. mysql_client_max_idle_closed_total{db_name="db-1",hash="hash-2"} 70
  106. mysql_client_max_idle_closed_total{db_name="db-2",hash="hash-2"} 700
  107. # HELP mysql_client_max_idle_time_closed_total The total number of connections closed due to SetConnMaxIdleTime.
  108. # TYPE mysql_client_max_idle_time_closed_total counter
  109. mysql_client_max_idle_time_closed_total{db_name="db-1",hash="hash-1"} 8
  110. mysql_client_max_idle_time_closed_total{db_name="db-1",hash="hash-2"} 80
  111. mysql_client_max_idle_time_closed_total{db_name="db-2",hash="hash-2"} 800
  112. # HELP mysql_client_max_lifetime_closed_total The total number of connections closed due to SetConnMaxLifetime.
  113. # TYPE mysql_client_max_lifetime_closed_total counter
  114. mysql_client_max_lifetime_closed_total{db_name="db-1",hash="hash-1"} 9
  115. mysql_client_max_lifetime_closed_total{db_name="db-1",hash="hash-2"} 90
  116. mysql_client_max_lifetime_closed_total{db_name="db-2",hash="hash-2"} 900
  117. # HELP mysql_client_max_open_connections Maximum number of open connections to the database.
  118. # TYPE mysql_client_max_open_connections gauge
  119. mysql_client_max_open_connections{db_name="db-1",hash="hash-1"} 1
  120. mysql_client_max_open_connections{db_name="db-1",hash="hash-2"} 10
  121. mysql_client_max_open_connections{db_name="db-2",hash="hash-2"} 100
  122. # HELP mysql_client_open_connections The number of established connections both in use and idle.
  123. # TYPE mysql_client_open_connections gauge
  124. mysql_client_open_connections{db_name="db-1",hash="hash-1"} 2
  125. mysql_client_open_connections{db_name="db-1",hash="hash-2"} 20
  126. mysql_client_open_connections{db_name="db-2",hash="hash-2"} 200
  127. # HELP mysql_client_wait_count_total The total number of connections waited for.
  128. # TYPE mysql_client_wait_count_total counter
  129. mysql_client_wait_count_total{db_name="db-1",hash="hash-1"} 5
  130. mysql_client_wait_count_total{db_name="db-1",hash="hash-2"} 50
  131. mysql_client_wait_count_total{db_name="db-2",hash="hash-2"} 500
  132. # HELP mysql_client_wait_duration_seconds_total The total time blocked waiting for a new connection.
  133. # TYPE mysql_client_wait_duration_seconds_total counter
  134. mysql_client_wait_duration_seconds_total{db_name="db-1",hash="hash-1"} 6
  135. mysql_client_wait_duration_seconds_total{db_name="db-1",hash="hash-2"} 60
  136. mysql_client_wait_duration_seconds_total{db_name="db-2",hash="hash-2"} 600
  137. `
  138. err := testutil.CollectAndCompare(c, strings.NewReader(val))
  139. assert.NoError(t, err)
  140. }