浏览代码

Remove monitoring dependencies from vips

DarthSim 1 周之前
父节点
当前提交
ad82173cb0
共有 6 个文件被更改,包括 109 次插入179 次删除
  1. 23 39
      monitoring/cloudwatch/cloudwatch.go
  2. 5 22
      monitoring/datadog/datadog.go
  3. 19 25
      monitoring/newrelic/newrelic.go
  4. 36 21
      monitoring/otel/otel.go
  5. 26 20
      monitoring/prometheus/prometheus.go
  6. 0 52
      vips/vips.go

+ 23 - 39
monitoring/cloudwatch/cloudwatch.go

@@ -4,7 +4,6 @@ import (
 	"context"
 	"fmt"
 	"log/slog"
-	"slices"
 	"sync"
 	"time"
 
@@ -15,15 +14,9 @@ import (
 
 	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/monitoring/stats"
+	"github.com/imgproxy/imgproxy/v3/vips"
 )
 
-type GaugeFunc func() float64
-
-type gauge struct {
-	unit cloudwatchTypes.StandardUnit
-	f    GaugeFunc
-}
-
 type bufferStats struct {
 	count         int
 	sum, min, max int
@@ -34,9 +27,6 @@ var (
 
 	client *cloudwatch.Client
 
-	gauges      = make(map[string]gauge)
-	gaugesMutex sync.RWMutex
-
 	collectorCtx       context.Context
 	collectorCtxCancel context.CancelFunc
 
@@ -85,19 +75,6 @@ func Enabled() bool {
 	return enabled
 }
 
-func AddGaugeFunc(name, unit string, f GaugeFunc) {
-	gaugesMutex.Lock()
-	defer gaugesMutex.Unlock()
-
-	standardUnit := cloudwatchTypes.StandardUnit(unit)
-
-	if !slices.Contains(cloudwatchTypes.StandardUnitNone.Values(), standardUnit) {
-		panic(fmt.Errorf("Unknown CloudWatch unit: %s", unit))
-	}
-
-	gauges[name] = gauge{unit: standardUnit, f: f}
-}
-
 func ObserveBufferSize(t string, size int) {
 	if enabled {
 		bufferStatsMutex.Lock()
@@ -165,23 +142,9 @@ func runMetricsCollector() {
 	for {
 		select {
 		case <-tick.C:
-			metricsCount := len(gauges) + len(bufferDefaultSizes) + len(bufferMaxSizes) + len(bufferSizeStats) + 3
+			metricsCount := len(bufferDefaultSizes) + len(bufferMaxSizes) + len(bufferSizeStats) + 8
 			metrics := make([]cloudwatchTypes.MetricDatum, 0, metricsCount)
 
-			func() {
-				gaugesMutex.RLock()
-				defer gaugesMutex.RUnlock()
-
-				for name, g := range gauges {
-					metrics = append(metrics, cloudwatchTypes.MetricDatum{
-						Dimensions: []cloudwatchTypes.Dimension{dimension},
-						MetricName: aws.String(name),
-						Unit:       g.unit,
-						Value:      aws.Float64(g.f()),
-					})
-				}
-			}()
-
 			func() {
 				bufferStatsMutex.Lock()
 				defer bufferStatsMutex.Unlock()
@@ -258,6 +221,27 @@ func runMetricsCollector() {
 				),
 			})
 
+			metrics = append(metrics, cloudwatchTypes.MetricDatum{
+				Dimensions: []cloudwatchTypes.Dimension{dimension},
+				MetricName: aws.String("VipsMemory"),
+				Unit:       cloudwatchTypes.StandardUnitBytes,
+				Value:      aws.Float64(vips.GetMem()),
+			})
+
+			metrics = append(metrics, cloudwatchTypes.MetricDatum{
+				Dimensions: []cloudwatchTypes.Dimension{dimension},
+				MetricName: aws.String("VipsMaxMemory"),
+				Unit:       cloudwatchTypes.StandardUnitBytes,
+				Value:      aws.Float64(vips.GetMemHighwater()),
+			})
+
+			metrics = append(metrics, cloudwatchTypes.MetricDatum{
+				Dimensions: []cloudwatchTypes.Dimension{dimension},
+				MetricName: aws.String("VipsAllocs"),
+				Unit:       cloudwatchTypes.StandardUnitCount,
+				Value:      aws.Float64(vips.GetAllocs()),
+			})
+
 			input := cloudwatch.PutMetricDataInput{
 				Namespace:  aws.String(config.CloudWatchNamespace),
 				MetricData: metrics,

+ 5 - 22
monitoring/datadog/datadog.go

@@ -9,7 +9,6 @@ import (
 	"os"
 	"reflect"
 	"strconv"
-	"sync"
 	"time"
 
 	"github.com/DataDog/datadog-go/v5/statsd"
@@ -21,21 +20,17 @@ import (
 	"github.com/imgproxy/imgproxy/v3/monitoring/errformat"
 	"github.com/imgproxy/imgproxy/v3/monitoring/stats"
 	"github.com/imgproxy/imgproxy/v3/version"
+	"github.com/imgproxy/imgproxy/v3/vips"
 )
 
 type spanCtxKey struct{}
 
-type GaugeFunc func() float64
-
 var (
 	enabled        bool
 	enabledMetrics bool
 
 	statsdClient     *statsd.Client
 	statsdClientStop chan struct{}
-
-	gaugeFuncs      = make(map[string]GaugeFunc)
-	gaugeFuncsMutex sync.RWMutex
 )
 
 func Init() {
@@ -186,13 +181,6 @@ func SendError(ctx context.Context, errType string, err error) {
 	}
 }
 
-func AddGaugeFunc(name string, f GaugeFunc) {
-	gaugeFuncsMutex.Lock()
-	defer gaugeFuncsMutex.Unlock()
-
-	gaugeFuncs["imgproxy."+name] = f
-}
-
 func ObserveBufferSize(t string, size int) {
 	if enabledMetrics {
 		statsdClient.Histogram("imgproxy.buffer.size", float64(size), []string{"type:" + t}, 1)
@@ -217,19 +205,14 @@ func runMetricsCollector() {
 	for {
 		select {
 		case <-tick.C:
-			func() {
-				gaugeFuncsMutex.RLock()
-				defer gaugeFuncsMutex.RUnlock()
-
-				for name, f := range gaugeFuncs {
-					statsdClient.Gauge(name, f(), nil, 1)
-				}
-			}()
-
 			statsdClient.Gauge("imgproxy.workers", float64(config.Workers), nil, 1)
 			statsdClient.Gauge("imgproxy.requests_in_progress", stats.RequestsInProgress(), nil, 1)
 			statsdClient.Gauge("imgproxy.images_in_progress", stats.ImagesInProgress(), nil, 1)
 			statsdClient.Gauge("imgproxy.workers_utilization", stats.WorkersUtilization(), nil, 1)
+
+			statsdClient.Gauge("imgproxy.vips.memory", vips.GetMem(), nil, 1)
+			statsdClient.Gauge("imgproxy.vips.max_memory", vips.GetMemHighwater(), nil, 1)
+			statsdClient.Gauge("imgproxy.vips.allocs", vips.GetAllocs(), nil, 1)
 		case <-statsdClientStop:
 			return
 		}

+ 19 - 25
monitoring/newrelic/newrelic.go

@@ -17,12 +17,11 @@ import (
 	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/monitoring/errformat"
 	"github.com/imgproxy/imgproxy/v3/monitoring/stats"
+	"github.com/imgproxy/imgproxy/v3/vips"
 )
 
 type transactionCtxKey struct{}
 
-type GaugeFunc func() float64
-
 type attributable interface {
 	AddAttribute(key string, value interface{})
 }
@@ -42,9 +41,6 @@ var (
 	harvesterCtx       context.Context
 	harvesterCtxCancel context.CancelFunc
 
-	gaugeFuncs      = make(map[string]GaugeFunc)
-	gaugeFuncsMutex sync.RWMutex
-
 	bufferSummaries      = make(map[string]*telemetry.Summary)
 	bufferSummariesMutex sync.RWMutex
 
@@ -211,13 +207,6 @@ func SendError(ctx context.Context, errType string, err error) {
 	}
 }
 
-func AddGaugeFunc(name string, f GaugeFunc) {
-	gaugeFuncsMutex.Lock()
-	defer gaugeFuncsMutex.Unlock()
-
-	gaugeFuncs["imgproxy."+name] = f
-}
-
 func ObserveBufferSize(t string, size int) {
 	if enabledHarvester {
 		bufferSummariesMutex.Lock()
@@ -270,19 +259,6 @@ func runMetricsCollector() {
 	for {
 		select {
 		case <-tick.C:
-			func() {
-				gaugeFuncsMutex.RLock()
-				defer gaugeFuncsMutex.RUnlock()
-
-				for name, f := range gaugeFuncs {
-					harvester.RecordMetric(telemetry.Gauge{
-						Name:      name,
-						Value:     f(),
-						Timestamp: time.Now(),
-					})
-				}
-			}()
-
 			func() {
 				bufferSummariesMutex.RLock()
 				defer bufferSummariesMutex.RUnlock()
@@ -325,6 +301,24 @@ func runMetricsCollector() {
 				Timestamp: time.Now(),
 			})
 
+			harvester.RecordMetric(telemetry.Gauge{
+				Name:      "imgproxy.vips.memory",
+				Value:     vips.GetMem(),
+				Timestamp: time.Now(),
+			})
+
+			harvester.RecordMetric(telemetry.Gauge{
+				Name:      "imgproxy.vips.max_memory",
+				Value:     vips.GetMemHighwater(),
+				Timestamp: time.Now(),
+			})
+
+			harvester.RecordMetric(telemetry.Gauge{
+				Name:      "imgproxy.vips.allocs",
+				Value:     vips.GetAllocs(),
+				Timestamp: time.Now(),
+			})
+
 			harvester.HarvestNow(harvesterCtx)
 		case <-harvesterCtx.Done():
 			return

+ 36 - 21
monitoring/otel/otel.go

@@ -47,12 +47,11 @@ import (
 	"github.com/imgproxy/imgproxy/v3/monitoring/errformat"
 	"github.com/imgproxy/imgproxy/v3/monitoring/stats"
 	"github.com/imgproxy/imgproxy/v3/version"
+	"github.com/imgproxy/imgproxy/v3/vips"
 )
 
 type hasSpanCtxKey struct{}
 
-type GaugeFunc func() float64
-
 var (
 	enabled        bool
 	enabledMetrics bool
@@ -634,6 +633,33 @@ func addDefaultMetrics() error {
 		return fmt.Errorf("Can't add buffer_max_size_bytes gauge to OpenTelemetry: %s", err)
 	}
 
+	vipsMemory, err := meter.Float64ObservableGauge(
+		"vips_memory_bytes",
+		metric.WithUnit("By"),
+		metric.WithDescription("A gauge of the vips tracked memory usage in bytes."),
+	)
+	if err != nil {
+		return fmt.Errorf("Can't add vips_memory_bytes gauge to OpenTelemetry: %s", err)
+	}
+
+	vipsMaxMemory, err := meter.Float64ObservableGauge(
+		"vips_max_memory_bytes",
+		metric.WithUnit("By"),
+		metric.WithDescription("A gauge of the max vips tracked memory usage in bytes."),
+	)
+	if err != nil {
+		return fmt.Errorf("Can't add vips_max_memory_bytes gauge to OpenTelemetry: %s", err)
+	}
+
+	vipsAllocs, err := meter.Float64ObservableGauge(
+		"vips_allocs",
+		metric.WithUnit("1"),
+		metric.WithDescription("A gauge of the number of active vips allocations."),
+	)
+	if err != nil {
+		return fmt.Errorf("Can't add vips_allocs gauge to OpenTelemetry: %s", err)
+	}
+
 	_, err = meter.RegisterCallback(
 		func(ctx context.Context, o metric.Observer) error {
 			memStats, merr := proc.MemoryInfo()
@@ -669,6 +695,11 @@ func addDefaultMetrics() error {
 			for t, v := range bufferMaxSizes {
 				o.ObserveInt64(bufferMaxSizeGauge, int64(v), metric.WithAttributes(attribute.String("type", t)))
 			}
+
+			o.ObserveFloat64(vipsMemory, vips.GetMem())
+			o.ObserveFloat64(vipsMaxMemory, vips.GetMemHighwater())
+			o.ObserveFloat64(vipsAllocs, vips.GetAllocs())
+
 			return nil
 		},
 		processResidentMemory,
@@ -684,6 +715,9 @@ func addDefaultMetrics() error {
 		workersUtilizationGauge,
 		bufferDefaultSizeGauge,
 		bufferMaxSizeGauge,
+		vipsMemory,
+		vipsMaxMemory,
+		vipsAllocs,
 	)
 	if err != nil {
 		return fmt.Errorf("Can't register OpenTelemetry callbacks: %s", err)
@@ -701,25 +735,6 @@ func addDefaultMetrics() error {
 	return nil
 }
 
-func AddGaugeFunc(name, desc, u string, f GaugeFunc) {
-	if meter == nil {
-		return
-	}
-
-	_, err := meter.Float64ObservableGauge(
-		name,
-		metric.WithUnit(u),
-		metric.WithDescription(desc),
-		metric.WithFloat64Callback(func(_ context.Context, obsrv metric.Float64Observer) error {
-			obsrv.Observe(f())
-			return nil
-		}),
-	)
-	if err != nil {
-		slog.Warn(fmt.Sprintf("Can't add %s gauge to OpenTelemetry: %s", name, err))
-	}
-}
-
 func ObserveBufferSize(t string, size int) {
 	if enabledMetrics {
 		bufferSizeHist.Record(context.Background(), int64(size), metric.WithAttributes(attribute.String("type", t)))

+ 26 - 20
monitoring/prometheus/prometheus.go

@@ -15,6 +15,7 @@ import (
 	"github.com/imgproxy/imgproxy/v3/config"
 	"github.com/imgproxy/imgproxy/v3/monitoring/stats"
 	"github.com/imgproxy/imgproxy/v3/reuseport"
+	"github.com/imgproxy/imgproxy/v3/vips"
 )
 
 var (
@@ -33,10 +34,7 @@ var (
 	bufferDefaultSize *prometheus.GaugeVec
 	bufferMaxSize     *prometheus.GaugeVec
 
-	workers            prometheus.Gauge
-	requestsInProgress prometheus.GaugeFunc
-	imagesInProgress   prometheus.GaugeFunc
-	workersUtilization prometheus.GaugeFunc
+	workers prometheus.Gauge
 )
 
 func Init() {
@@ -112,24 +110,42 @@ func Init() {
 	})
 	workers.Set(float64(config.Workers))
 
-	requestsInProgress = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
+	requestsInProgress := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
 		Namespace: config.PrometheusNamespace,
 		Name:      "requests_in_progress",
 		Help:      "A gauge of the number of requests currently being in progress.",
 	}, stats.RequestsInProgress)
 
-	imagesInProgress = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
+	imagesInProgress := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
 		Namespace: config.PrometheusNamespace,
 		Name:      "images_in_progress",
 		Help:      "A gauge of the number of images currently being in progress.",
 	}, stats.ImagesInProgress)
 
-	workersUtilization = prometheus.NewGaugeFunc(prometheus.GaugeOpts{
+	workersUtilization := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
 		Namespace: config.PrometheusNamespace,
 		Name:      "workers_utilization",
 		Help:      "A gauge of the workers utilization in percents.",
 	}, stats.WorkersUtilization)
 
+	vipsMemoryBytes := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
+		Namespace: config.PrometheusNamespace,
+		Name:      "vips_memory_bytes",
+		Help:      "A gauge of the vips tracked memory usage in bytes.",
+	}, vips.GetMem)
+
+	vipsMaxMemoryBytes := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
+		Namespace: config.PrometheusNamespace,
+		Name:      "vips_max_memory_bytes",
+		Help:      "A gauge of the max vips tracked memory usage in bytes.",
+	}, vips.GetMemHighwater)
+
+	vipsAllocs := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
+		Namespace: config.PrometheusNamespace,
+		Name:      "vips_allocs",
+		Help:      "A gauge of the number of active vips allocations.",
+	}, vips.GetAllocs)
+
 	prometheus.MustRegister(
 		requestsTotal,
 		statusCodesTotal,
@@ -145,6 +161,9 @@ func Init() {
 		requestsInProgress,
 		imagesInProgress,
 		workersUtilization,
+		vipsMemoryBytes,
+		vipsMaxMemoryBytes,
+		vipsAllocs,
 	)
 
 	enabled = true
@@ -270,16 +289,3 @@ func SetBufferMaxSize(t string, size int) {
 		bufferMaxSize.With(prometheus.Labels{"type": t}).Set(float64(size))
 	}
 }
-
-func AddGaugeFunc(name, help string, f func() float64) {
-	if !enabled {
-		return
-	}
-
-	gauge := prometheus.NewGaugeFunc(prometheus.GaugeOpts{
-		Namespace: config.PrometheusNamespace,
-		Name:      name,
-		Help:      help,
-	}, f)
-	prometheus.MustRegister(gauge)
-}

+ 0 - 52
vips/vips.go

@@ -26,11 +26,6 @@ import (
 	"github.com/imgproxy/imgproxy/v3/ierrors"
 	"github.com/imgproxy/imgproxy/v3/imagedata"
 	"github.com/imgproxy/imgproxy/v3/imagetype"
-	"github.com/imgproxy/imgproxy/v3/monitoring/cloudwatch"
-	"github.com/imgproxy/imgproxy/v3/monitoring/datadog"
-	"github.com/imgproxy/imgproxy/v3/monitoring/newrelic"
-	"github.com/imgproxy/imgproxy/v3/monitoring/otel"
-	"github.com/imgproxy/imgproxy/v3/monitoring/prometheus"
 	"github.com/imgproxy/imgproxy/v3/vips/color"
 )
 
@@ -123,53 +118,6 @@ func Init() error {
 		vipsConf.WebpPreset = C.VIPS_FOREIGN_WEBP_PRESET_DEFAULT
 	}
 
-	prometheus.AddGaugeFunc(
-		"vips_memory_bytes",
-		"A gauge of the vips tracked memory usage in bytes.",
-		GetMem,
-	)
-	prometheus.AddGaugeFunc(
-		"vips_max_memory_bytes",
-		"A gauge of the max vips tracked memory usage in bytes.",
-		GetMemHighwater,
-	)
-	prometheus.AddGaugeFunc(
-		"vips_allocs",
-		"A gauge of the number of active vips allocations.",
-		GetAllocs,
-	)
-
-	datadog.AddGaugeFunc("vips.memory", GetMem)
-	datadog.AddGaugeFunc("vips.max_memory", GetMemHighwater)
-	datadog.AddGaugeFunc("vips.allocs", GetAllocs)
-
-	newrelic.AddGaugeFunc("vips.memory", GetMem)
-	newrelic.AddGaugeFunc("vips.max_memory", GetMemHighwater)
-	newrelic.AddGaugeFunc("vips.allocs", GetAllocs)
-
-	otel.AddGaugeFunc(
-		"vips_memory_bytes",
-		"A gauge of the vips tracked memory usage in bytes.",
-		"By",
-		GetMem,
-	)
-	otel.AddGaugeFunc(
-		"vips_max_memory_bytes",
-		"A gauge of the max vips tracked memory usage in bytes.",
-		"By",
-		GetMemHighwater,
-	)
-	otel.AddGaugeFunc(
-		"vips_allocs",
-		"A gauge of the number of active vips allocations.",
-		"1",
-		GetAllocs,
-	)
-
-	cloudwatch.AddGaugeFunc("VipsMemory", "Bytes", GetMem)
-	cloudwatch.AddGaugeFunc("VipsMaxMemory", "Bytes", GetMemHighwater)
-	cloudwatch.AddGaugeFunc("VipsAllocs", "Count", GetAllocs)
-
 	return nil
 }