prometheus.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package main
  2. import (
  3. "net/http"
  4. "time"
  5. "github.com/prometheus/client_golang/prometheus"
  6. "github.com/prometheus/client_golang/prometheus/promhttp"
  7. )
  8. var (
  9. prometheusEnabled = false
  10. prometheusRequestsTotal prometheus.Counter
  11. prometheusErrorsTotal *prometheus.CounterVec
  12. prometheusRequestDuration prometheus.Histogram
  13. prometheusDownloadDuration prometheus.Histogram
  14. prometheusProcessingDuration prometheus.Histogram
  15. prometheusBufferSize *prometheus.HistogramVec
  16. prometheusBufferDefaultSize *prometheus.GaugeVec
  17. prometheusBufferMaxSize *prometheus.GaugeVec
  18. prometheusVipsMemory prometheus.Gauge
  19. prometheusVipsMaxMemory prometheus.Gauge
  20. prometheusVipsAllocs prometheus.Gauge
  21. )
  22. func initPrometheus() {
  23. if len(conf.PrometheusBind) == 0 {
  24. return
  25. }
  26. prometheusRequestsTotal = prometheus.NewCounter(prometheus.CounterOpts{
  27. Name: "requests_total",
  28. Help: "A counter of the total number of HTTP requests imgproxy processed.",
  29. })
  30. prometheusErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
  31. Name: "errors_total",
  32. Help: "A counter of the occurred errors separated by type.",
  33. }, []string{"type"})
  34. prometheusRequestDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  35. Name: "request_duration_seconds",
  36. Help: "A histogram of the response latency.",
  37. })
  38. prometheusDownloadDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  39. Name: "download_duration_seconds",
  40. Help: "A histogram of the source image downloading latency.",
  41. })
  42. prometheusProcessingDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  43. Name: "processing_duration_seconds",
  44. Help: "A histogram of the image processing latency.",
  45. })
  46. prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
  47. Name: "buffer_size_bytes",
  48. Help: "A histogram of the buffer size in bytes.",
  49. }, []string{"type"})
  50. prometheusBufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  51. Name: "buffer_default_size_bytes",
  52. Help: "A gauge of the buffer default size in bytes.",
  53. }, []string{"type"})
  54. prometheusBufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  55. Name: "buffer_max_size_bytes",
  56. Help: "A gauge of the buffer max size in bytes.",
  57. }, []string{"type"})
  58. prometheusVipsMemory = prometheus.NewGauge(prometheus.GaugeOpts{
  59. Name: "vips_memory_bytes",
  60. Help: "A gauge of the vips tracked memory usage in bytes.",
  61. })
  62. prometheusVipsMaxMemory = prometheus.NewGauge(prometheus.GaugeOpts{
  63. Name: "vips_max_memory_bytes",
  64. Help: "A gauge of the max vips tracked memory usage in bytes.",
  65. })
  66. prometheusVipsAllocs = prometheus.NewGauge(prometheus.GaugeOpts{
  67. Name: "vips_allocs",
  68. Help: "A gauge of the number of active vips allocations.",
  69. })
  70. prometheus.MustRegister(
  71. prometheusRequestsTotal,
  72. prometheusErrorsTotal,
  73. prometheusRequestDuration,
  74. prometheusDownloadDuration,
  75. prometheusProcessingDuration,
  76. prometheusBufferSize,
  77. prometheusBufferDefaultSize,
  78. prometheusBufferMaxSize,
  79. prometheusVipsMemory,
  80. prometheusVipsMaxMemory,
  81. prometheusVipsAllocs,
  82. )
  83. prometheusEnabled = true
  84. s := http.Server{
  85. Addr: conf.PrometheusBind,
  86. Handler: promhttp.Handler(),
  87. }
  88. go func() {
  89. logNotice("Starting Prometheus server at %s\n", s.Addr)
  90. if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  91. logFatal(err.Error())
  92. }
  93. }()
  94. }
  95. func startPrometheusDuration(m prometheus.Histogram) func() {
  96. t := time.Now()
  97. return func() {
  98. m.Observe(time.Since(t).Seconds())
  99. }
  100. }
  101. func incrementPrometheusErrorsTotal(t string) {
  102. prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc()
  103. }
  104. func observePrometheusBufferSize(t string, size int) {
  105. prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
  106. }
  107. func setPrometheusBufferDefaultSize(t string, size int) {
  108. prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  109. }
  110. func setPrometheusBufferMaxSize(t string, size int) {
  111. prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  112. }