|
| 1 | +package controlplane |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + dto "github.com/prometheus/client_model/go" |
| 9 | +) |
| 10 | + |
| 11 | +func metricGaugeValue(t *testing.T, metricName string) float64 { |
| 12 | + t.Helper() |
| 13 | + families, err := prometheus.DefaultGatherer.Gather() |
| 14 | + if err != nil { |
| 15 | + t.Fatalf("failed to gather metrics: %v", err) |
| 16 | + } |
| 17 | + for _, fam := range families { |
| 18 | + if fam.GetName() != metricName { |
| 19 | + continue |
| 20 | + } |
| 21 | + if fam.GetType() != dto.MetricType_GAUGE { |
| 22 | + t.Fatalf("metric %q is not a gauge", metricName) |
| 23 | + } |
| 24 | + if len(fam.GetMetric()) == 0 { |
| 25 | + return 0 |
| 26 | + } |
| 27 | + return fam.GetMetric()[0].GetGauge().GetValue() |
| 28 | + } |
| 29 | + t.Fatalf("metric %q not found", metricName) |
| 30 | + return 0 |
| 31 | +} |
| 32 | + |
| 33 | +func metricHistogramCount(t *testing.T, metricName string) uint64 { |
| 34 | + t.Helper() |
| 35 | + families, err := prometheus.DefaultGatherer.Gather() |
| 36 | + if err != nil { |
| 37 | + t.Fatalf("failed to gather metrics: %v", err) |
| 38 | + } |
| 39 | + for _, fam := range families { |
| 40 | + if fam.GetName() != metricName { |
| 41 | + continue |
| 42 | + } |
| 43 | + if fam.GetType() != dto.MetricType_HISTOGRAM { |
| 44 | + t.Fatalf("metric %q is not a histogram", metricName) |
| 45 | + } |
| 46 | + var total uint64 |
| 47 | + for _, metric := range fam.GetMetric() { |
| 48 | + total += metric.GetHistogram().GetSampleCount() |
| 49 | + } |
| 50 | + return total |
| 51 | + } |
| 52 | + t.Fatalf("metric %q not found", metricName) |
| 53 | + return 0 |
| 54 | +} |
| 55 | + |
| 56 | +func TestControlPlaneWorkerAcquireAndSpawnMetrics(t *testing.T) { |
| 57 | + acquireBefore := metricHistogramCount(t, "duckgres_control_plane_worker_acquire_seconds") |
| 58 | + spawnBefore := metricHistogramCount(t, "duckgres_control_plane_worker_spawn_seconds") |
| 59 | + |
| 60 | + observeControlPlaneWorkerAcquire(5 * time.Millisecond) |
| 61 | + observeControlPlaneWorkerSpawn(10 * time.Millisecond) |
| 62 | + |
| 63 | + acquireAfter := metricHistogramCount(t, "duckgres_control_plane_worker_acquire_seconds") |
| 64 | + spawnAfter := metricHistogramCount(t, "duckgres_control_plane_worker_spawn_seconds") |
| 65 | + |
| 66 | + if acquireAfter-acquireBefore != 1 { |
| 67 | + t.Fatalf("expected acquire histogram sample count delta 1, got %d", acquireAfter-acquireBefore) |
| 68 | + } |
| 69 | + if spawnAfter-spawnBefore != 1 { |
| 70 | + t.Fatalf("expected spawn histogram sample count delta 1, got %d", spawnAfter-spawnBefore) |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func TestControlPlaneWorkerQueueDepthGauge(t *testing.T) { |
| 75 | + before := metricGaugeValue(t, "duckgres_control_plane_worker_queue_depth") |
| 76 | + |
| 77 | + observeControlPlaneWorkerQueueDepthDelta(1) |
| 78 | + mid := metricGaugeValue(t, "duckgres_control_plane_worker_queue_depth") |
| 79 | + observeControlPlaneWorkerQueueDepthDelta(-1) |
| 80 | + after := metricGaugeValue(t, "duckgres_control_plane_worker_queue_depth") |
| 81 | + |
| 82 | + if mid != before+1 { |
| 83 | + t.Fatalf("expected queue depth to increase by 1: before=%f mid=%f", before, mid) |
| 84 | + } |
| 85 | + if after != before { |
| 86 | + t.Fatalf("expected queue depth to return to baseline: before=%f after=%f", before, after) |
| 87 | + } |
| 88 | +} |
0 commit comments