Use GaugeFunc for vips metrics

This commit is contained in:
DarthSim
2020-06-10 19:08:19 +06:00
parent b783e8bebf
commit ec545138ac
2 changed files with 19 additions and 22 deletions

View File

@@ -21,9 +21,9 @@ var (
prometheusBufferSize *prometheus.HistogramVec prometheusBufferSize *prometheus.HistogramVec
prometheusBufferDefaultSize *prometheus.GaugeVec prometheusBufferDefaultSize *prometheus.GaugeVec
prometheusBufferMaxSize *prometheus.GaugeVec prometheusBufferMaxSize *prometheus.GaugeVec
prometheusVipsMemory prometheus.Gauge prometheusVipsMemory prometheus.GaugeFunc
prometheusVipsMaxMemory prometheus.Gauge prometheusVipsMaxMemory prometheus.GaugeFunc
prometheusVipsAllocs prometheus.Gauge prometheusVipsAllocs prometheus.GaugeFunc
) )
func initPrometheus() { func initPrometheus() {
@@ -79,23 +79,23 @@ func initPrometheus() {
Help: "A gauge of the buffer max size in bytes.", Help: "A gauge of the buffer max size in bytes.",
}, []string{"type"}) }, []string{"type"})
prometheusVipsMemory = prometheus.NewGauge(prometheus.GaugeOpts{ prometheusVipsMemory = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: conf.PrometheusNamespace, Namespace: conf.PrometheusNamespace,
Name: "vips_memory_bytes", Name: "vips_memory_bytes",
Help: "A gauge of the vips tracked memory usage in bytes.", Help: "A gauge of the vips tracked memory usage in bytes.",
}) }, vipsGetMem)
prometheusVipsMaxMemory = prometheus.NewGauge(prometheus.GaugeOpts{ prometheusVipsMaxMemory = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: conf.PrometheusNamespace, Namespace: conf.PrometheusNamespace,
Name: "vips_max_memory_bytes", Name: "vips_max_memory_bytes",
Help: "A gauge of the max vips tracked memory usage in bytes.", Help: "A gauge of the max vips tracked memory usage in bytes.",
}) }, vipsGetMemHighwater)
prometheusVipsAllocs = prometheus.NewGauge(prometheus.GaugeOpts{ prometheusVipsAllocs = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
Namespace: conf.PrometheusNamespace, Namespace: conf.PrometheusNamespace,
Name: "vips_allocs", Name: "vips_allocs",
Help: "A gauge of the number of active vips allocations.", Help: "A gauge of the number of active vips allocations.",
}) }, vipsGetAllocs)
prometheus.MustRegister( prometheus.MustRegister(
prometheusRequestsTotal, prometheusRequestsTotal,

23
vips.go
View File

@@ -13,7 +13,6 @@ import (
"math" "math"
"os" "os"
"runtime" "runtime"
"time"
"unsafe" "unsafe"
) )
@@ -100,8 +99,6 @@ func initVips() error {
return fmt.Errorf("Can't load watermark: %s", err) return fmt.Errorf("Can't load watermark: %s", err)
} }
vipsCollectMetrics()
return nil return nil
} }
@@ -109,16 +106,16 @@ func shutdownVips() {
C.vips_shutdown() C.vips_shutdown()
} }
func vipsCollectMetrics() { func vipsGetMem() float64 {
if prometheusEnabled { return float64(C.vips_tracked_get_mem())
go func() { }
for range time.Tick(5 * time.Second) {
prometheusVipsMemory.Set(float64(C.vips_tracked_get_mem())) func vipsGetMemHighwater() float64 {
prometheusVipsMaxMemory.Set(float64(C.vips_tracked_get_mem_highwater())) return float64(C.vips_tracked_get_mem_highwater())
prometheusVipsAllocs.Set(float64(C.vips_tracked_get_allocs())) }
}
}() func vipsGetAllocs() float64 {
} return float64(C.vips_tracked_get_allocs())
} }
func vipsCleanup() { func vipsCleanup() {