-
-
Notifications
You must be signed in to change notification settings - Fork 101
Expand file tree
/
Copy pathreload_test.go
More file actions
186 lines (162 loc) · 4.61 KB
/
reload_test.go
File metadata and controls
186 lines (162 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//go:build integration
package sql_exporter
import (
"fmt"
"io"
"log/slog"
_ "net/http/pprof"
"os"
"path/filepath"
"runtime"
"testing"
_ "github.com/mithrandie/csvq-driver"
"go.yaml.in/yaml/v3"
"github.com/prometheus/client_golang/prometheus"
)
// setupCSVDirs creates a temp directory with a minimal CSV file usable as a table.
func setupCSVDirs(t *testing.T, n int) []string {
t.Helper()
base := t.TempDir()
dirs := make([]string, n)
for i := range dirs {
dir := filepath.Join(base, fmt.Sprintf("csv_%d", i))
if err := os.MkdirAll(dir, 0o755); err != nil {
t.Fatalf("mkdir CSV dir %d: %v", i, err)
}
if err := os.WriteFile(filepath.Join(dir, "metrics.csv"), []byte("value\n1\n"), 0o644); err != nil {
t.Fatalf("write CSV %d: %v", i, err)
}
dirs[i] = dir
}
return dirs
}
func writeConfig(t *testing.T, dirs []string) string {
t.Helper()
type metric struct {
Name string `yaml:"metric_name"`
Type string `yaml:"type"`
Help string `yaml:"help"`
Values []string `yaml:"values"`
Query string `yaml:"query"`
}
type collector struct {
Name string `yaml:"collector_name"`
Metrics []metric `yaml:"metrics"`
}
type staticConfig struct {
Targets map[string]string `yaml:"targets"`
}
type job struct {
Name string `yaml:"job_name"`
Collectors []string `yaml:"collectors"`
StaticConfigs []staticConfig `yaml:"static_configs"`
}
type global struct {
ScrapeTimeout string `yaml:"scrape_timeout"`
ScrapeTimeoutOffset string `yaml:"scrape_timeout_offset"`
MinInterval string `yaml:"min_interval"`
MaxConnections int `yaml:"max_connections"`
MaxIdleConnections int `yaml:"max_idle_connections"`
}
type cfg struct {
Global global `yaml:"global"`
Collectors []collector `yaml:"collectors"`
Jobs []job `yaml:"jobs"`
}
n := len(dirs)
collectors := make([]collector, n)
collectorNames := make([]string, n)
targets := make(map[string]string, n)
for i := range dirs {
name := fmt.Sprintf("col%d", i)
collectorNames[i] = name
collectors[i] = collector{
Name: name,
Metrics: []metric{{
Name: fmt.Sprintf("csvq_value_%d", i),
Type: "gauge",
Help: fmt.Sprintf("test metric %d", i),
Values: []string{"value"},
Query: "SELECT value FROM metrics",
}},
}
targets[fmt.Sprintf("target%d", i)] = "csvq:" + dirs[i]
}
c := cfg{
Global: global{
ScrapeTimeout: "10s",
ScrapeTimeoutOffset: "500ms",
MinInterval: "0s",
MaxConnections: 3,
MaxIdleConnections: 3,
},
Collectors: collectors,
Jobs: []job{{
Name: "test_job",
Collectors: collectorNames,
StaticConfigs: []staticConfig{{Targets: targets}},
}},
}
data, err := yaml.Marshal(c)
if err != nil {
t.Fatalf("marshal config: %v", err)
}
cfgFile := filepath.Join(t.TempDir(), "sql_exporter.yml")
if err := os.WriteFile(cfgFile, data, 0o644); err != nil {
t.Fatalf("write config: %v", err)
}
return cfgFile
}
func printMemStats(t *testing.T, label string) {
t.Helper()
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
t.Logf("[%s] HeapAlloc=%.2f MB HeapObjects=%d",
label, float64(ms.HeapAlloc)/1024/1024, ms.HeapObjects)
}
func runReloadCycles(t *testing.T, e Exporter, cfgFile string, numCycles int) int {
t.Helper()
printMemStats(t, "initial")
initialGoroutines := runtime.NumGoroutine()
t.Logf("initial goroutines: %d", initialGoroutines)
for cycle := 1; cycle <= numCycles; cycle++ {
for _, old := range e.Targets() {
if err := old.Close(); err != nil {
t.Logf("cycle %02d close error: %v", cycle, err)
}
}
if err := Reload(e, &cfgFile); err != nil {
t.Fatalf("cycle %02d Reload: %v", cycle, err)
}
if cycle%10 == 0 {
runtime.GC()
goroutines := runtime.NumGoroutine()
printMemStats(t, fmt.Sprintf("cycle %02d", cycle))
t.Logf("cycle %02d | goroutines: %d (+%d vs initial)",
cycle, goroutines, goroutines-initialGoroutines)
}
}
return runtime.NumGoroutine() - initialGoroutines
}
func TestReloadMemoryLeak(t *testing.T) {
const (
numTargets = 10
numCycles = 500
tolerance = 5
)
dirs := setupCSVDirs(t, numTargets)
cfgFile := writeConfig(t, dirs)
e, err := NewExporter(cfgFile, prometheus.NewRegistry())
if err != nil {
t.Fatalf("NewExporter: %v", err)
}
delta := runReloadCycles(t, e, cfgFile, numCycles)
t.Logf("goroutine delta=%d (expected <= %d)", delta, tolerance)
if delta > tolerance {
t.Errorf("expected goroutine delta <= %d, got %d — leak suspected", tolerance, delta)
}
}
func TestMain(m *testing.M) {
slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
os.Exit(m.Run())
}