Prechádzať zdrojové kódy

Collect vips memory metrics

DarthSim 6 rokov pred
rodič
commit
f43fd6eec0
2 zmenil súbory, kde vykonal 36 pridanie a 0 odobranie
  1. 15 0
      process.go
  2. 21 0
      prometheus.go

+ 15 - 0
process.go

@@ -13,6 +13,7 @@ import (
 	"math"
 	"os"
 	"runtime"
+	"time"
 	"unsafe"
 
 	"golang.org/x/sync/errgroup"
@@ -112,6 +113,8 @@ func initVips() {
 	if err := vipsPrepareWatermark(); err != nil {
 		logFatal(err.Error())
 	}
+
+	collectVipsMetrics()
 }
 
 func shutdownVips() {
@@ -119,6 +122,18 @@ func shutdownVips() {
 	C.vips_shutdown()
 }
 
+func collectVipsMetrics() {
+	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 cachedCString(str string) *C.char {
 	if cstr, ok := cstrings[str]; ok {
 		return cstr

+ 21 - 0
prometheus.go

@@ -19,6 +19,9 @@ var (
 	prometheusBufferSize         *prometheus.HistogramVec
 	prometheusBufferDefaultSize  *prometheus.GaugeVec
 	prometheusBufferMaxSize      *prometheus.GaugeVec
+	prometheusVipsMemory         prometheus.Gauge
+	prometheusVipsMaxMemory      prometheus.Gauge
+	prometheusVipsAllocs         prometheus.Gauge
 )
 
 func initPrometheus() {
@@ -66,6 +69,21 @@ func initPrometheus() {
 		Help: "A gauge of the buffer max size in bytes.",
 	}, []string{"type"})
 
+	prometheusVipsMemory = prometheus.NewGauge(prometheus.GaugeOpts{
+		Name: "vips_memory_bytes",
+		Help: "A gauge of the vips tracked memory usage in bytes.",
+	})
+
+	prometheusVipsMaxMemory = prometheus.NewGauge(prometheus.GaugeOpts{
+		Name: "vips_max_memory_bytes",
+		Help: "A gauge of the max vips tracked memory usage in bytes.",
+	})
+
+	prometheusVipsAllocs = prometheus.NewGauge(prometheus.GaugeOpts{
+		Name: "vips_allocs",
+		Help: "A gauge of the number of active vips allocations.",
+	})
+
 	prometheus.MustRegister(
 		prometheusRequestsTotal,
 		prometheusErrorsTotal,
@@ -75,6 +93,9 @@ func initPrometheus() {
 		prometheusBufferSize,
 		prometheusBufferDefaultSize,
 		prometheusBufferMaxSize,
+		prometheusVipsMemory,
+		prometheusVipsMaxMemory,
+		prometheusVipsAllocs,
 	)
 
 	prometheusEnabled = true