counter_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package metric
  2. import (
  3. "testing"
  4. "github.com/prometheus/client_golang/prometheus/testutil"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestNewCounterVec(t *testing.T) {
  8. counterVec := NewCounterVec(&CounterVecOpts{
  9. Namespace: "http_server",
  10. Subsystem: "requests",
  11. Name: "total",
  12. Help: "rpc client requests error count.",
  13. })
  14. defer counterVec.close()
  15. counterVecNil := NewCounterVec(nil)
  16. assert.NotNil(t, counterVec)
  17. assert.Nil(t, counterVecNil)
  18. }
  19. func TestCounterIncr(t *testing.T) {
  20. counterVec := NewCounterVec(&CounterVecOpts{
  21. Namespace: "http_client",
  22. Subsystem: "call",
  23. Name: "code_total",
  24. Help: "http client requests error count.",
  25. Labels: []string{"path", "code"},
  26. })
  27. defer counterVec.close()
  28. cv, _ := counterVec.(*promCounterVec)
  29. cv.Inc("/Users", "500")
  30. cv.Inc("/Users", "500")
  31. r := testutil.ToFloat64(cv.counter)
  32. assert.Equal(t, float64(2), r)
  33. }
  34. func TestCounterAdd(t *testing.T) {
  35. counterVec := NewCounterVec(&CounterVecOpts{
  36. Namespace: "rpc_server",
  37. Subsystem: "requests",
  38. Name: "err_total",
  39. Help: "rpc client requests error count.",
  40. Labels: []string{"method", "code"},
  41. })
  42. defer counterVec.close()
  43. cv, _ := counterVec.(*promCounterVec)
  44. cv.Add(11, "/Users", "500")
  45. cv.Add(22, "/Users", "500")
  46. r := testutil.ToFloat64(cv.counter)
  47. assert.Equal(t, float64(33), r)
  48. }