prometheus.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. )
  19. func initPrometheus() {
  20. if len(conf.PrometheusBind) == 0 {
  21. return
  22. }
  23. prometheusRequestsTotal = prometheus.NewCounter(prometheus.CounterOpts{
  24. Name: "requests_total",
  25. Help: "A counter of the total number of HTTP requests imgproxy processed.",
  26. })
  27. prometheusErrorsTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
  28. Name: "errors_total",
  29. Help: "A counter of the occurred errors separated by type.",
  30. }, []string{"type"})
  31. prometheusRequestDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  32. Name: "request_duration_seconds",
  33. Help: "A histogram of the response latency.",
  34. })
  35. prometheusDownloadDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  36. Name: "download_duration_seconds",
  37. Help: "A histogram of the source image downloading latency.",
  38. })
  39. prometheusProcessingDuration = prometheus.NewHistogram(prometheus.HistogramOpts{
  40. Name: "processing_duration_seconds",
  41. Help: "A histogram of the image processing latency.",
  42. })
  43. prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
  44. Name: "buffer_size_bytes",
  45. Help: "A histogram of the buffer size in bytes.",
  46. }, []string{"type"})
  47. prometheusBufferDefaultSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  48. Name: "buffer_default_size_bytes",
  49. Help: "A gauge of the buffer default size in bytes.",
  50. }, []string{"type"})
  51. prometheusBufferMaxSize = prometheus.NewGaugeVec(prometheus.GaugeOpts{
  52. Name: "buffer_max_size_bytes",
  53. Help: "A gauge of the buffer max size in bytes.",
  54. }, []string{"type"})
  55. prometheus.MustRegister(
  56. prometheusRequestsTotal,
  57. prometheusErrorsTotal,
  58. prometheusRequestDuration,
  59. prometheusDownloadDuration,
  60. prometheusProcessingDuration,
  61. prometheusBufferSize,
  62. prometheusBufferDefaultSize,
  63. prometheusBufferMaxSize,
  64. )
  65. prometheusEnabled = true
  66. s := http.Server{
  67. Addr: conf.PrometheusBind,
  68. Handler: promhttp.Handler(),
  69. }
  70. go func() {
  71. logNotice("Starting Prometheus server at %s\n", s.Addr)
  72. if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
  73. logFatal(err.Error())
  74. }
  75. }()
  76. }
  77. func startPrometheusDuration(m prometheus.Histogram) func() {
  78. t := time.Now()
  79. return func() {
  80. m.Observe(time.Since(t).Seconds())
  81. }
  82. }
  83. func incrementPrometheusErrorsTotal(t string) {
  84. prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc()
  85. }
  86. func observePrometheusBufferSize(t string, size int) {
  87. prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(float64(size))
  88. }
  89. func setPrometheusBufferDefaultSize(t string, size int) {
  90. prometheusBufferDefaultSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  91. }
  92. func setPrometheusBufferMaxSize(t string, size int) {
  93. prometheusBufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
  94. }