prometheus.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. package prom
  2. import (
  3. "github.com/prometheus/client_golang/prometheus"
  4. )
  5. var (
  6. // LibClient for mc redis and db client.
  7. LibClient = New().WithTimer("go_lib_client", []string{"method"}).WithState("go_lib_client_state", []string{"method", "name"}).WithCounter("go_lib_client_code", []string{"method", "code"})
  8. // RPCClient rpc client
  9. RPCClient = New().WithTimer("go_rpc_client", []string{"method"}).WithState("go_rpc_client_state", []string{"method", "name"}).WithCounter("go_rpc_client_code", []string{"method", "code"})
  10. // HTTPClient http client
  11. HTTPClient = New().WithTimer("go_http_client", []string{"method"}).WithState("go_http_client_state", []string{"method", "name"}).WithCounter("go_http_client_code", []string{"method", "code"})
  12. // HTTPServer for http server
  13. HTTPServer = New().WithTimer("go_http_server", []string{"user", "method"}).WithCounter("go_http_server_code", []string{"user", "method", "code"})
  14. // RPCServer for rpc server
  15. RPCServer = New().WithTimer("go_rpc_server", []string{"user", "method"}).WithCounter("go_rpc_server_code", []string{"user", "method", "code"})
  16. // BusinessErrCount for business err count
  17. BusinessErrCount = New().WithCounter("go_business_err_count", []string{"name"}).WithState("go_business_err_state", []string{"name"})
  18. // BusinessInfoCount for business info count
  19. BusinessInfoCount = New().WithCounter("go_business_info_count", []string{"name"}).WithState("go_business_info_state", []string{"name"})
  20. // CacheHit for cache hit
  21. CacheHit = New().WithCounter("go_cache_hit", []string{"name"})
  22. // CacheMiss for cache miss
  23. CacheMiss = New().WithCounter("go_cache_miss", []string{"name"})
  24. )
  25. // Prom struct info
  26. type Prom struct {
  27. timer *prometheus.HistogramVec
  28. counter *prometheus.CounterVec
  29. state *prometheus.GaugeVec
  30. }
  31. // New creates a Prom instance.
  32. func New() *Prom {
  33. return &Prom{}
  34. }
  35. // WithTimer with summary timer
  36. func (p *Prom) WithTimer(name string, labels []string) *Prom {
  37. if p == nil || p.timer != nil {
  38. return p
  39. }
  40. p.timer = prometheus.NewHistogramVec(
  41. prometheus.HistogramOpts{
  42. Name: name,
  43. Help: name,
  44. }, labels)
  45. prometheus.MustRegister(p.timer)
  46. return p
  47. }
  48. // WithCounter sets counter.
  49. func (p *Prom) WithCounter(name string, labels []string) *Prom {
  50. if p == nil || p.counter != nil {
  51. return p
  52. }
  53. p.counter = prometheus.NewCounterVec(
  54. prometheus.CounterOpts{
  55. Name: name,
  56. Help: name,
  57. }, labels)
  58. prometheus.MustRegister(p.counter)
  59. return p
  60. }
  61. // WithState sets state.
  62. func (p *Prom) WithState(name string, labels []string) *Prom {
  63. if p == nil || p.state != nil {
  64. return p
  65. }
  66. p.state = prometheus.NewGaugeVec(
  67. prometheus.GaugeOpts{
  68. Name: name,
  69. Help: name,
  70. }, labels)
  71. prometheus.MustRegister(p.state)
  72. return p
  73. }
  74. // Timing log timing information (in milliseconds) without sampling
  75. func (p *Prom) Timing(name string, time int64, extra ...string) {
  76. label := append([]string{name}, extra...)
  77. if p.timer != nil {
  78. p.timer.WithLabelValues(label...).Observe(float64(time))
  79. }
  80. }
  81. // Incr increments one stat counter without sampling
  82. func (p *Prom) Incr(name string, extra ...string) {
  83. label := append([]string{name}, extra...)
  84. if p.counter != nil {
  85. p.counter.WithLabelValues(label...).Inc()
  86. }
  87. if p.state != nil {
  88. p.state.WithLabelValues(label...).Inc()
  89. }
  90. }
  91. // Decr decrements one stat counter without sampling
  92. func (p *Prom) Decr(name string, extra ...string) {
  93. if p.state != nil {
  94. label := append([]string{name}, extra...)
  95. p.state.WithLabelValues(label...).Dec()
  96. }
  97. }
  98. // State set state
  99. func (p *Prom) State(name string, v int64, extra ...string) {
  100. if p.state != nil {
  101. label := append([]string{name}, extra...)
  102. p.state.WithLabelValues(label...).Set(float64(v))
  103. }
  104. }
  105. // Add add count v must > 0
  106. func (p *Prom) Add(name string, v int64, extra ...string) {
  107. label := append([]string{name}, extra...)
  108. if p.counter != nil {
  109. p.counter.WithLabelValues(label...).Add(float64(v))
  110. }
  111. if p.state != nil {
  112. p.state.WithLabelValues(label...).Add(float64(v))
  113. }
  114. }