|
| 1 | +package sql_exporter |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + _ "net/http/pprof" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "runtime" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + |
| 12 | + _ "github.com/mithrandie/csvq-driver" |
| 13 | + |
| 14 | + "github.com/prometheus/client_golang/prometheus" |
| 15 | +) |
| 16 | + |
| 17 | +// setupCSVDir creates a temp directory with a minimal CSV file usable as a table. |
| 18 | +func setupCSVDirs(t *testing.T, n int) []string { |
| 19 | + t.Helper() |
| 20 | + base := t.TempDir() |
| 21 | + dirs := make([]string, n) |
| 22 | + for i := range n { |
| 23 | + dir := filepath.Join(base, fmt.Sprintf("csv_%d", i)) |
| 24 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 25 | + t.Fatalf("mkdir CSV dir %d: %v", i, err) |
| 26 | + } |
| 27 | + if err := os.WriteFile(filepath.Join(dir, "metrics.csv"), []byte("value\n1\n"), 0o644); err != nil { |
| 28 | + t.Fatalf("write CSV %d: %v", i, err) |
| 29 | + } |
| 30 | + dirs[i] = dir |
| 31 | + } |
| 32 | + return dirs |
| 33 | +} |
| 34 | + |
| 35 | +// writeConfig writes a sql_exporter YAML config file pointing at csvDir with |
| 36 | +// n targets, returning the config file path. |
| 37 | +func writeConfig(t *testing.T, dirs []string, n int) string { |
| 38 | + t.Helper() |
| 39 | + |
| 40 | + var sb strings.Builder |
| 41 | + for i := range n { |
| 42 | + fmt.Fprintf(&sb, ` |
| 43 | + - collector_name: col%d |
| 44 | + metrics: |
| 45 | + - metric_name: csvq_value_%d |
| 46 | + type: gauge |
| 47 | + help: "test metric %d" |
| 48 | + values: [value] |
| 49 | + query: "SELECT value FROM metrics" |
| 50 | +`, i, i, i) |
| 51 | + } |
| 52 | + collectors := sb.String() |
| 53 | + |
| 54 | + sb.Reset() |
| 55 | + for i := range n { |
| 56 | + fmt.Fprintf(&sb, " target%d: csvq:%s\n", i, dirs[i]) |
| 57 | + } |
| 58 | + targets := sb.String() |
| 59 | + |
| 60 | + content := fmt.Sprintf(` |
| 61 | +global: |
| 62 | + scrape_timeout: 10s |
| 63 | + scrape_timeout_offset: 500ms |
| 64 | + min_interval: 0s |
| 65 | + max_connections: 3 |
| 66 | + max_idle_connections: 3 |
| 67 | +
|
| 68 | +collector_files: [] |
| 69 | +
|
| 70 | +collectors:%s |
| 71 | +
|
| 72 | +jobs: |
| 73 | + - job_name: test_job |
| 74 | + collectors: [col0, col1, col2, col3, col4, col5, col6, col7, col8, col9] |
| 75 | + static_configs: |
| 76 | + - targets: |
| 77 | +%s`, collectors, targets) |
| 78 | + |
| 79 | + cfgFile := filepath.Join(t.TempDir(), "sql_exporter.yml") |
| 80 | + if err := os.WriteFile(cfgFile, []byte(content), 0o644); err != nil { |
| 81 | + t.Fatalf("write config: %v", err) |
| 82 | + } |
| 83 | + |
| 84 | + return cfgFile |
| 85 | +} |
| 86 | + |
| 87 | +func printMemStats(t *testing.T, label string) { |
| 88 | + t.Helper() |
| 89 | + var ms runtime.MemStats |
| 90 | + runtime.ReadMemStats(&ms) |
| 91 | + t.Logf("[%s] HeapAlloc=%.2f MB HeapObjects=%d", |
| 92 | + label, float64(ms.HeapAlloc)/1024/1024, ms.HeapObjects) |
| 93 | +} |
| 94 | + |
| 95 | +func runReloadCycles(t *testing.T, e Exporter, cfgFile string, numCycles int) int { |
| 96 | + t.Helper() |
| 97 | + |
| 98 | + printMemStats(t, "initial") |
| 99 | + initialGoroutines := runtime.NumGoroutine() |
| 100 | + t.Logf("initial goroutines: %d", initialGoroutines) |
| 101 | + |
| 102 | + for cycle := 1; cycle <= numCycles; cycle++ { |
| 103 | + for _, old := range e.Targets() { |
| 104 | + if err := old.Close(); err != nil { |
| 105 | + t.Logf("cycle %02d close error: %v", cycle, err) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if err := Reload(e, &cfgFile); err != nil { |
| 110 | + t.Fatalf("cycle %02d Reload: %v", cycle, err) |
| 111 | + } |
| 112 | + |
| 113 | + if cycle%10 == 0 { |
| 114 | + runtime.GC() |
| 115 | + goroutines := runtime.NumGoroutine() |
| 116 | + printMemStats(t, fmt.Sprintf("cycle %02d", cycle)) |
| 117 | + t.Logf("cycle %02d | goroutines: %d (+%d vs initial)", |
| 118 | + cycle, goroutines, goroutines-initialGoroutines) |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + return runtime.NumGoroutine() - initialGoroutines |
| 123 | +} |
| 124 | + |
| 125 | +func TestReloadMemoryLeak(t *testing.T) { |
| 126 | + const ( |
| 127 | + numTargets = 10 |
| 128 | + numCycles = 500 |
| 129 | + tolerance = 5 |
| 130 | + ) |
| 131 | + |
| 132 | + dirs := setupCSVDirs(t, numTargets) |
| 133 | + cfgFile := writeConfig(t, dirs, numTargets) |
| 134 | + |
| 135 | + e, err := NewExporter(cfgFile, prometheus.NewRegistry()) |
| 136 | + if err != nil { |
| 137 | + t.Fatalf("NewExporter: %v", err) |
| 138 | + } |
| 139 | + |
| 140 | + delta := runReloadCycles(t, e, cfgFile, numCycles) |
| 141 | + |
| 142 | + t.Logf("goroutine delta=%d (expected <= %d)", delta, tolerance) |
| 143 | + if delta > tolerance { |
| 144 | + t.Errorf("expected goroutine delta <= %d, got %d — leak still present", tolerance, delta) |
| 145 | + } |
| 146 | +} |
0 commit comments