1
0

prometheus.go 2.5 KB

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