diff --git a/bufpool.go b/bufpool.go index c53cd28f..3a7d008d 100644 --- a/bufpool.go +++ b/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()) + } } diff --git a/config.go b/config.go index 1e345606..8309c83f 100644 --- a/config.go +++ b/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() } diff --git a/download.go b/download.go index d7c3152f..9284628d 100644 --- a/download.go +++ b/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 { diff --git a/prometheus.go b/prometheus.go index 820a05ca..21ab8853 100644 --- a/prometheus.go +++ b/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) +} diff --git a/server.go b/server.go index fbd391db..3b34c2c7 100644 --- a/server.go +++ b/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) }