1
0
mirror of https://github.com/imgproxy/imgproxy.git synced 2025-03-17 20:17:48 +02:00

Add prometheus metrics for buffers

This commit is contained in:
DarthSim 2019-01-28 22:18:54 +06:00
parent 69549b878a
commit e566aedcc3
5 changed files with 37 additions and 5 deletions

View File

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

View File

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

View File

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

View File

@ -16,6 +16,8 @@ var (
prometheusRequestDuration prometheus.Histogram prometheusRequestDuration prometheus.Histogram
prometheusDownloadDuration prometheus.Histogram prometheusDownloadDuration prometheus.Histogram
prometheusProcessingDuration prometheus.Histogram prometheusProcessingDuration prometheus.Histogram
prometheusBuffersTotal *prometheus.CounterVec
prometheusBufferSize *prometheus.HistogramVec
) )
func initPrometheus() { func initPrometheus() {
@ -48,12 +50,24 @@ func initPrometheus() {
Help: "A histogram of the image processing latency.", 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( prometheus.MustRegister(
prometheusRequestsTotal, prometheusRequestsTotal,
prometheusErrorsTotal, prometheusErrorsTotal,
prometheusRequestDuration, prometheusRequestDuration,
prometheusDownloadDuration, prometheusDownloadDuration,
prometheusProcessingDuration, prometheusProcessingDuration,
prometheusBuffersTotal,
prometheusBufferSize,
) )
prometheusEnabled = true prometheusEnabled = true
@ -81,3 +95,12 @@ func startPrometheusDuration(m prometheus.Histogram) func() {
func incrementPrometheusErrorsTotal(t string) { func incrementPrometheusErrorsTotal(t string) {
prometheusErrorsTotal.With(prometheus.Labels{"type": t}).Inc() 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)
}

View File

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