gauge_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package metric
  2. import (
  3. "testing"
  4. "github.com/prometheus/client_golang/prometheus/testutil"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestNewGaugeVec(t *testing.T) {
  8. gaugeVec := NewGaugeVec(&GaugeVecOpts{
  9. Namespace: "rpc_server",
  10. Subsystem: "requests",
  11. Name: "duration",
  12. Help: "rpc server requests duration(ms).",
  13. })
  14. defer gaugeVec.close()
  15. gaugeVecNil := NewGaugeVec(nil)
  16. assert.NotNil(t, gaugeVec)
  17. assert.Nil(t, gaugeVecNil)
  18. }
  19. func TestGaugeInc(t *testing.T) {
  20. gaugeVec := NewGaugeVec(&GaugeVecOpts{
  21. Namespace: "rpc_client2",
  22. Subsystem: "requests",
  23. Name: "duration_ms",
  24. Help: "rpc server requests duration(ms).",
  25. Labels: []string{"path"},
  26. })
  27. defer gaugeVec.close()
  28. gv, _ := gaugeVec.(*promGuageVec)
  29. gv.Inc("/users")
  30. gv.Inc("/users")
  31. r := testutil.ToFloat64(gv.gauge)
  32. assert.Equal(t, float64(2), r)
  33. }
  34. func TestGaugeAdd(t *testing.T) {
  35. gaugeVec := NewGaugeVec(&GaugeVecOpts{
  36. Namespace: "rpc_client",
  37. Subsystem: "request",
  38. Name: "duration_ms",
  39. Help: "rpc server requests duration(ms).",
  40. Labels: []string{"path"},
  41. })
  42. defer gaugeVec.close()
  43. gv, _ := gaugeVec.(*promGuageVec)
  44. gv.Add(-10, "/classroom")
  45. gv.Add(30, "/classroom")
  46. r := testutil.ToFloat64(gv.gauge)
  47. assert.Equal(t, float64(20), r)
  48. }
  49. func TestGaugeSet(t *testing.T) {
  50. gaugeVec := NewGaugeVec(&GaugeVecOpts{
  51. Namespace: "http_client",
  52. Subsystem: "request",
  53. Name: "duration_ms",
  54. Help: "rpc server requests duration(ms).",
  55. Labels: []string{"path"},
  56. })
  57. gaugeVec.close()
  58. gv, _ := gaugeVec.(*promGuageVec)
  59. gv.Set(666, "/users")
  60. r := testutil.ToFloat64(gv.gauge)
  61. assert.Equal(t, float64(666), r)
  62. }