prometheus.go 2.9 KB

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