Browse Source

Add prometheus metrics for buffers

DarthSim 6 years ago
parent
commit
e566aedcc3
5 changed files with 37 additions and 5 deletions
  1. 11 2
      bufpool.go
  2. 1 1
      config.go
  3. 1 1
      download.go
  4. 23 0
      prometheus.go
  5. 1 1
      server.go

+ 11 - 2
bufpool.go

@@ -7,6 +7,7 @@ import (
 
 type bufPool struct {
 	mutex sync.Mutex
+	name  string
 	size  int
 	top   *bufPoolEntry
 }
@@ -16,8 +17,8 @@ type bufPoolEntry struct {
 	next *bufPoolEntry
 }
 
-func newBufPool(n int, size int) *bufPool {
-	pool := bufPool{size: size}
+func newBufPool(name string, n int, size int) *bufPool {
+	pool := bufPool{name: name, size: size}
 
 	for i := 0; i < n; i++ {
 		pool.grow()
@@ -36,6 +37,10 @@ func (p *bufPool) grow() {
 	}
 
 	p.top = &bufPoolEntry{buf: buf, next: p.top}
+
+	if prometheusEnabled {
+		incrementBuffersTotal(p.name)
+	}
 }
 
 func (p *bufPool) get() *bytes.Buffer {
@@ -59,4 +64,8 @@ func (p *bufPool) put(buf *bytes.Buffer) {
 	defer p.mutex.Unlock()
 
 	p.top = &bufPoolEntry{buf: buf, next: p.top}
+
+	if prometheusEnabled {
+		observeBufferSize(p.name, buf.Cap())
+	}
 }

+ 1 - 1
config.go

@@ -441,9 +441,9 @@ func init() {
 		logFatal("GZip buffer size can't be creater than %d", ^uint32(0))
 	}
 
-	initDownloading()
 	initNewrelic()
 	initPrometheus()
+	initDownloading()
 	initErrorsReporting()
 	initVips()
 }

+ 1 - 1
download.go

@@ -80,7 +80,7 @@ func initDownloading() {
 		Transport: transport,
 	}
 
-	downloadBufPool = newBufPool(conf.Concurrency, conf.DownloadBufferSize)
+	downloadBufPool = newBufPool("download", conf.Concurrency, conf.DownloadBufferSize)
 }
 
 func checkDimensions(width, height int) error {

+ 23 - 0
prometheus.go

@@ -16,6 +16,8 @@ var (
 	prometheusRequestDuration    prometheus.Histogram
 	prometheusDownloadDuration   prometheus.Histogram
 	prometheusProcessingDuration prometheus.Histogram
+	prometheusBuffersTotal       *prometheus.CounterVec
+	prometheusBufferSize         *prometheus.HistogramVec
 )
 
 func initPrometheus() {
@@ -48,12 +50,24 @@ func initPrometheus() {
 		Help: "A histogram of the image processing latency.",
 	})
 
+	prometheusBuffersTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
+		Name: "buffers_total",
+		Help: "A counter of the total number of buffers imgproxy allocated.",
+	}, []string{"type"})
+
+	prometheusBufferSize = prometheus.NewHistogramVec(prometheus.HistogramOpts{
+		Name: "buffer_size_megabytes",
+		Help: "A histogram of the buffer size in megabytes.",
+	}, []string{"type"})
+
 	prometheus.MustRegister(
 		prometheusRequestsTotal,
 		prometheusErrorsTotal,
 		prometheusRequestDuration,
 		prometheusDownloadDuration,
 		prometheusProcessingDuration,
+		prometheusBuffersTotal,
+		prometheusBufferSize,
 	)
 
 	prometheusEnabled = true
@@ -81,3 +95,12 @@ func startPrometheusDuration(m prometheus.Histogram) func() {
 func incrementPrometheusErrorsTotal(t string) {
 	prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc()
 }
+
+func incrementBuffersTotal(t string) {
+	prometheusBuffersTotal.With(prometheus.Labels{"type": t}).Inc()
+}
+
+func observeBufferSize(t string, cap int) {
+	size := float64(cap) / 1024.0 / 1024.0
+	prometheusBufferSize.With(prometheus.Labels{"type": t}).Observe(size)
+}

+ 1 - 1
server.go

@@ -69,7 +69,7 @@ func startServer() *http.Server {
 	}
 
 	if conf.GZipCompression > 0 {
-		responseGzipBufPool = newBufPool(conf.Concurrency, conf.GZipBufferSize)
+		responseGzipBufPool = newBufPool("gzip", conf.Concurrency, conf.GZipBufferSize)
 		responseGzipPool = newGzipPool(conf.Concurrency)
 	}