diff --git a/prometheus.go b/prometheus.go index 5026bb8b..f2880ad4 100644 --- a/prometheus.go +++ b/prometheus.go @@ -21,9 +21,9 @@ var ( prometheusBufferSize *prometheus.HistogramVec prometheusBufferDefaultSize *prometheus.GaugeVec prometheusBufferMaxSize *prometheus.GaugeVec - prometheusVipsMemory prometheus.Gauge - prometheusVipsMaxMemory prometheus.Gauge - prometheusVipsAllocs prometheus.Gauge + prometheusVipsMemory prometheus.GaugeFunc + prometheusVipsMaxMemory prometheus.GaugeFunc + prometheusVipsAllocs prometheus.GaugeFunc ) func initPrometheus() { @@ -79,23 +79,23 @@ func initPrometheus() { Help: "A gauge of the buffer max size in bytes.", }, []string{"type"}) - prometheusVipsMemory = prometheus.NewGauge(prometheus.GaugeOpts{ + prometheusVipsMemory = prometheus.NewGaugeFunc(prometheus.GaugeOpts{ Namespace: conf.PrometheusNamespace, Name: "vips_memory_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, Name: "vips_max_memory_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, Name: "vips_allocs", Help: "A gauge of the number of active vips allocations.", - }) + }, vipsGetAllocs) prometheus.MustRegister( prometheusRequestsTotal, diff --git a/vips.go b/vips.go index 7f66da54..93a25bd8 100644 --- a/vips.go +++ b/vips.go @@ -13,7 +13,6 @@ import ( "math" "os" "runtime" - "time" "unsafe" ) @@ -100,8 +99,6 @@ func initVips() error { return fmt.Errorf("Can't load watermark: %s", err) } - vipsCollectMetrics() - return nil } @@ -109,16 +106,16 @@ func shutdownVips() { C.vips_shutdown() } -func vipsCollectMetrics() { - if prometheusEnabled { - go func() { - for range time.Tick(5 * time.Second) { - prometheusVipsMemory.Set(float64(C.vips_tracked_get_mem())) - prometheusVipsMaxMemory.Set(float64(C.vips_tracked_get_mem_highwater())) - prometheusVipsAllocs.Set(float64(C.vips_tracked_get_allocs())) - } - }() - } +func vipsGetMem() float64 { + return float64(C.vips_tracked_get_mem()) +} + +func vipsGetMemHighwater() float64 { + return float64(C.vips_tracked_get_mem_highwater()) +} + +func vipsGetAllocs() float64 { + return float64(C.vips_tracked_get_allocs()) } func vipsCleanup() {