prometheus.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package prometheus
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "time"
  7. "github.com/prometheus/client_golang/prometheus"
  8. "github.com/prometheus/client_golang/prometheus/promhttp"
  9. log "github.com/sirupsen/logrus"
  10. "github.com/imgproxy/imgproxy/v3/config"
  11. "github.com/imgproxy/imgproxy/v3/reuseport"
  12. )
  13. var (
  14. enabled = false
  15. requestsTotal prometheus.Counter
  16. errorsTotal *prometheus.CounterVec
  17. requestDuration prometheus.Histogram
  18. downloadDuration prometheus.Histogram
  19. processingDuration prometheus.Histogram
  20. bufferSize *prometheus.HistogramVec
  21. bufferDefaultSize *prometheus.GaugeVec
  22. bufferMaxSize *prometheus.GaugeVec
  23. )
  24. func Init() {
  25. if len(config.PrometheusBind) == 0 {
  26. return
  27. }
  28. requestsTotal = prometheus.NewCounter(prometheus.CounterOpts{
  29. Namespace: config.PrometheusNamespace,
  30. Name: "requests_total",
  31. Help: "A counter of the total number of HTTP requests imgproxy processed.",
  32. })
  33. errorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
  34. Namespace: config.PrometheusNamespace,
  35. Name: "errors_total",
  36. Help: "A counter of the occurred errors separated by type.",
  37. }, []string{"type"})
  38. requestDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  39. Namespace: config.PrometheusNamespace,
  40. Name: "request_duration_seconds",
  41. Help: "A histogram of the response latency.",
  42. })
  43. downloadDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  44. Namespace: config.PrometheusNamespace,
  45. Name: "download_duration_seconds",
  46. Help: "A histogram of the source image downloading latency.",
  47. })
  48. processingDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  49. Namespace: config.PrometheusNamespace,
  50. Name: "processing_duration_seconds",
  51. Help: "A histogram of the image processing latency.",
  52. })
  53. bufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
  54. Namespace: config.PrometheusNamespace,
  55. Name: "buffer_size_bytes",
  56. Help: "A histogram of the buffer size in bytes.",
  57. Buckets: prometheus.ExponentialBuckets(1024, 2, 14),
  58. }, []string{"type"})
  59. bufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  60. Namespace: config.PrometheusNamespace,
  61. Name: "buffer_default_size_bytes",
  62. Help: "A gauge of the buffer default size in bytes.",
  63. }, []string{"type"})
  64. bufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  65. Namespace: config.PrometheusNamespace,
  66. Name: "buffer_max_size_bytes",
  67. Help: "A gauge of the buffer max size in bytes.",
  68. }, []string{"type"})
  69. prometheus.MustRegister(
  70. requestsTotal,
  71. errorsTotal,
  72. requestDuration,
  73. downloadDuration,
  74. processingDuration,
  75. bufferSize,
  76. bufferDefaultSize,
  77. bufferMaxSize,
  78. )
  79. enabled = true
  80. }
  81. func StartServer(cancel context.CancelFunc) error {
  82. if !enabled {
  83. return nil
  84. }
  85. s := http.Server{Handler: promhttp.Handler()}
  86. l, err := reuseport.Listen("tcp", config.PrometheusBind)
  87. if err != nil {
  88. return fmt.Errorf("Can't start Prometheus metrics server: %s", err)
  89. }
  90. go func() {
  91. log.Infof("Starting Prometheus server at %s", config.PrometheusBind)
  92. if err := s.Serve(l); err != nil && err != http.ErrServerClosed {
  93. log.Error(err)
  94. }
  95. cancel()
  96. }()
  97. return nil
  98. }
  99. func StartRequest() context.CancelFunc {
  100. return startDuration(requestDuration)
  101. }
  102. func StartDownloadingSegment() context.CancelFunc {
  103. return startDuration(downloadDuration)
  104. }
  105. func StartProcessingSegment() context.CancelFunc {
  106. return startDuration(processingDuration)
  107. }
  108. func startDuration(m prometheus.Histogram) context.CancelFunc {
  109. if !enabled {
  110. return func() {}
  111. }
  112. t := time.Now()
  113. return func() {
  114. m.Observe(time.Since(t).Seconds())
  115. }
  116. }
  117. func IncrementErrorsTotal(t string) {
  118. if enabled {
  119. errorsTotal.With(prometheus.Labels{"type": t}).Inc()
  120. }
  121. }
  122. func IncrementRequestsTotal() {
  123. if enabled {
  124. requestsTotal.Inc()
  125. }
  126. }
  127. func ObserveBufferSize(t string, size int) {
  128. if enabled {
  129. bufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
  130. }
  131. }
  132. func SetBufferDefaultSize(t string, size int) {
  133. if enabled {
  134. bufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  135. }
  136. }
  137. func SetBufferMaxSize(t string, size int) {
  138. if enabled {
  139. bufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  140. }
  141. }
  142. func AddGaugeFunc(name, help string, f func() float64) {
  143. gauge := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  144. Namespace: config.PrometheusNamespace,
  145. Name: name,
  146. Help: help,
  147. }, f)
  148. prometheus.MustRegister(gauge)
  149. }