Bläddra i källkod

Use GaugeFunc for vips metrics

DarthSim 5 år sedan
förälder
incheckning
ec545138ac
2 ändrade filer med 19 tillägg och 22 borttagningar
  1. 9 9
      prometheus.go
  2. 10 13
      vips.go

+ 9 - 9
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,

+ 10 - 13
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() {