prometheus.go 4.2 KB

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