prometheus.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package main
  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. )
  10. var (
  11. prometheusEnabled = false
  12. prometheusRequestsTotal prometheus.Counter
  13. prometheusErrorsTotal *prometheus.CounterVec
  14. prometheusRequestDuration prometheus.Histogram
  15. prometheusDownloadDuration prometheus.Histogram
  16. prometheusProcessingDuration prometheus.Histogram
  17. prometheusBufferSize *prometheus.HistogramVec
  18. prometheusBufferDefaultSize *prometheus.GaugeVec
  19. prometheusBufferMaxSize *prometheus.GaugeVec
  20. prometheusVipsMemory prometheus.GaugeFunc
  21. prometheusVipsMaxMemory prometheus.GaugeFunc
  22. prometheusVipsAllocs prometheus.GaugeFunc
  23. )
  24. func initPrometheus() {
  25. if len(conf.PrometheusBind) == 0 {
  26. return
  27. }
  28. prometheusRequestsTotal = prometheus.NewCounter(prometheus.CounterOpts{
  29. Namespace: conf.PrometheusNamespace,
  30. Name: "requests_total",
  31. Help: "A counter of the total number of HTTP requests imgproxy processed.",
  32. })
  33. prometheusErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
  34. Namespace: conf.PrometheusNamespace,
  35. Name: "errors_total",
  36. Help: "A counter of the occurred errors separated by type.",
  37. }, []string{"type"})
  38. prometheusRequestDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  39. Namespace: conf.PrometheusNamespace,
  40. Name: "request_duration_seconds",
  41. Help: "A histogram of the response latency.",
  42. })
  43. prometheusDownloadDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  44. Namespace: conf.PrometheusNamespace,
  45. Name: "download_duration_seconds",
  46. Help: "A histogram of the source image downloading latency.",
  47. })
  48. prometheusProcessingDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  49. Namespace: conf.PrometheusNamespace,
  50. Name: "processing_duration_seconds",
  51. Help: "A histogram of the image processing latency.",
  52. })
  53. prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
  54. Namespace: conf.PrometheusNamespace,
  55. Name: "buffer_size_bytes",
  56. Help: "A histogram of the buffer size in bytes.",
  57. }, []string{"type"})
  58. prometheusBufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  59. Namespace: conf.PrometheusNamespace,
  60. Name: "buffer_default_size_bytes",
  61. Help: "A gauge of the buffer default size in bytes.",
  62. }, []string{"type"})
  63. prometheusBufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  64. Namespace: conf.PrometheusNamespace,
  65. Name: "buffer_max_size_bytes",
  66. Help: "A gauge of the buffer max size in bytes.",
  67. }, []string{"type"})
  68. prometheusVipsMemory = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  69. Namespace: conf.PrometheusNamespace,
  70. Name: "vips_memory_bytes",
  71. Help: "A gauge of the vips tracked memory usage in bytes.",
  72. }, vipsGetMem)
  73. prometheusVipsMaxMemory = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  74. Namespace: conf.PrometheusNamespace,
  75. Name: "vips_max_memory_bytes",
  76. Help: "A gauge of the max vips tracked memory usage in bytes.",
  77. }, vipsGetMemHighwater)
  78. prometheusVipsAllocs = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
  79. Namespace: conf.PrometheusNamespace,
  80. Name: "vips_allocs",
  81. Help: "A gauge of the number of active vips allocations.",
  82. }, vipsGetAllocs)
  83. prometheus.MustRegister(
  84. prometheusRequestsTotal,
  85. prometheusErrorsTotal,
  86. prometheusRequestDuration,
  87. prometheusDownloadDuration,
  88. prometheusProcessingDuration,
  89. prometheusBufferSize,
  90. prometheusBufferDefaultSize,
  91. prometheusBufferMaxSize,
  92. prometheusVipsMemory,
  93. prometheusVipsMaxMemory,
  94. prometheusVipsAllocs,
  95. )
  96. prometheusEnabled = true
  97. }
  98. func startPrometheusServer(cancel context.CancelFunc) error {
  99. s := http.Server{Handler: promhttp.Handler()}
  100. l, err := listenReuseport("tcp", conf.PrometheusBind)
  101. if err != nil {
  102. return fmt.Errorf("Can't start Prometheus metrics server: %s", err)
  103. }
  104. go func() {
  105. logNotice("Starting Prometheus server at %s", conf.PrometheusBind)
  106. if err := s.Serve(l); err != nil && err != http.ErrServerClosed {
  107. logError(err.Error())
  108. }
  109. cancel()
  110. }()
  111. return nil
  112. }
  113. func startPrometheusDuration(m prometheus.Histogram) func() {
  114. t := time.Now()
  115. return func() {
  116. m.Observe(time.Since(t).Seconds())
  117. }
  118. }
  119. func incrementPrometheusErrorsTotal(t string) {
  120. prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc()
  121. }
  122. func observePrometheusBufferSize(t string, size int) {
  123. prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
  124. }
  125. func setPrometheusBufferDefaultSize(t string, size int) {
  126. prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  127. }
  128. func setPrometheusBufferMaxSize(t string, size int) {
  129. prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  130. }